KSquare Utilities
FeatureNumList_Old.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 "OSservices.h"
14 #include "RunLog.h"
15 using namespace KKB;
16 
17 
18 #include "FeatureNumList.h"
19 #include "FileDesc.h"
20 using namespace KKMLL;
21 
22 
24 
25  featureNums (NULL),
26  featureNumsAllocatedSize (0),
28  numOfFeatures (0)
29 {
30  if (!fileDesc)
31  {
32  cerr << endl
33  << "FeatureNumList::FeatureNumList *** ERROR *** fileDesc = NULL" << endl
34  << endl;
35  osWaitForEnter ();
36  }
37 
38  AllocateArraySize (fileDesc->NumOfFields ());
39 }
40 
41 
42 
44  const BitString& bitString
45  ):
46  featureNums (NULL),
47  featureNumsAllocatedSize (0),
49  numOfFeatures (0)
50 
51 {
52  if (!fileDesc)
53  {
54  cerr << endl
55  << "FeatureNumList *** ERROR *** No 'FileDesc' object provided." << endl
56  << endl;
57  osWaitForEnter ();
58  exit (-1);
59  }
60 
61  if (fileDesc->NumOfFields () != kkint32 (bitString.BitLen ()))
62  {
63  cerr << endl
64  << "FeatureNumList *** ERROR ***" << endl
65  << endl
66  << " BitString.Len[" << bitString.BitLen () << "] is different than FileDesc.NumOfFields[" << fileDesc->NumOfFields () << "]" << endl
67  << endl;
68  osWaitForEnter ();
69  exit (-1);
70  }
71 
72  VectorUint16 listOfBits;
73  bitString.ListOfSetBits16 (listOfBits);
74 
75  if (kkuint32 (featureNumsAllocatedSize) < listOfBits.size ())
76  AllocateArraySize ((kkint32)listOfBits.size ());
77 
78  for (kkuint32 x = 0; x < listOfBits.size (); x++)
79  AddFeature (listOfBits[x]);
80 }
81 
82 
83 
84 FeatureNumList::FeatureNumList (const FeatureNumList& _featureNumList):
85  featureNums (NULL),
86  featureNumsAllocatedSize (0),
88  numOfFeatures (_featureNumList.NumOfFeatures ())
89 {
90  if (fileDesc == NULL)
91  {
92  cerr << endl
93  << "FeatureNumList::FeatureNumList (const FeatureNumList& _featureNumList)" << endl
94  << " fileDesc == NULL" << endl
95  << endl;
96  osWaitForEnter ();
97  exit (-1);
98  }
99 
100  if ((numOfFeatures < 0) || (numOfFeatures > (kkint32)fileDesc->NumOfFields ()))
101  {
102  cerr << endl
103  << "FeatureNumList::FeatureNumList (const FeatureNumList& _featureNumList)" << endl
104  << " numOfFeatures[" << numOfFeatures << "] is out of range of fileDesc" << endl
105  << endl;
106  osWaitForEnter ();
107  exit (-1);
108  }
109 
110  featureNums = new kkuint16[numOfFeatures + 1];
111  featureNumsAllocatedSize = numOfFeatures + 1;
112  for (kkint32 x = 0; x < numOfFeatures; x++)
113  featureNums[x] = _featureNumList[x];
114 }
115 
116 
117 
119  const KKStr& _featureListStr,
120  bool& _valid
121  ):
122 
123  featureNums (NULL),
124  featureNumsAllocatedSize (0),
126  numOfFeatures (0)
127 
128 {
129  if (!fileDesc)
130  {
131  cerr << endl
132  << endl
133  << "FeatureNumList::FeatureNumList *** ERROR *** fileDesc = NULL" << endl
134  << endl;
135  osWaitForEnter ();
136  }
137 
138  AllocateArraySize (fileDesc->NumOfFields ());
139 
140  ExtractFeatureNumsFromStr (_featureListStr, _valid);
141 }
142 
143 
144 
145 FeatureNumList::FeatureNumList (FileDescPtr _fileDesc,
146  FeatureSelectionType _selectionType,
147  const KKStr& _featureListStr,
148  bool& _valid
149  ):
150  featureNums (NULL),
151  featureNumsAllocatedSize (0),
152  fileDesc (_fileDesc),
153  numOfFeatures (0)
154 
155 {
156  if (!fileDesc)
157  {
158  cerr << endl
159  << endl
160  << "FeatureNumList::FeatureNumList *** ERROR *** fileDesc = NULL" << endl
161  << endl;
162  osWaitForEnter ();
163  }
164 
165  AllocateArraySize (fileDesc->NumOfFields ());
166 
167  switch (_selectionType)
168  {
169  case IncludeFeatureNums:
170  ExtractFeatureNumsFromStr (_featureListStr, _valid);
171  break;
172 
173 
174  case ExcludeFeatureNums:
175  FeatureNumList excludedFeatures (fileDesc, _featureListStr, _valid);
176 
177  numOfFeatures = fileDesc->NumOfFields () - excludedFeatures.NumOfFeatures ();
178  featureNums = new kkuint16[numOfFeatures];
179  featureNumsAllocatedSize = numOfFeatures;
180 
181  kkint32 y = 0;
182  for (kkuint16 x = 0; x < fileDesc->NumOfFields (); x++)
183  {
184  if (!excludedFeatures.InList (x))
185  {
186  featureNums[y] = x;
187  y++;
188  }
189  }
190  break;
191 
192  } /* End Of Switch */
193 }
194 
195 
196 
197 FeatureNumList::FeatureNumList ():
198  featureNums (NULL),
199  featureNumsAllocatedSize (0),
200  fileDesc (NULL),
201  numOfFeatures (0)
202 {
203 }
204 
205 
206 
208 {
209  delete [] featureNums;
210 }
211 
212 
213 
215 {
216  kkint32 memoryConsumedEstimated = sizeof (FeatureNumList);
217  if (featureNums)
218  memoryConsumedEstimated += sizeof (kkuint16) * featureNumsAllocatedSize;
219 
220  return memoryConsumedEstimated;
221 } /* MemoryConsumedEstimated */
222 
223 
224 
225 void FeatureNumList::AllocateArraySize (kkint32 size)
226 {
227  if (featureNumsAllocatedSize >= size)
228  {
229  // We have already allocated at least 'size' for featureNums.
230  return;
231  }
232 
233  if (!featureNums)
234  {
235  featureNums = new kkuint16[size];
236  featureNumsAllocatedSize = size;
237  for (kkint32 x = 0; x < size; x++)
238  featureNums[x] = 0;
239  }
240 
241  else
242  {
243  kkuint16* newFeatureNums = new kkuint16[size];
244 
245  kkint32 x;
246 
247  for (x = 0; x < numOfFeatures; x++)
248  newFeatureNums[x] = featureNums[x];
249 
250  for (x = numOfFeatures; x < size; x++)
251  newFeatureNums[x] = 0;
252 
253 
254  delete [] featureNums;
255  featureNums = newFeatureNums;
256  featureNumsAllocatedSize = size;
257  }
258 } /* AllocateArraySize */
259 
260 
261 
262 void FeatureNumList::ToBitString (BitString& bitStr) const
263 {
264  bitStr.ReSet ();
265  kkint32 x;
266 
267  for (x = 0; x < NumOfFeatures (); x++)
268  bitStr.Set (featureNums[x]);
269 } /* ToBitString */
270 
271 
272 
274 {
275  kkuint16* newList = new kkuint16[numOfFeatures];
276  for (kkint32 x = 0; x < numOfFeatures; x++)
277  newList[x] = featureNums[x];
278  return newList;
279 } /* CreateFeatureNumArray */
280 
281 
282 
284 {
285  if (numOfFeatures >= (kkint32)fileDesc->NumOfFields ())
286  return true;
287  return false;
288 } /* AllFeaturesSelected */
289 
290 
291 
292 void FeatureNumList::UnSet ()
293 {
294  if (featureNums)
295  {
296  kkint32 x;
297  for (x = 0; x < featureNumsAllocatedSize; x++)
298  featureNums[x] = 0;
299  numOfFeatures = 0;
300  }
301  numOfFeatures = 0;
302 } /* UnSet */
303 
304 
305 
306 void FeatureNumList::UnSet (kkuint16 featureNum)
307 {
308  if (!featureNums)
309  return;
310 
311  kkint32 indexToDel = numOfFeatures - 1; // Starting with last index
312 
313  while ((indexToDel >= 0) && (featureNums[indexToDel] > featureNum))
314  indexToDel--;
315 
316  if (indexToDel >= 0)
317  {
318  if (featureNums[indexToDel] == featureNum)
319  {
320  // We found the index to delete.
321  kkint32 x;
322  for (x = indexToDel; x < (numOfFeatures - 1); x++)
323  featureNums[x] = featureNums[x + 1];
324  }
325 
326  numOfFeatures--;
327  featureNums[numOfFeatures] = 0;
328  }
329 } /* UnSet */
330 
331 
332 
333 void FeatureNumList::AddFeature (kkuint16 featureNum)
334 {
335  if (!featureNums)
336  {
337  featureNums = new kkuint16[10];
338  featureNumsAllocatedSize = 10;
339  }
340 
341  if (featureNum >= fileDesc->NumOfFields ())
342  {
343  KKStr errMsg = "FeatureNumList::AddFeature ***ERROR*** exceeded MaxNumOfFeatures.";
344  errMsg << endl
345  << " FeastureNum[" << featureNum << "] MaxNumOfFeatures[" << fileDesc->NumOfFields () << "]";
346 
347  cerr << std::endl << errMsg << endl << endl;
348  throw KKException (errMsg);
349  }
350 
351  else if (numOfFeatures >= featureNumsAllocatedSize)
352  {
353  // Expand the featureNums array
354  kkint32 newNumOfFeatures = numOfFeatures + 10;
355  kkuint16* newFeatureNums = new kkuint16[newNumOfFeatures];
356 
357  for (kkint32 x = 0; x < numOfFeatures; x++)
358  newFeatureNums[x] = featureNums[x];
359 
360  delete [] featureNums;
361  featureNums = newFeatureNums;
362  featureNumsAllocatedSize = newNumOfFeatures;
363  }
364 
365  if (numOfFeatures == 0)
366  {
367  featureNums[0] = featureNum;
368  numOfFeatures = 1;
369  }
370 
371  else
372  {
373  kkint32 x = numOfFeatures - 1; // Setting x to end of list
374  if (featureNums[x] < featureNum)
375  {
376  // Adding feature to end of list.
377  featureNums[numOfFeatures] = featureNum;
378  numOfFeatures++;
379  }
380 
381  else if (!InList (featureNum))
382  {
383  while ((x >= 0) && (featureNums[x] > featureNum))
384  {
385  featureNums[x + 1] = featureNums[x];
386  featureNums[x] = featureNum;
387  x--;
388  }
389 
390  numOfFeatures++;
391  }
392  }
393 
394  return;
395 } /* AddFeature */
396 
397 
398 
400 {
401  bool valid;
402  FeatureNumList features (_fileDesc, "All", valid);
403  return features;
404 } /* AllFeatures */
405 
406 
407 
409 {
410  for (kkuint16 x = 0; x < fileDesc->NumOfFields (); ++x)
411  {
412  if (fileDesc->Type (x) != IgnoreAttribute)
413  AddFeature (kkuint16 (x));
414  }
415  return;
416 } /* SetAllFeatures */
417 
418 
419 /**
420  *brief Returns true if the FeatureNumList 'z' is a subset of myself.
421  */
422 bool FeatureNumList::IsSubSet (const FeatureNumList& z)
423 {
424  bool isSubSet = true;
425 
426  kkint32 idx = 0;
427  while ((idx < z.NumSelFeatures ()) && isSubSet)
428  {
429  kkint32 fn = z[idx];
430  isSubSet = InList (fn);
431  idx++;
432  }
433  return isSubSet;
434 }
435 
436 
437 
438 bool FeatureNumList::InList (kkuint16 _featureNum) const
439 {
440  bool found = false;
441  kkint32 x = 0;
442 
443  while ((x < numOfFeatures) && (!found))
444  {
445  found = (_featureNum == featureNums[x]);
446  if (!found)
447  x++;
448  }
449 
450  return found;
451 } /* InList */
452 
453 
454 
455 bool FeatureNumList::Test (kkuint16 _featureNum) const
456 {
457  return InList (_featureNum);
458 } /* Test */
459 
460 
461 
462 kkuint16 FeatureNumList::operator[] (kkint32 _idx) const
463 {
464  if (_idx >= numOfFeatures)
465  {
466  cerr << endl << endl << endl
467  << "FeatureNumList::operator[] Invalid Index[" << _idx << "] requested." << endl
468  << endl;
470  exit (-1);
471  }
472  else
473  {
474  return featureNums[_idx];
475  }
476 }
477 
478 
479 
480 KKStr FeatureNumList::ToString () const
481 {
482  KKStr featureNumStr (numOfFeatures * 6);
483 
484  if (numOfFeatures <= 0)
485  return featureNumStr;
486 
487  kkint32 nextIdx = 0;
488 
489  while (nextIdx < numOfFeatures)
490  {
491  kkint32 startOfGroup = nextIdx;
492  kkint32 endOfGroup = nextIdx;
493 
494  while ((endOfGroup < (numOfFeatures - 1)) &&
495  (featureNums[endOfGroup] == (featureNums[endOfGroup + 1] - 1))
496  )
497  {
498  endOfGroup++;
499  }
500 
501  if ((endOfGroup - startOfGroup) < 3)
502  {
503  kkint32 x;
504  for (x = startOfGroup; x <= endOfGroup; x++)
505  {
506  if (!featureNumStr.Empty ())
507  featureNumStr << ",";
508  featureNumStr << featureNums[x];
509  }
510  }
511  else
512  {
513  if (!featureNumStr.Empty ())
514  featureNumStr << ",";
515  featureNumStr << featureNums[startOfGroup] << "-" << featureNums[endOfGroup];
516  }
517 
518  nextIdx = endOfGroup + 1;
519  }
520 
521  return featureNumStr;
522 } /* ToString */
523 
524 
525 
526 
528 {
529  BitString bs (fileDesc->NumOfFields ());
530  ToBitString (bs);
531  return bs.HexStr ();
532 } /* ToHexString */
533 
534 
535 
537  bool& _valid
538  )
539 {
540  _valid = true;
541 
542  KKStr field;
543  kkuint16 featureNum;
544 
545  _featureListStr.Upper ();
546 
547  numOfFeatures = 0;
548 
549  if (_featureListStr == "NONE")
550  {
551  return;
552  }
553  if (_featureListStr == "ALL")
554  {
556  return;
557  }
558 
559  field = _featureListStr.ExtractToken (", \n\r\t");
560  while (!field.Empty ())
561  {
562  _featureListStr.TrimLeft ();
563 
564  kkint32 dashPos = field.LocateCharacter ('-');
565  if (dashPos < 0)
566  {
567  // This is not a range
568 
569  featureNum = kkuint16 (atoi (field.Str ()));
570  //if (field.ValidInt (featureNum))
571  {
572  if (featureNum >= fileDesc->NumOfFields ())
573  {
574  _valid = false;
575  return;
576  }
577  else
578  {
579  bool alreadyInList = InList (featureNum);
580  if (!alreadyInList)
581  AddFeature (featureNum);
582  }
583  }
584  }
585  else
586  {
587  // We are looking at a range
588  kkuint16 startFeatureNum = kkuint16 (field.ExtractTokenInt (" -"));
589  kkuint16 endFeatureNum = kkuint16 (field.ExtractTokenInt (" -"));
590 
591  if ((startFeatureNum >= fileDesc->NumOfFields ()) ||
592  (endFeatureNum >= fileDesc->NumOfFields ()) ||
593  (startFeatureNum > endFeatureNum)
594  )
595  {
596  _valid = false;
597  }
598  else
599  {
600  for (featureNum = startFeatureNum; featureNum <= endFeatureNum; featureNum++)
601  {
602  bool alreadyInList = InList (featureNum);
603  if (!alreadyInList)
604  AddFeature (featureNum);
605  }
606  }
607  }
608  field = _featureListStr.ExtractToken (", \n\r\t");
609  }
610 } /* ExtractFeatureNumsFromStr */
611 
612 
613 
614 void FeatureNumList::Load (const KKStr& _fileName,
615  bool& _successful,
616  RunLog& _log
617  )
618 {
619  _log.Level (20) << "FeatureNumList::Load - File["
620  << _fileName << "]."
621  << endl;
622 
623  FILE* inputFile = osFOPEN (_fileName.Str (), "r");
624  if (!inputFile)
625  {
626  _log.Level (-1) << "FeatureNumList::Load *** ERROR ***" << endl;
627  _log.Level (-1) << " Could Not Open File[" << _fileName << "]." << endl;
628  _successful = false;
629  }
630 
631  char buff [102400];
632 
633  if (fgets (buff, sizeof (buff), inputFile))
634  {
635  KKStr firstLine (buff);
636  kkint32 fileDescNumOfFields = atoi (firstLine.Str ());
637  if (fileDesc->NumOfFields () != fileDescNumOfFields)
638  {
639  _log.Level (-1) << endl
640  << "FeatureNumList::Load *** ERROR ***" << endl
641  << " Mismatch in field count" << endl
642  << " FileDesc->NumOfFields[" << fileDesc->NumOfFields () << "]" << endl
643  << " From File [" << fileDescNumOfFields << "]" << endl
644  << endl;
645  _successful = false;
646  fclose (inputFile);
647  return;
648  }
649  }
650  else
651  {
652  _log.Level (-1) << endl
653  << "FeatureNumList::Load *** ERROR ***" << endl
654  << " Missing Data from File" << endl
655  << endl;
656  _successful = false;
657  fclose (inputFile);
658  return;
659  }
660 
661  if (fgets (buff, sizeof (buff), inputFile))
662  {
663  _log.Level (50) << "Load - FeatureList = [" << buff << "]." << endl;
664  bool valid;
665  ExtractFeatureNumsFromStr (buff, valid);
666  if (!valid)
667  _successful = false;
668  }
669  else
670  {
671  _log.Level (-1) << "FeatureNumList::Load *** ERROR ***" << endl;
672  _log.Level (-1) << " No Data in File[" << _fileName << "]." << endl;
673  _successful = false;
674  }
675 
676  fclose (inputFile);
677  _successful = true;
678 } /* Load */
679 
680 
681 
682 void FeatureNumList::Save (const KKStr& _fileName,
683  bool& _successful,
684  RunLog& _log
685  )
686 {
687  _log.Level (20) << "FeatureNumList::Save - File["
688  << _fileName << "]."
689  << endl;
690 
691  _successful = true;
692 
693  ofstream outFile (_fileName.Str ());
694 
695  outFile << fileDesc->NumOfFields () << endl;
696  outFile << ToString () << endl;
697 
698  outFile.close ();
699 } /* Save */
700 
701 
702 
703 
704 void FeatureNumList::Save (ostream& o)
705 {
706  o << "<FeatureNumList>" << "\t"
707  << "NumOfFeatures" << "\t" << numOfFeatures
708  << "FeatureNums" << "\t" << ToString ()
709  << "</FeatureNumList>";
710 } /* Save */
711 
712 
713 
715 {
716  if (featureNums)
717  delete [] featureNums;
718 
719  fileDesc = _features.fileDesc;
720  numOfFeatures = _features.NumOfFeatures ();
721 
722  featureNums = new kkuint16[numOfFeatures];
723  featureNumsAllocatedSize = numOfFeatures;
724 
725  for (kkint32 fn = 0; fn < numOfFeatures; fn++)
726  featureNums[fn] = _features[fn];
727 
728  return *this;
729 } /* operator= */
730 
731 
732 
734 {
735  *this = *_features;
736  return *this;
737 }
738 
739 
740 
741 kkint32 FeatureNumList::Compare (const FeatureNumList& _features) const
742 {
743  kkint32 x = 0;
744 
745  while ((x < numOfFeatures) && (x < _features.NumOfFeatures ()))
746  {
747  if (featureNums[x] < _features.featureNums[x])
748  return -1;
749 
750  else if (featureNums[x] > _features.featureNums[x])
751  return 1;
752 
753  x++;
754  }
755 
756  if (x < numOfFeatures)
757  return 1;
758 
759  else if (x < _features.numOfFeatures)
760  return -1;
761 
762  return 0;
763 } /* Compare */
764 
765 
766 
767 /** @brief Indicates if the two FeatureNumList instances have the same features selected. */
768 bool FeatureNumList::operator== (const FeatureNumList& _features) const
769 {
770  if (numOfFeatures != _features.numOfFeatures)
771  {
772  // No point even comparing the list of feature numbers, if the lengths are
773  // different, then they can not be equal.
774  return false;
775  }
776 
777  return Compare (_features) == 0;
778 } /* operator== */
779 
780 
781 
782 /**
783  * @brief Indicates if the Left FeatureNumLiost instances is less than the right one.
784  * @see Compare
785  */
786 bool FeatureNumList::operator< (const FeatureNumList& _features) const
787 {
788  return Compare (_features) < 0;
789 } /* operator< */
790 
791 
792 
793 /**
794  * @brief Indicates if the Left FeatureNumLiost instances is greater than the right one.
795  * @see Compare
796  */
797 bool FeatureNumList::operator> (const FeatureNumList& _features) const
798 {
799  return Compare (_features) > 0;
800 } /* operator> */
801 
802 
803 
804 namespace KKMLL
805 {
806  ostream& operator<< ( ostream& os,
807  const FeatureNumList& features
808  )
809  {
810  os << features.ToString ();
811  return os;
812  }
813 
814 
815  ostream& operator<< ( ostream& os,
816  const FeatureNumListPtr& features
817  )
818  {
819  os << features->ToString ();
820  return os;
821  }
822 }
823 
824 
825 
826 /**
827  *@brief Performs a logical 'AND' operation on the two FeatureNumList instances.
828  *@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
829  * instances. Both FeatureNumList objects must be referencing the same FileDesc instance otherwise an exception will be thrown.
830  */
831 FeatureNumList FeatureNumList::operator* (const FeatureNumList& rightSide) const
832 {
833  if (fileDesc != rightSide.FileDesc ())
834  {
835  KKStr errMsg = "FeatureNumList::operator* ***ERROR*** Incompatible FileDesc's";
836  errMsg << endl
837  << " The associated FileDesc instances are not the same.";
838  cerr << endl << endl << errMsg << endl <<endl;
839  throw KKException (errMsg);
840  }
841 
842  FeatureNumList result (fileDesc);
843  result.AllocateArraySize (numOfFeatures);
844 
845  kkint32 l = 0;
846  kkint32 r = 0;
847 
848  while ((l < numOfFeatures) && (r < rightSide.numOfFeatures))
849  {
850  if (featureNums[l] < rightSide.featureNums[r])
851  {
852  l++;
853  }
854 
855  else if (featureNums[l] > rightSide.featureNums[r])
856  {
857  r++;
858  }
859 
860  else
861  {
862  result.AddFeature (featureNums[l]);
863  l++;
864  r++;
865  }
866  }
867 
868  return result;
869 } /* operator* */
870 
871 
872 
873 
874 /**
875  * @brief Performs a logical 'OR' operation on the two FeatureNumList instances.
876  * @details Will return a new FeatureNumList instance that will consist of a list of features that are in either
877  * left and right FeatureNumList instances. Both FeatureNumList objects must be referencing the same FileDesc instance
878  * otherwise an exception will be thrown.
879  */
880 FeatureNumList FeatureNumList::operator+ (const FeatureNumList& rightSide) const
881 {
882  FeatureNumList result (rightSide);
883 
884  kkint32 l = 0;
885  for (l = 0; l < numOfFeatures; l++)
886  result.AddFeature (featureNums[l]);
887 
888  return result;
889 } /* operator+ */
890 
891 
892 
893 /**
894  * @brief Adds the features that are selected in the right FeatureNumList instance to the left instance.
895  * @details Both FeatureNumList objects must be referencing the same FileDesc instance otherwise an exception will be thrown.
896  */
898 {
899  kkint32 newFeatureNumsAllocatedSize = Min (numOfFeatures + rightSide.numOfFeatures, (kkint32)fileDesc->NumOfFields ());
900  kkuint16* newFeatureNums = new kkuint16[newFeatureNumsAllocatedSize];
901  kkint32 newNumOfFeatures = 0;
902 
903  kkuint16* leftFeatureNums = featureNums;
904  kkint32 leftNumOfFeatures = numOfFeatures;
905  kkuint16* rightFeatureNums = rightSide.featureNums;
906  kkint32 rightNumOfFeatures = rightSide.numOfFeatures;
907 
908  kkint32 l = 0;
909  kkint32 r = 0;
910 
911  while ((l < leftNumOfFeatures) && (r < rightNumOfFeatures))
912  {
913  if (leftFeatureNums[l] < rightFeatureNums[r])
914  {
915  newFeatureNums[newNumOfFeatures] = leftFeatureNums[l];
916  newNumOfFeatures++;
917  l++;
918  }
919 
920  else if (leftFeatureNums[l] > rightFeatureNums[r])
921  {
922  newFeatureNums[newNumOfFeatures] = rightFeatureNums[r];
923  newNumOfFeatures++;
924  r++;
925  }
926 
927  else
928  {
929  newFeatureNums[newNumOfFeatures] = rightFeatureNums[r];
930  newNumOfFeatures++;
931  l++;
932  r++;
933  }
934  }
935 
936  while (l < leftNumOfFeatures)
937  {
938  newFeatureNums[newNumOfFeatures] = leftFeatureNums[l];
939  newNumOfFeatures++;
940  l++;
941  }
942 
943  while (r < rightNumOfFeatures)
944  {
945  newFeatureNums[newNumOfFeatures] = rightFeatureNums[r];
946  newNumOfFeatures++;
947  r++;
948  }
949 
950  delete [] featureNums;
951  featureNums = newFeatureNums;
952  featureNumsAllocatedSize = newFeatureNumsAllocatedSize;
953  numOfFeatures = newNumOfFeatures;
954 
955  return *this;
956 } /* operator+= */
957 
958 
959 
960 
961 /** @brief Adds the feature 'featureNum' to the selected list of features. */
963 {
964  AddFeature (featureNum);
965  return *this;
966 } /* operator+= */
967 
968 
969 
970 
971 
972 /** @brief Returns a new FeatureNumList instance that will consists of the left FeatureNumList instance with 'rightSide' feature added in. */
974 {
975  if (kkint32 (rightSide) >= fileDesc->NumOfFields ())
976  {
977  KKStr errMsg = "FeatureNumList::operator+ ***ERROR***";
978  errMsg <<" Feature[" << rightSide << "] is too large.";
979  cerr << endl << endl << errMsg << endl <<endl;
980  throw KKException (errMsg);
981  }
982 
983  FeatureNumList result (*this);
984  result.AddFeature (rightSide);
985  return result;
986 } /* operator+ */
987 
988 
989 
990 
991 /** @brief Returns a new FeatureNumList instance that will consists of the left FeatureNumList instance with 'rightSide' removed from it. */
993 {
994  if (rightSide >= fileDesc->NumOfFields ())
995  {
996  KKStr errMsg = "FeatureNumList::operator- ***ERROR***";
997  errMsg <<" Feature[" << rightSide << "] is too large.";
998  cerr << endl << endl << errMsg << endl <<endl;
999  throw KKException (errMsg);
1000  }
1001 
1002  FeatureNumList result (*this);
1003  result.UnSet (rightSide);
1004 
1005  return result;
1006 } /* operator- */
1007 
1008 
1009 
1010 /**
1011  * @brief Returns a new FeatureNumList instance that consists of the left side instance with the
1012  * selected features in the right side removed.
1013  * @details Both FeatureNumList objects must be referencing the same FileDesc instance otherwise
1014  * an exception will be thrown.
1015  */
1016 FeatureNumList FeatureNumList::operator- (const FeatureNumList& rightSide) const
1017 {
1018  if (fileDesc != rightSide.FileDesc ())
1019  {
1020  KKStr errMsg = "FeatureNumList::operator- ***ERROR*** Incompatible FileDesc's";
1021  errMsg << endl
1022  << " The associated FileDesc instances are not the same.";
1023  cerr << endl << endl << errMsg << endl <<endl;
1024  throw KKException (errMsg);
1025  }
1026 
1027  FeatureNumList result (*this);
1028  kkint32 x;
1029  for (x = 0; x < rightSide.NumOfFeatures (); x++)
1030  result.UnSet (rightSide[x]);
1031 
1032  return result;
1033 } /* operator- */
1034 
1035 
1036 
1037 
1038 /** @brief removes the feature specified on the right side from the FeatureNumList on the left side. */
1040 {
1041  UnSet (rightSide);
1042  return *this;
1043 } /* operator-= */
1044 
1045 
1046 
1048 {
1049  if (numToKeep > numOfFeatures)
1050  {
1051  cerr << endl
1052  << endl
1053  << "FeatureNumList::RandomlySelectFeatures *** ERROR ***" << endl
1054  << endl
1055  << "NumToKeep[" << numToKeep << "] Is greater than NumOfFeatures[" << numOfFeatures << "]" << endl
1056  << endl
1057  << endl;
1058  numToKeep = numOfFeatures;
1059  }
1060 
1061  FeatureNumListPtr randomlySelectedFeatures = new FeatureNumList (fileDesc);
1062 
1063  kkint32 x, y, z;
1064 
1065  // Initialize Selected Features to the currently selected features in featureNums
1066  kkuint16* selectedFeatures = new kkuint16[numOfFeatures];
1067  for (x = 0; x < numOfFeatures; x++)
1068  selectedFeatures[x] = featureNums[x];
1069 
1070  // Randomize the order of the selected featured
1071  //for (x = 0; x < numOfFeatures; x++)
1072  for (x = 0; x < numToKeep; x++)
1073  {
1074  y = LRand48() % numOfFeatures;
1075  z = selectedFeatures[x];
1076  selectedFeatures[x] = selectedFeatures[y];
1077  selectedFeatures[y] = z;
1078  }
1079 
1080  // Assign the first 'numToKeep' featured from the random order of selected features
1081  for (x = 0; x < numToKeep; x++)
1082  randomlySelectedFeatures->AddFeature (selectedFeatures[x]);
1083 
1084  delete [] selectedFeatures;
1085 
1086  return randomlySelectedFeatures;
1087 } /* RandomlySelectFeatures */
1088 
1089 
1090 
1091 
1093 {
1094  if ((idx < 0) || (idx >= numOfFeatures))
1095  {
1096  cerr << endl << endl
1097  << "FeatureNumList::AttributeType *** ERROR ***" << endl
1098  << endl
1099  << " Invalid Index[" << idx << "] Valid Range (0.." << (numOfFeatures - 1) << ")" << endl
1100  << endl;
1101 
1102  return NULLAttribute;
1103  }
1104 
1105  if (!fileDesc)
1106  {
1107  KKStr errMsg = "FeatureNumList::AttributeType ***ERROR*** 'fileDesc == NULL' There is a major programing flaw.";
1108  cerr << endl << endl << errMsg << endl << endl;
1109  throw KKException (errMsg);
1110  }
1111 
1112  return fileDesc->Type (featureNums[idx]);
1113 } /* FeatureAttributeType */
1114 
1115 
1116 
1118 {
1119  kkuint16 x;
1120 
1121  FeatureNumList result;
1122 
1123  for (x = 0; x < kkuint16 (fileDesc->NumOfFields ()); x++)
1124  {
1125  if (!InList (x))
1126  result.AddFeature (x);
1127  }
1128 
1129  return result;
1130 } /* Complement */
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for &#39;size&#39; characters.
Definition: KKStr.cpp:655
__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;.
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.
bool AllFeaturesSelected() const
Returns true if all features are selected.
Keeps track of selected features.
FeatureNumList(FileDescPtr _fileDesc)
KKStr HexStr() const
Returns a Hex-String representation.
Definition: BitString.cpp:361
void osWaitForEnter()
void ReSet()
Set all bits to &#39;0&#39;.
Definition: BitString.cpp:207
KKStr ExtractToken(const char *delStr="\n\t\r ")
Definition: KKStr.cpp:2969
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.
void ExtractFeatureNumsFromStr(KKStr featureListStr, bool &valid)
Will select the features specified in "featureListStr".
KKStr ToHexString() const
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
FeatureNumList & operator+=(kkuint16 featureNum)
Returns this FeatureNumList that is a union of this instance and &#39;rightSide&#39;.
FeatureNumList(FileDescPtr _fileDesc, const KKStr &_featureListStr, bool &_valid)
Constructs a &#39;FeatureNumList&#39; instance from a string that contains a list of selected features...
void SetAllFeatures()
Selects all features except those flagged as &#39;IgnoreAttribute&#39; in the associated FileDesc.
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.
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.
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 TrimLeft(const char *whiteSpaceChars="\n\r\t ")
Definition: KKStr.cpp:1745
void UnSet(kkuint16 featureNum)
Turns off specified feature &#39;featureNum&#39;; if &#39;featureNum&#39; is not turned on then nothing happens; same...
bool Empty() const
Definition: KKStr.h:241
void Save(const KKStr &_fileName, bool &_successful, RunLog &_log)
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 Upper()
Converts all characters in string to their Upper case equivalents via &#39;toupper&#39;.
Definition: KKStr.cpp:2461
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
AttributeType
Definition: Attribute.h:36
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
FileDesc * FileDescPtr
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 Save(std::ostream &o)
void Load(const KKStr &_fileName, bool &_successful, RunLog &_log)
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
kkint32 ExtractTokenInt(const char *delStr)
Definition: KKStr.cpp:3129
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
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)
FeatureNumList(FileDescPtr _fileDesc, const BitString &bitString)
Constructs a &#39;FeatureNumList&#39; instance using the set bits in &#39;bitString&#39; to indicate which features a...
KKMLL::AttributeType FeatureAttributeType(kkint32 idx) const
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;.
kkint32 MemoryConsumedEstimated() const
bool operator<(const FeatureNumList &_features) const
Indicates if the Left FeatureNumList instances is less than the right one.
FeatureNumList & operator-=(kkuint16 rightSide)