KSquare Utilities
FeatureNumList2.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <fstream>
4 #include <string>
5 #include <iostream>
6 #include <vector>
7 #include "MemoryDebug.h"
8 using namespace std;
9 
10 
11 #include "KKBaseTypes.h"
12 #include "KKException.h"
13 #include "KKStrParser.h"
14 #include "OSservices.h"
15 #include "RunLog.h"
16 using namespace KKB;
17 
18 #include "FileDesc.h"
19 #include "FeatureNumList.h"
20 using namespace KKMLL;
21 
22 
23 FeatureNumList::FeatureNumList (const FeatureNumList& _featureNumList):
24  featureNums (NULL),
25  featureNumsAllocatedSize (0),
26  maxFeatureNum (_featureNumList.MaxFeatureNum ()),
27  numOfFeatures (0)
28 {
29  kkuint16* otherFeatureNums = _featureNumList.featureNums;
30  kkuint16 otherNumOfFeatures = _featureNumList.numOfFeatures;
31  AllocateArraySize (_featureNumList.numOfFeatures + 1);
32  for (kkuint16 x = 0; x < otherNumOfFeatures; x++)
33  AddFeature (otherFeatureNums[x]);
34 }
35 
36 
37 
38 
39 FeatureNumList::FeatureNumList (kkuint32 _maxFeatureNum):
40 
41  featureNums (NULL),
42  featureNumsAllocatedSize (0),
43  maxFeatureNum (_maxFeatureNum),
44  numOfFeatures (0)
45 {
46  AllocateArraySize (_maxFeatureNum + 1);
47 }
48 
49 
50 
51 FeatureNumList::FeatureNumList (const BitString& bitString):
52  featureNums (NULL),
53  featureNumsAllocatedSize (0),
54  maxFeatureNum (0),
55  numOfFeatures (0)
56 
57 {
58  maxFeatureNum = bitString.BitLen () - 1;
59 
60  VectorUint16 listOfSelectedFeatures;
61  bitString.ListOfSetBits16 (listOfSelectedFeatures);
62  AllocateArraySize ((kkint32)listOfSelectedFeatures.size ());
63  for (kkuint32 x = 0; x < listOfSelectedFeatures.size (); x++)
64  AddFeature (listOfSelectedFeatures[x]);
65 }
66 
67 
68 
69 
70 FeatureNumList::FeatureNumList (const KKStr& _featureListStr,
71  bool& _valid
72  ):
73 
74  featureNums (NULL),
75  featureNumsAllocatedSize (0),
76  maxFeatureNum (0),
77  numOfFeatures (0)
78 
79 {
80  FeatureNumListPtr selFeatures = ExtractFeatureNumsFromStr (_featureListStr);
81  if (selFeatures == NULL)
82  {
83  _valid = false;
84  return;
85  }
86 
87  maxFeatureNum = selFeatures->MaxFeatureNum ();
88  AllocateArraySize (selFeatures->NumOfFeatures ());
89  const kkuint16* selFeatureNums = selFeatures->FeatureNums ();
90 
91  for (kkuint16 x = 0; x < selFeatures->NumOfFeatures (); ++x)
92  AddFeature (selFeatureNums[x]);
93 
94  delete selFeatures;
95  selFeatures = NULL;
96  return;
97 }
98 
99 
100 
101 
102 
103 FeatureNumList::FeatureNumList ():
104  featureNums (NULL),
105  featureNumsAllocatedSize (0),
106  maxFeatureNum (0),
107  numOfFeatures (0)
108 {
109 }
110 
111 
112 
114 {
115  delete [] featureNums;
116 }
117 
118 
119 
121 {
122  kkint32 memoryConsumedEstimated = sizeof (FeatureNumList);
123  if (featureNums)
124  memoryConsumedEstimated += sizeof (kkuint16) * featureNumsAllocatedSize;
125 
126  return memoryConsumedEstimated;
127 } /* MemoryConsumedEstimated */
128 
129 
130 
131 void FeatureNumList::AllocateArraySize (kkuint16 size)
132 {
133  if (featureNumsAllocatedSize >= size)
134  {
135  // We have already allocated at least 'size' for featureNums.
136  return;
137  }
138 
139  if (!featureNums)
140  {
141  featureNums = new kkuint16[size];
142  featureNumsAllocatedSize = size;
143  for (kkuint16 x = 0; x < size; ++x)
144  featureNums[x] = 0;
145  }
146 
147  else
148  {
149  kkuint16* newFeatureNums = new kkuint16[size];
150 
151  kkuint16 x;
152 
153  for (x = 0; x < numOfFeatures; ++x)
154  newFeatureNums[x] = featureNums[x];
155 
156  for (x = numOfFeatures; x < size; ++x)
157  newFeatureNums[x] = 0;
158 
159  delete [] featureNums;
160  featureNums = newFeatureNums;
161  newFeatureNums = NULL;
162  featureNumsAllocatedSize = size;
163  }
164 } /* AllocateArraySize */
165 
166 
167 
168 void FeatureNumList::ToBitString (BitString& bitStr) const
169 {
170  bitStr.ReSet ();
171  kkint32 x;
172 
173  for (x = 0; x < NumOfFeatures (); x++)
174  bitStr.Set (featureNums[x]);
175 } /* ToBitString */
176 
177 
178 
180 {
181  kkuint16* newList = new kkuint16[numOfFeatures];
182  for (kkint32 x = 0; x < numOfFeatures; x++)
183  newList[x] = featureNums[x];
184  return newList;
185 } /* CreateFeatureNumArray */
186 
187 
188 
189 bool FeatureNumList::AllFeaturesSelected (FileDescPtr fileDesc) const
190 {
191  if (numOfFeatures >= (kkint32)fileDesc->NumOfFields ())
192  return true;
193  return false;
194 } /* AllFeaturesSelected */
195 
196 
197 
198 void FeatureNumList::UnSet ()
199 {
200  if (featureNums)
201  {
202  kkint32 x;
203  for (x = 0; x < featureNumsAllocatedSize; x++)
204  featureNums[x] = 0;
205  numOfFeatures = 0;
206  }
207  numOfFeatures = 0;
208 } /* UnSet */
209 
210 
211 
212 void FeatureNumList::UnSet (kkuint16 featureNum)
213 {
214  if (!featureNums)
215  return;
216 
217  kkint32 indexToDel = numOfFeatures - 1; // Starting with last index
218 
219  while ((indexToDel >= 0) && (featureNums[indexToDel] > featureNum))
220  indexToDel--;
221 
222  if (indexToDel >= 0)
223  {
224  if (featureNums[indexToDel] == featureNum)
225  {
226  // We found the index to delete.
227  kkint32 x;
228  for (x = indexToDel; x < (numOfFeatures - 1); x++)
229  featureNums[x] = featureNums[x + 1];
230  }
231 
232  numOfFeatures--;
233  featureNums[numOfFeatures] = 0;
234  }
235 } /* UnSet */
236 
237 
238 
239 void FeatureNumList::AddFeature (kkuint16 featureNum)
240 {
241  if (!featureNums)
242  {
243  featureNums = new kkuint16[10];
244  featureNumsAllocatedSize = 10;
245  }
246 
247  if (numOfFeatures >= featureNumsAllocatedSize)
248  {
249  // Expand the featureNums array
250  kkint32 newFeatureNumsAllocatedSize = numOfFeatures + 10;
251  kkuint16* newFeatureNums = new kkuint16[newFeatureNumsAllocatedSize];
252 
253  kkint32 x = 0;
254  for (x = 0; x < numOfFeatures; ++x)
255  newFeatureNums[x] = featureNums[x];
256 
257  while (x < newFeatureNumsAllocatedSize)
258  {
259  newFeatureNums[x] = 0;
260  ++x;
261  }
262 
263  delete [] featureNums;
264  featureNums = newFeatureNums;
265  featureNumsAllocatedSize = newFeatureNumsAllocatedSize;
266  }
267 
268  if (numOfFeatures == 0)
269  {
270  featureNums[0] = featureNum;
271  numOfFeatures = 1;
272  }
273 
274  else
275  {
276  kkint32 x = numOfFeatures - 1; // Setting x to end of list
277  if (featureNums[x] < featureNum)
278  {
279  // Adding feature to end of list.
280  featureNums[numOfFeatures] = featureNum;
281  numOfFeatures++;
282  }
283 
284  else if (!InList (featureNum))
285  {
286  while ((x >= 0) && (featureNums[x] > featureNum))
287  {
288  featureNums[x + 1] = featureNums[x];
289  featureNums[x] = featureNum;
290  x--;
291  }
292 
293  numOfFeatures++;
294  }
295  }
296 
297  return;
298 } /* AddFeature */
299 
300 
301 /**
302  *@details A static method that will return a instance of 'FeatureNumList' that will have all non 'IgnoreAttribute' features
303  * in '_fileDesc' selected.
304  */
305 FeatureNumList FeatureNumList::AllFeatures (FileDescPtr _fileDesc)
306 {
307  kkuint16 maxFeatureNum = _fileDesc->NumOfFields () - 1;
308  FeatureNumList features (maxFeatureNum);
309 
310  const AttributeTypeVector& attributeTypes = _fileDesc->AttributeVector ();
311  for (kkuint16 fn = 0; fn <= maxFeatureNum; ++fn)
312  {
313  if (attributeTypes[fn] != IgnoreAttribute)
314  features.AddFeature (fn);
315  }
316 
317  return features;
318 } /* AllFeatures */
319 
320 
321 
322 
323 /**
324  *@details Using 'fileDesc' as the guide as to how many features there are and which ones are to
325  * be ignored will set all features that are not 'IgnoreAttribute' to on.
326  */
327 void FeatureNumList::SetAllFeatures (FileDescPtr fileDesc)
328 {
329  for (kkuint16 x = 0; x <= maxFeatureNum; ++x)
330  {
331  if (fileDesc->Type (x) != IgnoreAttribute)
332  AddFeature (kkuint16 (x));
333  }
334  return;
335 } /* SetAllFeatures */
336 
337 
338 
339 
340 /**
341  *@brief Returns true if the FeatureNumList instance 'z' is a subset of this instance.
342  */
343 bool FeatureNumList::IsSubSet (const FeatureNumList& z)
344 {
345  bool isSubSet = true;
346 
347  kkint32 idx = 0;
348  while ((idx < z.NumSelFeatures ()) && isSubSet)
349  {
350  kkint32 fn = z[idx];
351  isSubSet = InList (fn);
352  idx++;
353  }
354  return isSubSet;
355 }
356 
357 
358 
359 bool FeatureNumList::InList (kkuint16 _featureNum) const
360 {
361  bool found = false;
362  kkint32 x = 0;
363 
364  while ((x < numOfFeatures) && (!found))
365  {
366  found = (_featureNum == featureNums[x]);
367  if (!found)
368  x++;
369  }
370 
371  return found;
372 } /* InList */
373 
374 
375 
376 bool FeatureNumList::Test (kkuint16 _featureNum) const
377 {
378  return InList (_featureNum);
379 } /* Test */
380 
381 
382 
383 kkuint16 FeatureNumList::operator[] (kkint32 _idx) const
384 {
385  if (_idx >= numOfFeatures)
386  {
387  KKStr errMsg (100);
388  errMsg << "FeatureNumList::operator[] ***ERROR*** Invalid Index[" << _idx << "] requested.";
389  cerr << endl << errMsg << endl << endl;
390  throw KKException (errMsg);
391  }
392  else
393  {
394  return featureNums[_idx];
395  }
396 }
397 
398 
399 
400 KKStr FeatureNumList::ToString () const
401 {
402  KKStr featureNumStr (numOfFeatures * 6);
403 
404  if (numOfFeatures <= 0)
405  return featureNumStr;
406 
407  kkint32 nextIdx = 0;
408 
409  while (nextIdx < numOfFeatures)
410  {
411  kkint32 startOfGroup = nextIdx;
412  kkint32 endOfGroup = nextIdx;
413 
414  while ((endOfGroup < (numOfFeatures - 1)) &&
415  (featureNums[endOfGroup] == (featureNums[endOfGroup + 1] - 1))
416  )
417  {
418  endOfGroup++;
419  }
420 
421  if ((endOfGroup - startOfGroup) < 3)
422  {
423  kkint32 x;
424  for (x = startOfGroup; x <= endOfGroup; x++)
425  {
426  if (!featureNumStr.Empty ())
427  featureNumStr << ",";
428  featureNumStr << featureNums[x];
429  }
430  }
431  else
432  {
433  if (!featureNumStr.Empty ())
434  featureNumStr << ",";
435  featureNumStr << featureNums[startOfGroup] << "-" << featureNums[endOfGroup];
436  }
437 
438  nextIdx = endOfGroup + 1;
439  }
440 
441  return featureNumStr;
442 } /* ToString */
443 
444 
445 
447 {
448  BitString bs (maxFeatureNum + 1);
449  ToBitString (bs);
450  return bs.HexStr ();
451 } /* ToHexString */
452 
453 
454 
455 KKStr FeatureNumList::ToHexString (FileDescPtr fileDesc) const
456 {
457  BitString bs (fileDesc->NumOfFields ());
458  ToBitString (bs);
459  return bs.HexStr ();
460 } /* ToHexString */
461 
462 
463 
464 
465 VectorUint16* FeatureNumList::StrToUInt16Vetor (const KKStr& s)
466 {
467  bool valid = true;
468  VectorUint16* results = new VectorUint16 ();
469 
470  KKStrParser parser (s);
471 
472  KKStr field = parser.GetNextToken (",\t \n\r");
473  while (!field.Empty ())
474  {
475  kkint32 dashPos = field.LocateCharacter ('-');
476  if (dashPos < 0)
477  {
478  kkint32 n = field.ToInt32 ();
479  if ((n < 0) || (n > uint16_max))
480  {
481  valid = false;
482  break;
483  }
484  results->push_back (n);
485  }
486  else
487  {
488  // We are looking at a range
489  kkint32 startNum = field.SubStrPart (0, dashPos - 1).ToInt32 ();
490  kkint32 endNum = field.SubStrPart (dashPos + 1).ToInt32 ();
491 
492  if ((startNum > endNum) || (startNum < 0) || (endNum > uint16_max))
493  {
494  valid = false;
495  break;
496  }
497 
498  for (kkuint16 z = startNum; z <= endNum; ++z)
499  results->push_back (z);
500  }
501  field = parser.GetNextToken (",\t \n\r");
502  }
503 
504  if (!valid)
505  {
506  delete results;
507  results = NULL;
508  }
509  else
510  {
511  sort (results->begin (), results->end ());
512  }
513 
514  return results;
515 } /* StrToUInt16Vetor */
516 
517 
518 
519 /**
520  *@brief A static method that will return a new instance 'FeatureNum2List' where the features listed in
521  * '_featureListStr' will be turned on. 'maxFeatureNum' will be set to the highest feature number listed in
522  * '_featureListStr'.
523  */
524 FeatureNumListPtr FeatureNumList::ExtractFeatureNumsFromStr (const KKStr& _featureListStr)
525 {
526  bool valid = true;
527 
528  if (_featureListStr.EqualIgnoreCase ("NONE"))
529  return new FeatureNumList (1);
530 
531  VectorUint16* list = StrToUInt16Vetor (_featureListStr);
532  if (!list)
533  {
534  delete list;
535  list= NULL;
536  return NULL;
537  }
538 
539  if (list->size () < 1)
540  {
541  delete list;
542  list = NULL;
543  return new FeatureNumList (1);
544  }
545 
546  kkuint16 firstNum = (*list)[0];
547  kkuint16 lastNum = (*list)[list->size () - 1];
548 
549  FeatureNumListPtr result = new FeatureNumList (lastNum);
550  VectorUint16::const_iterator idx;
551  for (idx = list->begin (); idx != list->end (); ++idx)
552  result->AddFeature (*idx);
553 
554  delete list;
555  list = NULL;
556  return result;
557 } /* ExtractFeatureNumsFromStr */
558 
559 
560 
561 
562 
563 
564 void FeatureNumList::Load (const KKStr& _fileName,
565  bool& _successful,
566  RunLog& _log
567  )
568 {
569  _log.Level (20) << "FeatureNumList::Load - File[" << _fileName << "]." << endl;
570 
571  delete featureNums;
572  featureNums = NULL;
573  featureNumsAllocatedSize = 0;
574  numOfFeatures = 0;
575  maxFeatureNum = 0;
576 
577  FILE* inputFile = osFOPEN (_fileName.Str (), "r");
578  if (!inputFile)
579  {
580  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** Could Not Open File[" << _fileName << "]." << endl << endl;
581  _successful = false;
582  return;
583  }
584 
585  kkuint16 mfn = 0;
586  kkuint16 nof = 0;
587  KKStr featureNumStr = "";
588 
589  kkint32 fileDescNumOfFields = 0;
590  char buff [102400];
591 
592  while (fgets (buff, sizeof (buff), inputFile))
593  {
594  KKStr line (buff);
595  KKStr fieldName = line.ExtractToken2 ("\n\t\r").Trim ();
596 
597  if (fieldName.EqualIgnoreCase ("MaxFeatureNum"))
598  mfn = (kkuint16)line.ExtractTokenUint ("\t\n\r");
599 
600  else if (fieldName.EqualIgnoreCase ("NumOfFeatures"))
601  nof = (kkuint16)line.ExtractTokenUint ("\t\n\r");
602 
603  else if (fieldName.EqualIgnoreCase ("FeatureNums"))
604  featureNumStr = (kkuint16)line.ExtractTokenUint ("\t\n\r");
605  }
606 
607  fclose (inputFile);
608 
609  if (mfn < 1)
610  {
611  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** 'maxFeatureNum' was not defined." << endl << endl;
612  _successful = false;
613  }
614 
615  if (featureNumStr.Empty ())
616  {
617  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** 'featureNums' was not specified." << endl << endl;
618  _successful = false;
619  return;
620  }
621 
622  VectorUint16* list = StrToUInt16Vetor (featureNumStr);
623  if (!list)
624  {
625  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** 'featureNums' included invalid features." << endl << endl;
626  _successful = false;
627  return;
628  }
629 
630  kkuint16 firstNum = (*list)[0];
631  kkuint16 lastNum = (*list)[list->size () - 1];
632  if (lastNum > mfn)
633  {
634  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** 'featureNums' included features[" << lastNum << "] which is larger than maxFeaureNum[" << maxFeatureNum << "]" << endl << endl;
635  _successful = false;
636  }
637 
638  else if (list->size () != nof)
639  {
640  _log.Level (-1) << endl << "FeatureNumList::Load ***ERROR*** numOfFeatures[" << nof << "] did not agree with number of features specified in FeatureNums" << endl << endl;
641  _successful = false;
642  }
643 
644  if (_successful)
645  {
646  maxFeatureNum = mfn;
647  AllocateArraySize (nof + 1);
648  VectorUint16::const_iterator idx;
649  for (idx = list->begin (); idx != list->end (); ++idx)
650  AddFeature (*idx);
651  }
652  delete list;
653  list = NULL;
654  return;
655 } /* Load */
656 
657 
658 
659 void FeatureNumList::Save (const KKStr& fileName)
660 {
661  ofstream outFile (fileName.Str ());
662  outFile << "MaxFeatureNum" << "\t" << maxFeatureNum << endl;
663  outFile << "NumOfFeatures" << "\t" << numOfFeatures << endl;
664  outFile << "FeatureNums" << "\t" << ToString () << endl;
665  outFile.close ();
666 } /* Save */
667 
668 
669 
670 
671 void FeatureNumList::SaveXML (ostream& o)
672 {
673  o << "<FeatureNumList>" << "\t"
674  << "MaxFeatureNum" << "\t" << maxFeatureNum << "\t"
675  << "NumOfFeatures" << "\t" << numOfFeatures << "\t"
676  << "FeatureNums" << "\t" << ToString ()
677  << "</FeatureNumList>";
678 } /* Save */
679 
680 
681 
683 {
684  delete featureNums;
685  featureNums = NULL;
686 
687  numOfFeatures = _features.NumOfFeatures ();
688  maxFeatureNum = _features.MaxFeatureNum ();
689  featureNums = new kkuint16[numOfFeatures];
690  featureNumsAllocatedSize = numOfFeatures;
691  const kkuint16* rightFeatureNums = _features.FeatureNums ();
692 
693  for (kkint32 x = 0; x < numOfFeatures; ++x)
694  featureNums[x] = _features[x];
695 
696  return *this;
697 } /* operator= */
698 
699 
700 
702 {
703  *this = *_features;
704  return *this;
705 }
706 
707 
708 
709 kkint32 FeatureNumList::Compare (const FeatureNumList& _features) const
710 {
711  kkint32 x = 0;
712 
713  while ((x < numOfFeatures) && (x < _features.NumOfFeatures ()))
714  {
715  if (featureNums[x] < _features.featureNums[x])
716  return -1;
717 
718  else if (featureNums[x] > _features.featureNums[x])
719  return 1;
720 
721  x++;
722  }
723 
724  if (x < numOfFeatures)
725  return 1;
726 
727  else if (x < _features.numOfFeatures)
728  return -1;
729 
730  return 0;
731 } /* Compare */
732 
733 
734 
735 /** @brief Indicates if the two FeatureNumList instances have the same features selected. */
736 bool FeatureNumList::operator== (const FeatureNumList& _features) const
737 {
738  if (numOfFeatures != _features.numOfFeatures)
739  {
740  // No point even comparing the list of feature numbers, if the lengths are
741  // different, then they can not be equal.
742  return false;
743  }
744 
745  return Compare (_features) == 0;
746 } /* operator== */
747 
748 
749 
750 /**
751  * @brief Indicates if the Left FeatureNumLiost instances is less than the right one.
752  * @see Compare
753  */
754 bool FeatureNumList::operator< (const FeatureNumList& _features) const
755 {
756  return Compare (_features) < 0;
757 } /* operator< */
758 
759 
760 
761 /**
762  * @brief Indicates if the Left FeatureNumLiost instances is greater than the right one.
763  * @see Compare
764  */
765 bool FeatureNumList::operator> (const FeatureNumList& _features) const
766 {
767  return Compare (_features) > 0;
768 } /* operator> */
769 
770 
771 
772 namespace KKMLL
773 {
774  ostream& operator<< ( ostream& os,
775  const FeatureNumList& features
776  )
777  {
778  os << features.ToString ();
779  return os;
780  }
781 
782 
783  ostream& operator<< ( ostream& os,
784  const FeatureNumListPtr& features
785  )
786  {
787  os << features->ToString ();
788  return os;
789  }
790 }
791 
792 
793 
794 /**
795  *@brief Returns the intersection of the two FeatureNumList instances.
796  *@details Will return a new FeatureNumList instance that will consist of a list of features that are selected in both the left and right FeatureNumList
797  * instances.
798  */
799 FeatureNumList FeatureNumList::operator* (const FeatureNumList& rightSide) const
800 {
801  kkuint16 mfn = Max (maxFeatureNum, rightSide.MaxFeatureNum ());
802  kkuint16 bestCaseNof = Min (numOfFeatures, rightSide.NumOfFeatures ());
803  FeatureNumList result (mfn);
804  result.AllocateArraySize (bestCaseNof);
805 
806  kkint32 l = 0;
807  kkint32 r = 0;
808 
809  while ((l < numOfFeatures) && (r < rightSide.numOfFeatures))
810  {
811  if (featureNums[l] < rightSide.featureNums[r])
812  l++;
813 
814  else if (featureNums[l] > rightSide.featureNums[r])
815  r++;
816 
817  else
818  {
819  result.AddFeature (featureNums[l]);
820  l++;
821  r++;
822  }
823  }
824 
825  return result;
826 } /* operator* */
827 
828 
829 
830 
831 /**
832  * @brief Performs a logical 'OR' operation on the two FeatureNumList instances.
833  * @details Will return a new FeatureNumList instance that will consist of a list of features that are in either
834  * left and right FeatureNumList instances. Both FeatureNumList objects must be referencing the same FileDesc instance
835  * otherwise an exception will be thrown.
836  */
837 FeatureNumList FeatureNumList::operator+ (const FeatureNumList& rightSide) const
838 {
839  FeatureNumList result (rightSide);
840 
841  kkint32 l = 0;
842  for (l = 0; l < numOfFeatures; l++)
843  result.AddFeature (featureNums[l]);
844 
845  return result;
846 } /* operator+ */
847 
848 
849 
850 /**
851  * @brief Adds the features that are selected in the right FeatureNumList instance to the left instance.
852  * @details Both FeatureNumList objects must be referencing the same FileDesc instance otherwise an exception will be thrown.
853  */
855 {
856  const kkuint16* rightFeatureNums = rightSide.FeatureNums ();
857  kkuint16 rightNumOfFeatures = rightSide.NumOfFeatures ();
858 
859  for (kkuint16 x = 0; x < rightNumOfFeatures; ++x)
860  AddFeature (rightFeatureNums[x]);
861 
862  return *this;
863 } /* operator+= */
864 
865 
866 
867 
868 /** @brief Adds the feature 'featureNum' to the selected list of features. */
870 {
871  AddFeature (featureNum);
872  return *this;
873 } /* operator+= */
874 
875 
876 
877 
878 
879 /** @brief Returns a new FeatureNumList instance that will consists of the left FeatureNumList instance with 'rightSide' feature added in. */
881 {
882  FeatureNumList result (*this);
883  result.AddFeature (rightSide);
884  return result;
885 } /* operator+ */
886 
887 
888 
889 
890 /** @brief Returns a new FeatureNumList instance that will consists of the left FeatureNumList instance with 'rightSide' removed from it. */
892 {
893  FeatureNumList result (*this);
894  result.UnSet (rightSide);
895  return result;
896 } /* operator- */
897 
898 
899 
900 /**
901  * @brief Returns a new FeatureNumList instance that consists of the left side instance with the
902  * selected features in the right side removed.
903  * @details Both FeatureNumList objects must be referencing the same FileDesc instance otherwise
904  * an exception will be thrown.
905  */
906 FeatureNumList FeatureNumList::operator- (const FeatureNumList& rightSide) const
907 {
908  FeatureNumList result (maxFeatureNum);
909 
910  kkint32 l = 0;
911  kkint32 r = 0;
912 
913  while ((l < numOfFeatures) && (r < rightSide.numOfFeatures))
914  {
915  if (featureNums[l] < rightSide.featureNums[r])
916  {
917  result.AddFeature (featureNums[l]);
918  l++;
919  }
920 
921  else if (featureNums[l] > rightSide.featureNums[r])
922  r++;
923 
924  else
925  {
926  l++;
927  r++;
928  }
929  }
930 
931  return result;
932 } /* operator- */
933 
934 
935 
936 
937 /** @brief removes the feature specified on the right side from the FeatureNumList on the left side. */
939 {
940  UnSet (rightSide);
941  return *this;
942 } /* operator-= */
943 
944 
945 
947 {
948  if (numToKeep > numOfFeatures)
949  {
950  cerr << endl
951  << endl
952  << "FeatureNumList::RandomlySelectFeatures *** ERROR ***" << endl
953  << endl
954  << "NumToKeep[" << numToKeep << "] Is greater than NumOfFeatures[" << numOfFeatures << "]" << endl
955  << endl
956  << endl;
957  numToKeep = numOfFeatures;
958  }
959 
960  FeatureNumListPtr randomlySelectedFeatures = new FeatureNumList (maxFeatureNum);
961 
962  kkint32 x, y, z;
963 
964  // Initialize Selected Features to the currently selected features in featureNums
965  kkuint16* selectedFeatures = new kkuint16[numOfFeatures];
966  for (x = 0; x < numOfFeatures; x++)
967  selectedFeatures[x] = featureNums[x];
968 
969  // Randomize the order of the selected featured
970  //for (x = 0; x < numOfFeatures; x++)
971  for (x = 0; x < numToKeep; x++)
972  {
973  y = LRand48() % numOfFeatures;
974  z = selectedFeatures[x];
975  selectedFeatures[x] = selectedFeatures[y];
976  selectedFeatures[y] = z;
977  }
978 
979  // Assign the first 'numToKeep' featured from the random order of selected features
980  for (x = 0; x < numToKeep; x++)
981  randomlySelectedFeatures->AddFeature (selectedFeatures[x]);
982 
983  delete [] selectedFeatures;
984 
985  return randomlySelectedFeatures;
986 } /* RandomlySelectFeatures */
987 
988 
989 
990 
992 {
993  FeatureNumList result (maxFeatureNum);
994  kkuint16 x = 0;
995  kkuint16 fni = 0;
996  while (fni < numOfFeatures)
997  {
998  while (x < featureNums[fni])
999  {
1000  result.AddFeature (x);
1001  ++x;
1002  }
1003  ++fni;
1004  }
1005 
1006  while (x < maxFeatureNum)
1007  {
1008  result.AddFeature (x);
1009  ++x;
1010  }
1011  return result;
1012 } /* Complement */
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
void Save(const KKStr &fileName)
bool EqualIgnoreCase(const char *s2) const
Definition: KKStr.cpp:1257
__int32 kkint32
Definition: KKBaseTypes.h:88
FeatureNumListPtr RandomlySelectFeatures(kkint32 numToKeep) const
Generates a new FeatureNumList object that will select at random &#39;numToKeep&#39; features from this insta...
FeatureNumList operator+(kkuint16 rightSide) const
Returns new FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
kkuint32 NumOfFields() const
Definition: FileDesc.h:197
FeatureNumList operator+(const FeatureNumList &rightSide) const
Returns new FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
kkint32 LRand48()
A implementations of the Unix version of rand48 returning a 32 bit integer.
Definition: KKBaseTypes.cpp:36
bool Test(kkuint16 _featureNum) const
Indicates whether feature &#39;_featureNum&#39; is selected.
Keeps track of selected features.
KKStr GetNextToken(const char *delStr="\n\t\r ")
Extract next Token from string, tokens will be separated by delimiter characters. ...
KKStr HexStr() const
Returns a Hex-String representation.
Definition: BitString.cpp:361
void ReSet()
Set all bits to &#39;0&#39;.
Definition: BitString.cpp:207
unsigned __int16 kkuint16
16 bit unsigned integer.
Definition: KKBaseTypes.h:86
FeatureNumList & operator=(const FeatureNumListPtr _features)
bool InList(kkuint16 featureNum) const
returns true if &#39;_featureNum&#39; is one of the selected features.
KKStr ToHexString() const
const AttributeTypeVector & AttributeVector() const
Definition: FileDesc.h:115
FeatureNumList & operator+=(kkuint16 featureNum)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
void SaveXML(std::ostream &o)
FeatureNumList(const BitString &bitString)
Constructs a &#39;FeatureNumList&#39; instance using the set bits in &#39;bitString&#39; to indicate which features a...
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
kkuint16 * CreateFeatureNumArray() const
Allocates a array of kkint32&#39;s that is a copy of FeatureNums. The caller will own the array and is re...
FeatureNumList operator-(const FeatureNumList &rightSide) const
KKStr & operator=(KKStr &&src)
Definition: KKStr.cpp:1369
Allows you to manage very long bit strings.
Definition: BitString.h:31
FeatureNumList Complement() const
Perform a complement of selected features. That is if a feature is selected turn it off and if it is ...
KKTHread * KKTHreadPtr
kkuint16 operator[](kkint32 idx) const
Returns back the selected feature.
KKStrParser(const KKStr &_str)
Definition: KKStrParser.cpp:42
bool IsSubSet(const FeatureNumList &z)
Returns true if &#39;z&#39; is a subset of this instance.
FeatureNumList * FeatureNumListPtr
void UnSet()
Turns off all features so that no feature is selected.
kkint32 MaxFeatureNum() const
void ToBitString(BitString &bitStr) const
kkint32 NumOfFeatures() const
void AddFeature(kkuint16 featureNum)
Adds &#39;featureNum&#39; to the list of selected features. If it is already selected nothing happens...
void UnSet(kkuint16 featureNum)
Turns off specified feature &#39;featureNum&#39;; if &#39;featureNum&#39; is not turned on then nothing happens; same...
kkuint32 BitLen() const
Returns the length of the bit-string.
Definition: BitString.h:64
bool Empty() const
Definition: KKStr.h:241
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; and ending at &#39;lastInd...
Definition: KKStr.cpp:2802
#define uint16_max
Definition: KKBaseTypes.h:118
void SetAllFeatures(FileDescPtr fileDesc)
Selects all features except those flagged as &#39;IgnoreAttribute&#39; in the associated FileDesc.
kkint32 ToInt32() const
Definition: KKStr.cpp:3587
KKStr ToHexString(FileDescPtr fileDesc) const
Uses &#39;fileDesc&#39; to determine length of hex string.
bool operator>(const FeatureNumList &_features) const
Indicates if the Left FeatureNumList instances is greater than the right one.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void ListOfSetBits16(VectorUint16 &setBits) const
Definition: BitString.cpp:266
kkint32 LocateCharacter(char ch) const
Returns index of 1st occurrence of &#39;ch&#39; otherwise -1.
Definition: KKStr.cpp:2021
kkint32 Compare(const FeatureNumList &_features) const
Compare with another featureNumList returning -1, 0, and 1 indicating less_than, equal, or greater_than.
FeatureNumList operator-(kkuint16 rightSide) const
void Set(kkuint32 bitNum)
Set the bit indicated by &#39;bitNum&#39; to &#39;1&#39;.
Definition: BitString.cpp:182
FeatureNumList & operator=(const FeatureNumList &_features)
static FeatureNumList AllFeatures(FileDescPtr fileDesc)
Create a FeatureNumList object where all features are selected, except ones that are flagged as Ignor...
void Load(const KKStr &_fileName, bool &_successful, RunLog &_log)
FeatureNumList(kkuint32 _maxFeatureNum)
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
KKStr ToString() const
Returns comma delimited list of all features selected; will make use of range specification.
kkint32 NumSelFeatures() const
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
bool AllFeaturesSelected(FileDescPtr fileDesc) const
Returns true if all features are selected.
FeatureNumList(const KKStr &_featureListStr, bool &_valid)
Constructs a &#39;FeatureNumList&#39; instance from a string that contains a list of selected features...
FeatureNumList * FeatureNumListPtr
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
FeatureNumList operator*(const FeatureNumList &rightSide) const
Returns new instance that is the intersection of features.
bool operator==(const FeatureNumList &_features) const
Indicates if the two FeatureNumLiost instances have the same features selected.
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
BitString(kkuint32 _bitLen)
Construct a bit string of length _binLen with all bits set to &#39;0&#39;.
Definition: BitString.cpp:68
Class that manages the extraction of tokens from a String without being destructive to the original s...
Definition: KKStrParser.h:18
KKException(const KKStr &_exceptionStr)
Definition: KKException.cpp:45
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
Definition: KKBaseTypes.h:141
FeatureNumList & operator+=(const FeatureNumList &rightSide)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
KKStr SubStrPart(kkint32 firstChar) const
returns a SubString consisting of all characters starting at index &#39;firstChar&#39; until the end of the s...
Definition: KKStr.cpp:2780
kkint32 MemoryConsumedEstimated() const
bool operator<(const FeatureNumList &_features) const
Indicates if the Left FeatureNumList instances is less than the right one.
const kkuint16 * FeatureNums() const
FeatureNumList & operator-=(kkuint16 rightSide)