AlcapDAQ  1
MqlArray.h
Go to the documentation of this file.
1 //
2 // Tables are kept in arrays of structures. Each time an element is added,
3 // the array size increases according to size_new = N + C*size_old, where N
4 // would typically be of order 10 and C would typically be of order 2. It is
5 // necessary to copy all values when changing the array size, so it should
6 // be done fairly infrequently.
7 //
8 
9 #ifndef MQLARRAY
10 #define MQLARRAY
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 static const int REALLOC_N = 2;
16 static const int REALLOC_C = 8;
17 
18 template<typename T> class MqlArray
19 {
20  public:
21 
23  fArray = NULL;
24  fSize = 0;
25  fLastSize = 0;
26  fAllocation = 0;
27  fImmutable = false;
28  }
29 
31  if(!fImmutable && fArray != NULL) {
32  delete[] fArray;
33  }
34  }
35 
36  inline T& operator[] (int i) {
37  return fArray[i];
38  }
39 
40  inline int next() {
41  int retval = fSize;
42  setSize(fSize + 1);
43  return retval;
44  }
45 
46  inline int size() {
47  return fSize;
48  }
49 
50  inline void setSize(int size) {
51  if(size > fAllocation) {
52  realloc(size);
53  }
54  fSize = size;
55  }
56 
57  inline void reset() {
58 #if 0
59  if(!fImmutable) {
60  int nextSize = (int) (fLastSize * 0.9);
61  fSize = 0;
62  realloc(nextSize);
63  }
64 #endif
65  }
66 
67  inline void finish() {
68 #if 0
69  if(!fImmutable) {
70  fAllocation = 0;
71  fLastSize = fSize;
72  fSize = 0;
73 
74  if(fArray != NULL) {
75  delete[] fArray;
76  fArray = NULL;
77  }
78  }
79 #else
80  if(!fImmutable) {
81  fSize = 0;
82  }
83 #endif
84  }
85 
86  inline void sort(int (*compar)(const void *, const void *)) {
87  qsort(fArray, fSize, sizeof(T), compar);
88  }
89 
90  inline bool checkSort(int (*compar)(const void *, const void *)) {
91  for(int i = 1; i < fSize; i++) {
92 // if(compar(&fArray[i-1], &fArray[i]) > 0) {
93  if(compar(&(*this)[i-1], &(*this)[i]) > 0) {
94 printf("checkSort: problem at element %d\n", i);
95  return false;
96  }
97  }
98 
99  return true;
100  }
101 
102  void realloc(int minSize) {
103 
104  int newSize = REALLOC_N*fAllocation + REALLOC_C;
105 
106  if(minSize > newSize) {
107  newSize = minSize;
108  }
109 
110  // make a new array and put it into place
111  T *oldArray = fArray;
112  T *newArray = new T[newSize];
113  fArray = newArray;
114  fAllocation = newSize;
115 
116  // copy the contents and delete the old array
117  if(oldArray != NULL) {
118  memcpy(newArray, oldArray, fSize*sizeof(T));
119  memset(newArray+fSize, 0, (newSize-fSize)*sizeof(T));
120  delete[] oldArray;
121  } else {
122  memset(newArray, 0, (newSize)*sizeof(T));
123  }
124  }
125 
126  void fromBank(void *pevent, char *bankName) {
127  fSize = bk_locate(pevent, bankName, (DWORD*) &fArray)
128  * sizeof(DWORD) / sizeof(T);
129  if(fArray == NULL)
130  {
131  printf("Warning: could not find bank %s\n", bankName);
132  }
133  fImmutable = true;
134  }
135 
136  void toBank(void *pevent, char *bankName) {
137  T *outputArray;
138  bk_create(pevent, bankName, TID_DWORD, (DWORD*) &outputArray);
139  memcpy(outputArray, fArray, fSize * sizeof(T));
140  bk_close(pevent, outputArray + fSize);
141  }
142 
143  private:
144 
145  T *fArray;
147  int fSize;
150 };
151 
152 #endif