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