ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fastvec.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file fastvec.h
1 #ifndef __fastvec_h__
2 #define __fastvec_h__
3 
4 #include <vector>
5 #include <cstring>
6 #include <iostream>
7 
8 
9 // a vector which resides on the stack when small enough,
10 // otherwise it resides on the heap
11 class fastvec
12 {
13  public:
14  fastvec() : size(0){}
16 
17  void push_back(unsigned int a)
18  {
19  if(size > 16383)
20  {
21  vec.push_back(a);
22  size += 1;
23  }
24  else
25  {
26  arr[size] = a;
27  size += 1;
28  }
29  }
30 
31  void push_back(unsigned int* a, unsigned int num)
32  {
33  if( (size+num) < 16384)
34  {
35  memcpy(&(arr[size]), a, (num<<2));
36  size += num;
37  }
38  else
39  {
40  for(unsigned int i=0;i<num;++i)
41  {
42  push_back(a[i]);
43  }
44  }
45  }
46 
47  unsigned int& operator[](unsigned int idx)
48  {
49 // unsigned long int address = (( (unsigned long int)(&(arr[idx])) & ((unsigned long int)((idx > 16383) - 1))) ^ ( (unsigned long int)(&(vec[idx-16384])) & ((unsigned long int)((idx <= 16383) - 1))));
50 //
51 // return ( *( (unsigned int*)(address) ) );
52 
53  if(idx <= 16383)
54  {
55  return arr[idx];
56  }
57  else
58  {
59  return vec[idx-16384];
60  }
61  }
62 
63  void operator=(const fastvec& other)
64  {
65  size = other.size;
66  if(size < 16384)
67  {
68  for(unsigned int i=0;i<size;++i)
69  {
70  arr[i] = other.arr[i];
71  }
72  }
73  else
74  {
75  for(unsigned int i=0;i<16384;++i)
76  {
77  arr[i] = other.arr[i];
78  }
79  vec = other.vec;
80  }
81  }
82 
83  void clear()
84  {
85  size = 0;
86  vec.clear();
87  }
88 
89  std::vector<unsigned int> vec;
90  unsigned int arr[16384];
91  unsigned int size;
92 };
93 
94 
95 class fastvec2d
96 {
97  public:
98  fastvec2d(unsigned int second_dimension_size) : size(0), size2(second_dimension_size)
99  {
100  nstack = (16384/size2);
101  }
103 
104  void fill(unsigned int* buf, unsigned int bufsize)
105  {
106  if(size < nstack)
107  {
108  arr_size[size] = bufsize;
109  memcpy(&(arr[size*size2]), &(buf[0]), (bufsize<<2));
110  size += 1;
111  }
112  else
113  {
114  std::vector<unsigned int> tempvec;
115  for(unsigned int i=0;i<bufsize;++i)
116  {
117  tempvec.push_back(buf[i]);
118  }
119  vec.push_back(tempvec);
120  size += 1;
121  }
122  }
123 
124  unsigned int n_entries(unsigned int idx)
125  {
126  if(idx < nstack)
127  {
128  return arr_size[idx];
129  }
130  else
131  {
132  return vec[idx - nstack].size();
133  }
134  }
135 
136  unsigned int& operator()(unsigned int idx1, unsigned int idx2)
137  {
138  if(idx1 < nstack)
139  {
140  return arr[idx1*size2 + idx2];
141  }
142  else
143  {
144  return vec[idx1 - nstack][idx2];
145  }
146  }
147 
148  void fetch(unsigned int begin, unsigned int end, unsigned int* result_arr, unsigned int* result_size)
149  {
150  if(end < nstack)
151  {
152  memcpy(result_arr, &(arr[begin*size2]), ((1+end-begin)*size2)*4);
153  memcpy(result_size, &(arr_size[begin]), (1+end-begin)*4);
154  }
155  else
156  {
157  for(unsigned int i=begin;i<=end;++i)
158  {
159  unsigned int entries = n_entries(i);
160  result_size[i-begin] = entries;
161  for(unsigned int j=0;j<entries;++j)
162  {
163  result_arr[(i-begin)*size2 + j] = (*this)(i,j);
164  }
165  }
166  }
167  }
168 
169 
170  unsigned int arr[16384];
171  unsigned int arr_size[16384];
172  unsigned int size;
173  unsigned int size2;
174  unsigned int nstack;
175  std::vector<std::vector<unsigned int> > vec;
176 };
177 
178 
179 
180 #endif