All Classes Namespaces Files Functions Variables Typedefs Macros
clizSave.h
Go to the documentation of this file.
1 // clizSave.h - ClusterLizard header file - Save
2 
3 // Copyright (C) 2013, 2014 Simeon Knieling, M.Sc.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 //
9 
10 //Save Class
11 
12 #pragma once
13 #ifndef _INCLUDE_CLIZSAVE
14 #define _INCLUDE_CLIZSAVE
15 
16 #include "clizFunctions.h"
17 
20 namespace cliz
21 {
22  extern int inputComplete;
23  extern int numAccumulated;
24  extern int numDetected;
25 
27  extern Mutex numDetectedMutex;
28 
37  {
38  void **chunk;
39  unsigned int *cols;
40  unsigned int *rows;
41  unsigned int num;
43 
45  {
46  num = 1;
47  chunk = (void**)gMemMan.allocate(sizeof(void*)*num);
48  cols = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
49  rows = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
50  };
51  ChunkPtrStruct(unsigned int iNum)
52  {
53  num = iNum;
54  chunk = (void**)gMemMan.allocate(sizeof(void*)*num);
55  cols = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
56  rows = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
57  };
59  {
60  gMemMan.free(chunk);
61  gMemMan.free(cols);
62  gMemMan.free(rows);
63  };
64  void setNum(unsigned int iNum)
65  {
66  num = iNum;
67  gMemMan.free(chunk);
68  gMemMan.free(cols);
69  gMemMan.free(rows);
70  chunk = (void**)gMemMan.allocate(sizeof(void*)*num);
71  cols = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
72  rows = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
73  //chunk = (void**)realloc(chunk, sizeof(void*)*num);
74  //cols = (unsigned int*)realloc(cols, sizeof(unsigned int)*num);
75  //rows = (unsigned int*)realloc(rows, sizeof(unsigned int)*num);
76  };
78  {
79  if (num != CPS2.num)
80  {
81  num = CPS2.num;
82  gMemMan.free(chunk);
83  gMemMan.free(cols);
84  gMemMan.free(rows);
85  chunk = (void**)gMemMan.allocate(sizeof(void*)*num);
86  cols = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
87  rows = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
88  //chunk = (void**)realloc(chunk, sizeof(void*)*num);
89  //cols = (unsigned int*)realloc(cols, sizeof(unsigned int)*num);
90  //rows = (unsigned int*)realloc(rows, sizeof(unsigned int)*num);
91  }
92  memcpy(chunk, CPS2.chunk, sizeof(void*)*num);
93  memcpy(cols, CPS2.cols, sizeof(unsigned int)*num);
94  memcpy(rows, CPS2.rows, sizeof(unsigned int)*num);
95  return *this;
96  };
98  {
99  num = CPS2.num;
100  chunk = (void**)gMemMan.allocate(sizeof(void*)*num);
101  cols = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
102  rows = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
103  memcpy(chunk, CPS2.chunk, sizeof(void*)*num);
104  memcpy(cols, CPS2.cols, sizeof(unsigned int)*num);
105  memcpy(rows, CPS2.rows, sizeof(unsigned int)*num);
106  };
107  };
108 
117  {
118  unsigned int *indices;
119  unsigned int num;
121  {
122  num = 1;
123  indices = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
124  indices[0] = 0;
125  }
126  IndicesStruct(unsigned int iNum)
127  {
128  num = iNum;
129  indices = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
130  for (unsigned int i = 0; i < num; i++)
131  {
132  indices[i] = i;
133  }
134  }
135  IndicesStruct(unsigned int iNum, unsigned int *iIndices)
136  {
137  num = iNum;
138  indices = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
139  memcpy(indices, iIndices, sizeof(unsigned int)*num);
140  }
142  {
143  if (num != IS2.num)
144  {
145  num = IS2.num;
146  gMemMan.free(indices);
147  indices = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
148  //indices = (unsigned int*)realloc(indices, sizeof(unsigned int)*num);
149  }
150  memcpy(indices, IS2.indices, sizeof(unsigned int)*num);
151  return *this;
152  }
154  {
155  num = IS2.num;
156  indices = (unsigned int*)gMemMan.allocate(sizeof(unsigned int)*num);
157  memcpy(indices, IS2.indices, sizeof(unsigned int)*num);
158  }
160  {
161  gMemMan.free(indices);
162  };
163  };
164 
170  class Save
171  {
172  private:
173  std::vector<ChunkPtrStruct*> stack; //Should probably use a std::queue with std::deque
174  Mutex stackMutex;
175  ConVar stackConVar;
176  public:
178  {
179  stack.reserve(10);
180  };
182  {
183  Lock lockStack(stackMutex);
184  if (stack.size() < 1)
185  {
186  stack.push_back(chunk);
187  }
188  else
189  {
190  stack.insert(stack.begin(), chunk);
191  }
192  lockStack.unlock();
193  stackConVar.notify_one();
194  };
195  void popBack(ChunkPtrStruct &chunk)
196  {
197  Lock lockStack(stackMutex);
198  while (stack.size() < 1)
199  {
200  this->stackConVar.wait(lockStack);
201  }
202  chunk = (*stack[stack.size() - 1]);
203  delete stack[stack.size() - 1];
204  stack.erase(stack.end() - 1);
205  };
206  //Don't block if Save is empty. Pop out everything inside
207  void diarrhea(ChunkPtrStruct *&chunk, unsigned int &n)
208  {
209  Lock lockStack(stackMutex);
210  n = stack.size();
211  if (n > 0)
212  {
213  chunk = new ChunkPtrStruct[n];
214  for (int i = 0; i < (signed) n; i++)
215  {
216  chunk[i] = (*stack[stack.size() - 1]);
217  delete stack[stack.size() - 1];
218  stack.erase(stack.end() - 1);
219  }
220  }
221  };
222  };
223 }
224 
225 #endif
int numDetected
Definition: cliz.h:59
IndicesStruct & operator=(const IndicesStruct &IS2)
Definition: clizSave.h:141
void setNum(unsigned int iNum)
Definition: clizSave.h:64
Definition: cliz.h:55
Definition: clizSave.h:36
void popBack(ChunkPtrStruct &chunk)
Definition: clizSave.h:195
Definition: clizSave.h:116
MemoryManager * memMan
Definition: clizSave.h:42
int inputComplete
Definition: cliz.h:57
Mutex numAccumulatedMutex
Definition: cliz.h:61
unsigned int num
Definition: clizSave.h:119
~ChunkPtrStruct()
Definition: clizSave.h:58
IndicesStruct(const IndicesStruct &IS2)
Definition: clizSave.h:153
unsigned int num
Definition: clizSave.h:41
Definition: clizFunctions.h:268
void free(memPtr *memAdr)
Definition: clizFunctions.h:480
unsigned int * cols
Definition: clizSave.h:39
void ** chunk
Definition: clizSave.h:38
unsigned int * indices
Definition: clizSave.h:118
ChunkPtrStruct(const ChunkPtrStruct &CPS2)
Definition: clizSave.h:97
boost::mutex::scoped_lock Lock
Definition: clizFunctions.h:44
void diarrhea(ChunkPtrStruct *&chunk, unsigned int &n)
Definition: clizSave.h:207
Save()
Definition: clizSave.h:177
IndicesStruct()
Definition: clizSave.h:120
void pushFront(ChunkPtrStruct *chunk)
Definition: clizSave.h:181
memPtr * allocate(size_t numBytes)
Definition: clizFunctions.h:447
IndicesStruct(unsigned int iNum, unsigned int *iIndices)
Definition: clizSave.h:135
ChunkPtrStruct()
Definition: clizSave.h:44
int numAccumulated
Definition: cliz.h:58
GlobalMemoryManager gMemMan
Definition: clizFunctions.cpp:14
Mutex numDetectedMutex
Definition: cliz.h:62
~IndicesStruct()
Definition: clizSave.h:159
unsigned int * rows
Definition: clizSave.h:40
boost::mutex Mutex
Definition: clizFunctions.h:42
ChunkPtrStruct & operator=(const ChunkPtrStruct &CPS2)
Definition: clizSave.h:77
boost::condition_variable ConVar
Definition: clizFunctions.h:43
ChunkPtrStruct(unsigned int iNum)
Definition: clizSave.h:51
Definition: clizSave.h:170
IndicesStruct(unsigned int iNum)
Definition: clizSave.h:126