KSquare Utilities
StartStopPoint.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdlib.h>
3 #include <memory.h>
4 
5 #include <string>
6 #include <iostream>
7 #include <fstream>
8 #include <vector>
9 #include <map>
10 
11 #include "MemoryDebug.h"
12 using namespace std;
13 
14 
15 #include "GlobalGoalKeeper.h"
16 #include "KKBaseTypes.h"
17 #include "KKStrParser.h"
18 #include "OSservices.h"
19 #include "KKStr.h"
20 using namespace KKB;
21 
22 #include "Variables.h"
23 
24 #include "StartStopPoint.h"
25 using namespace KKLSC;
26 
27 
28 
29 KKB::KKStr StartStopPoint::startStopPointStrs [] = {"NULL", "StartPoint", "Start", "StopPoint", "Stop", "Invalid", ""};
31 
33 {
34  if (t <= StartStopType::Null) return (startStopPointStrs[(int)StartStopType::Null]);
35  if (t >= StartStopType::Invalid) return (startStopPointStrs[(int)StartStopType::Invalid]);
36  return (startStopPointStrs[(int)t]);
37 }
38 
39 
40 
42 {
43  for (int x = (int)StartStopType::Null; x < (int)StartStopType::Invalid; ++x)
44  if (s.EqualIgnoreCase (startStopPointStrs[x]))
45  return (StartStopType)x;
46  return StartStopType::Null;
47 }
48 
49 
51  StartStopType _type
52  ):
53  scanLineNum (_scanLineNum),
54  type (_type)
55 {
56 }
57 
58 
59 
61  scanLineNum (entry.scanLineNum),
62  type (entry.type)
63 {
64 }
65 
66 
67 
69  scanLineNum (-1),
71 {
73 }
74 
75 
76 
78 {
79 }
80 
81 
83 {
84  return sizeof (*this);
85 }
86 
87 
88 
89 
91 {
92  KKStr s (48);
93  s << scanLineNum << "\t" << TypeStr ();
94  return s;
95 }
96 
97 
98 
100 {
101  scanLineNum = parser.ExtractTokenInt ("\t");
102  KKStr typeStr = parser.ExtractToken2 ("\t");
103  type = StartStopTypeFromStr (typeStr);
104 }
105 
106 
107 
108 
111 {
112 }
113 
114 
116 {
117  Clear ();
118 }
119 
120 
122 {
123  while (size () > 0)
124  {
125  StartStopPointPtr e = back ();
126  pop_back ();
127  delete e;
128  e = NULL;
129  }
130 }
131 
132 
133 
135 {
136  const_iterator idx2;
137 
138  kkint32 mem = sizeof (*this);
139 
140  for (idx2 = begin (); idx2 != end (); ++idx2)
141  {
142  StartStopPointPtr sp = *idx2;
143  mem += sp->MemoryConsumedEstimated ();
144  }
145 
146  return mem;
147 }
148 
149 
150 
151 
152 StartStopPointPtr StartStopPointList::AddEntry (kkint32 _scanLineNum,
154  )
155 {
156  StartStopPointPtr newEntry = new StartStopPoint (_scanLineNum, _type);
157  return AddEntry (newEntry);
158 } /* AddEntry */
159 
160 
161 
162 StartStopPointPtr StartStopPointList::AddEntry (StartStopPointPtr& _entry)
163 {
164  if (size () < 1)
165  {
166  push_back (_entry);
167  return _entry;
168  }
169 
170  StartStopPointPtr entryAdded = _entry;
171 
172  kkint32 m = FindGreaterOrEqual (_entry->ScanLineNum ());
173  if (m < 0)
174  push_back (_entry);
175 
176  else
177  {
178  StartStopPointPtr existingEntry = (*this)[m];
179  if (existingEntry->ScanLineNum () == _entry->ScanLineNum ())
180  {
181  entryAdded = existingEntry;
182  existingEntry->Type (_entry->Type ());
183  delete _entry;
184  _entry = NULL;
185  }
186  else
187  {
188  entryAdded = _entry;
189  idx = begin () + m;
190  insert (idx, _entry);
191  }
192  }
193 
194  return entryAdded;
195 } /* AddEntry */
196 
197 
198 
200 {
201  kkint32 m = FindEqual (_scanLineNum);
202  if (m < 0)
203  return;
204 
205  StartStopPointPtr entry = (*this)[m];
206  erase (begin () + m);
207  delete entry;
208 }
209 
210 
211 kkint32 StartStopPointList::FindEqual (kkint32 _scanLineNum) const
212 {
213  if (size () < 1)
214  return -1;
215 
216  kkuint32 b = 0;
217  kkuint32 e = size () - 1;
218  kkuint32 m = 0;
219 
220  kkint32 entryScanLineNum = 0;
221 
222  do
223  {
224  m = (b + e) / 2;
225 
226  entryScanLineNum = (*this)[m]->ScanLineNum ();
227 
228  if (entryScanLineNum == _scanLineNum)
229  return m;
230 
231  if (entryScanLineNum > _scanLineNum)
232  e = m - 1;
233 
234  else
235  b = m + 1;
236 
237  } while (b <= e);
238  return -1;
239 } /* FindGreaterOrEqual */
240 
241 
242 
243 
244 kkint32 StartStopPointList::FindGreaterOrEqual (kkint32 _scanLineNum) const
245 {
246  if (size () < 1)
247  return -1;
248 
249  if ((*this)[size () - 1]->ScanLineNum () < _scanLineNum)
250  return -1;
251 
252  kkint32 b = 0;
253  kkint32 e = (kkint32)size () - 1;
254  kkint32 m = 0;
255 
256  kkint32 entryScanLineNum = 0;
257 
258  do
259  {
260  m = (b + e) / 2;
261 
262  entryScanLineNum = (*this)[m]->ScanLineNum ();
263 
264  if (entryScanLineNum == _scanLineNum)
265  return m;
266 
267  if (entryScanLineNum > _scanLineNum)
268  e = m - 1;
269 
270  else
271  b = m + 1;
272 
273  } while (b <= e);
274 
275  if (entryScanLineNum > _scanLineNum)
276  return m;
277 
278  if (m < ((kkint32)size () - 1))
279  return m + 1;
280  else
281  return -1;
282 } /* FindGreaterOrEqual */
283 
284 
285 
286 kkint32 StartStopPointList::FindLessOrEqual (kkint32 _scanLineNum) const
287 {
288  if (size () < 1)
289  return -1;
290 
291  if ((*this)[0]->ScanLineNum () > _scanLineNum)
292  return -1;
293 
294  kkint32 b = 0;
295  kkint32 e = (kkint32)size () - 1;
296  kkint32 m = 0;
297 
298  kkint32 entryScanLineNum = 0;
299 
300  do
301  {
302  m = (b + e) / 2;
303 
304  entryScanLineNum = (*this)[m]->ScanLineNum ();
305 
306  if (entryScanLineNum == _scanLineNum)
307  return m;
308 
309  if (entryScanLineNum > _scanLineNum)
310  e = m - 1;
311 
312  else
313  b = m + 1;
314 
315  } while (b <= e);
316 
317  if (entryScanLineNum < _scanLineNum)
318  return m;
319 
320  if (m > 0)
321  return m - 1;
322  else
323  return -1;
324 } /* FindLessOrEqual */
325 
326 
327 
328 StartStopPointPtr StartStopPointList::PrevEntry (kkint32 _scanLineNum) const
329 {
330  kkint32 m = FindLessOrEqual (_scanLineNum);
331  if (m >= 0)
332  return (*this)[m];
333  else
334  return NULL;
335 } /* PrevEntry */
336 
337 
338 
339 StartStopPointPtr StartStopPointList::SuccEntry (kkint32 _scanLineNum) const
340 {
341  kkint32 m = FindGreaterOrEqual (_scanLineNum);
342  if (m >= 0)
343  return (*this)[m];
344  else
345  return NULL;
346 } /* SuccEntry */
347 
348 
349 
350 StartStopPointPtr StartStopPointList::NearestEntry (kkint32 _scanLineNum) const
351 {
352  if (size () < 1)
353  return NULL;
354 
355  else if (size () == 1)
356  return (*this)[0];
357 
358  kkint32 m = FindLessOrEqual (_scanLineNum);
359  if (m < 0)
360  return (*this)[0];
361 
362  if (m >= ((kkint32)size () - 1))
363  return (*this)[m];
364 
365  kkint32 deltaBefore = (_scanLineNum - (*this)[m]->ScanLineNum ());
366  kkint32 deltaAfter = (*this)[m + 1]->ScanLineNum () - _scanLineNum;
367  if (deltaBefore <= deltaAfter)
368  return (*this)[m];
369  else
370  return (*this)[m + 1];
371 }
372 
373 
374 
375 
377  kkint32 _end
378  ):
379  start (_start),
380  end (_end)
381 {
382 }
383 
384 
385 
388 {
389 }
390 
391 
392 
394  KKQueue<StartStopRegion> (true)
395 {
396  if (startStopPoints.size () < 1)
397  {
398  PushOnBack (new StartStopRegion (0, int32_max));
399  }
400  else
401  {
402  kkint32 prevLineNum = 0;
403  StartStopPoint::StartStopType prevType = StartStopPoint::StartStopType::StartPoint;
404 
405  StartStopPointList::const_iterator idx = startStopPoints.begin ();
406  while (idx != startStopPoints.end ())
407  {
408  kkint32 nextLineNum = (*idx)->ScanLineNum ();
409  StartStopPoint::StartStopType nextType = (*idx)->Type ();
410 
411  if (prevType == StartStopPoint::StartStopType::StartPoint)
412  {
413  if (nextType == StartStopPoint::StartStopType::StopPoint)
414  {
415  PushOnBack (new StartStopRegion (prevLineNum, nextLineNum));
416  prevType = nextType;
417  prevLineNum = nextLineNum;
418  }
419  else
420  {
421  // Since we appear to have two start points in a row we will ignore the second one.
422  }
423  }
424  else
425  {
426  // previous point was StopPoint.
427  if (nextType == StartStopPoint::StartStopType::StopPoint)
428  {
429  // We have two stop points in a row; will ignore
430  }
431  else
432  {
433  // We have the beginnings of a new StartStopregion.
434  prevType = nextType;
435  prevLineNum = nextLineNum;
436  }
437  }
438 
439  ++idx;
440  }
441 
442  if (prevType == StartStopPoint::StartStopType::StartPoint)
443  {
444  PushOnBack (new StartStopRegion (prevLineNum, int32_max));
445  }
446  }
447 }
448 
449 
450 
452 {
453 }
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
static const KKStr & StartStopTypeToStr(StartStopType t)
__int32 kkint32
Definition: KKBaseTypes.h:88
KKStr ToTabDelStr() const
void DeleteEntry(kkint32 _scanLineNum)
KKStr ExtractToken2(const char *delStr="\n\t\r ")
Extract first Token from the string.
Definition: KKStr.cpp:3026
kkint32 ScanLineNum() const
StartStopRegionList(const StartStopPointList &startStopPoints)
StartStopRegion(kkint32 _start, kkint32 _end)
StartStopPointPtr PrevEntry(kkint32 _scanLineNum) const
bool EqualIgnoreCase(const KKStr &s2) const
Definition: KKStr.cpp:1250
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
StartStopPointPtr SuccEntry(kkint32 _scanLineNum) const
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void ParseTabDelStr(KKStr parser)
StartStopPoint(kkint32 _scanLineNum, StartStopType _type)
Contains Classes that are specific to Cameras physical characteristics.
StartStopType Type() const
Defines a single region in a Scanner File that is to be included in a count.
static StartStopType StartStopTypeFromStr(const KKStr &s)
StartStopPointPtr AddEntry(kkint32 _scanLineNum, StartStopPoint::StartStopType _type)
kkint32 MemoryConsumedEstimated() const
kkint32 ExtractTokenInt(const char *delStr)
Definition: KKStr.cpp:3129
StartStopPointPtr NearestEntry(kkint32 _scanLineNum) const
StartStopPoint(const KKStr &s)
StartStopPointPtr AddEntry(StartStopPointPtr &_entry)
kkint32 MemoryConsumedEstimated() const
void Type(StartStopType _type)
const KKStr & TypeStr() const
StartStopPoint(const StartStopPoint &entry)
#define int32_max
Definition: KKBaseTypes.h:119
Class that keeps track of parameter details of a single scanner file.