-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray.h
170 lines (155 loc) · 3.86 KB
/
Array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef __ARRAY_H
#define __ARRAY_H
#include <cerrno>
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
using namespace std;
class Array {
protected:
int *theArray;
char theFlg;
int lastPos;
unsigned int theSize;
unsigned int totSize;
long *offset;
public:
Array(int sz, int npart=1);
~Array();
int operator [] (unsigned int index)
{
return theArray[index];
};
char flg()
{
return theFlg;
}
void setflg(char flg)
{
theFlg = flg;
}
int lastpos()
{
return lastPos;
}
//to be used ony for use_seq
void setlastpos()
{
theArray[lastPos+1] = theSize-lastPos-2;
lastPos = theSize;
}
long get_offset(int pos=0)
{
return offset[pos];
}
void set_offset(long off, int pos=0)
{
offset[pos] = off;
}
int totsize()
{
return totSize;
}
void reset()
{
theSize = 0;
lastPos = 0;
theFlg = 0;
}
int *array()
{
return theArray;
}
int size()
{
return theSize;
}
void setsize(int size)
{
theSize = size;
}
void setitem(int pos, int item)
{
theArray[pos] = item;
}
void additem(int item){
theArray[theSize] = item;
theSize++;
}
void flushbuf(int fd, int use_seq, int pos=0)
{
lseek(fd, offset[pos]*sizeof(int),SEEK_SET);
// int wblk = (use_seq==1) ? lastPos : theSize;
// //if (lastPos != theSize)
// // cout << "WBLK " << wblk << " " << lastPos << " "
// // << theSize << endl << flush;
int wblk = theSize;
if (wblk > 0){
int res = ::write(fd, (char *)theArray, wblk*sizeof(int));
if (res < wblk*sizeof(int)){
perror("Error writing");
exit(errno);
}
offset[pos] += wblk;
}
theSize = 0;
}
void add (int fd, int item, int use_seq, int pos, int custid=-1)
{
if (use_seq){
// if (theSize+1+((custid == -1)?0:2)> totSize){
// //cout << "WRITE " << item << " " << custid << " "
// // << offset << " " << lastPos << " " << theSize << " "
// // << totSize << endl << flush;
// if (lastPos == 0 && custid == -1){
// cout << "REALLOC " << totSize << " "<< theSize << endl;
// totSize *= 2;
// theArray = (int *)realloc(theArray, totSize*sizeof(int));
// if (theArray == NULL){
// perror("ERROR IN REALLOC Array::add");
// exit(errno);
// }
// }
// else{
// flushbuf(fd,use_seq,pos);
// for (int i=0; lastPos < theSize; i++, lastPos++)
// theArray[i] = theArray[lastPos];
// theSize = i;
// lastPos = 0;
// //cout << "WROTE " << theSize << " " << lastPos << " "<<
// // offset << endl <<flush;
// }
// }
// if (custid !=-1){
// theArray[theSize++] = custid; //store custid
// theSize++; //for the tid count
// }
if (theSize+2 > totSize){
flushbuf(fd,use_seq,pos);
}
theArray[theSize++] = custid;
}
else{
if (theSize+1 > totSize){
flushbuf(fd,use_seq,pos);
}
}
theArray[theSize++] = item;
}
// void add (int fd, int item, int use_seq, int custid=-1)
// {
// if (theSize+1 > totSize){
// totSize = (int) (totSize*2);
// theArray = (int *)realloc(theArray, totSize*sizeof(int));
// if (theArray == NULL){
// cout << "MEMORY EXCEEDED\n";
// exit(-1);
// }
// }
// theArray[theSize] = item;
// theSize++;
// }
};
#endif //__ARRAY_H