20 using namespace KKMLL;
25 featureNumsAllocatedSize (0),
29 kkuint16* otherFeatureNums = _featureNumList.featureNums;
30 kkuint16 otherNumOfFeatures = _featureNumList.numOfFeatures;
31 AllocateArraySize (_featureNumList.numOfFeatures + 1);
32 for (
kkuint16 x = 0; x < otherNumOfFeatures; x++)
42 featureNumsAllocatedSize (0),
43 maxFeatureNum (_maxFeatureNum),
46 AllocateArraySize (_maxFeatureNum + 1);
53 featureNumsAllocatedSize (0),
62 AllocateArraySize ((kkint32)listOfSelectedFeatures.size ());
63 for (kkuint32 x = 0; x < listOfSelectedFeatures.size (); x++)
64 AddFeature (listOfSelectedFeatures[x]);
75 featureNumsAllocatedSize (0),
81 if (selFeatures == NULL)
105 featureNumsAllocatedSize (0),
115 delete [] featureNums;
124 memoryConsumedEstimated +=
sizeof (
kkuint16) * featureNumsAllocatedSize;
126 return memoryConsumedEstimated;
133 if (featureNumsAllocatedSize >= size)
142 featureNumsAllocatedSize = size;
153 for (x = 0; x < numOfFeatures; ++x)
154 newFeatureNums[x] = featureNums[x];
156 for (x = numOfFeatures; x < size; ++x)
157 newFeatureNums[x] = 0;
159 delete [] featureNums;
160 featureNums = newFeatureNums;
161 newFeatureNums = NULL;
162 featureNumsAllocatedSize = size;
174 bitStr
.Set (featureNums[x]
);
182 for (
kkint32 x = 0; x < numOfFeatures; x++)
183 newList[x] = featureNums[x];
203 for (x = 0; x < featureNumsAllocatedSize; x++)
217 kkint32 indexToDel = numOfFeatures - 1;
219 while ((indexToDel >= 0) && (featureNums[indexToDel] > featureNum))
224 if (featureNums[indexToDel] == featureNum)
228 for (x = indexToDel; x < (numOfFeatures - 1); x++)
229 featureNums[x] = featureNums[x + 1];
233 featureNums[numOfFeatures] = 0;
244 featureNumsAllocatedSize = 10;
247 if (numOfFeatures >= featureNumsAllocatedSize)
250 kkint32 newFeatureNumsAllocatedSize = numOfFeatures + 10;
254 for (x = 0; x < numOfFeatures; ++x)
255 newFeatureNums[x] = featureNums[x];
257 while (x < newFeatureNumsAllocatedSize)
259 newFeatureNums[x] = 0;
263 delete [] featureNums;
264 featureNums = newFeatureNums;
265 featureNumsAllocatedSize = newFeatureNumsAllocatedSize;
268 if (numOfFeatures == 0)
270 featureNums[0] = featureNum;
277 if (featureNums[x] < featureNum)
280 featureNums[numOfFeatures] = featureNum;
286 while ((x >= 0) && (featureNums[x] > featureNum))
288 featureNums[x + 1] = featureNums[x];
289 featureNums[x] = featureNum;
311 for (
kkuint16 fn = 0; fn <= maxFeatureNum; ++fn)
313 if (attributeTypes[fn] != IgnoreAttribute)
314 features.AddFeature (fn);
329 for (
kkuint16 x = 0; x <= maxFeatureNum; ++x)
331 if (fileDesc->Type (x) != IgnoreAttribute)
332 AddFeature (kkuint16 (x));
345 bool isSubSet =
true;
364 while ((x < numOfFeatures) && (!found))
366 found = (_featureNum == featureNums[x]);
385 if (_idx >= numOfFeatures)
388 errMsg <<
"FeatureNumList::operator[] ***ERROR*** Invalid Index[" << _idx <<
"] requested.";
389 cerr << endl << errMsg << endl << endl;
394 return featureNums[_idx];
402 KKStr featureNumStr
(numOfFeatures * 6
);
404 if (numOfFeatures <= 0)
405 return featureNumStr;
409 while (nextIdx < numOfFeatures)
411 kkint32 startOfGroup = nextIdx;
414 while ((endOfGroup < (numOfFeatures - 1)) &&
415 (featureNums[endOfGroup] == (featureNums[endOfGroup + 1] - 1))
421 if ((endOfGroup - startOfGroup) < 3)
424 for (x = startOfGroup; x <= endOfGroup; x++)
427 featureNumStr <<
",";
428 featureNumStr << featureNums[x];
434 featureNumStr <<
",";
435 featureNumStr << featureNums[startOfGroup] <<
"-" << featureNums[endOfGroup];
438 nextIdx = endOfGroup + 1;
441 return featureNumStr;
484 results->push_back (n);
492 if ((startNum > endNum) || (startNum < 0) || (endNum >
uint16_max))
498 for (kkuint16 z = startNum; z <= endNum; ++z)
499 results->push_back (z);
511 sort (results->begin (), results->end ());
531 VectorUint16* list = StrToUInt16Vetor (_featureListStr);
539 if (list->size () < 1)
543 return new FeatureNumList (1);
547 kkuint16 lastNum = (*list)[list->size () - 1];
550 VectorUint16::const_iterator idx;
551 for (idx = list->begin (); idx != list->end (); ++idx)
552 result->AddFeature (*idx);
569 _log.Level (20) <<
"FeatureNumList::Load - File[" << _fileName <<
"]." << endl;
573 featureNumsAllocatedSize = 0;
580 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** Could Not Open File[" << _fileName <<
"]." << endl << endl;
587 KKStr featureNumStr =
"";
589 kkint32 fileDescNumOfFields = 0;
592 while (fgets (buff,
sizeof (buff), inputFile))
595 KKStr fieldName = line.ExtractToken2 (
"\n\t\r").Trim ();
597 if (fieldName.EqualIgnoreCase (
"MaxFeatureNum"))
598 mfn = (kkuint16)line.ExtractTokenUint (
"\t\n\r");
600 else if (fieldName.EqualIgnoreCase (
"NumOfFeatures"))
601 nof = (kkuint16)line.ExtractTokenUint (
"\t\n\r");
603 else if (fieldName.EqualIgnoreCase (
"FeatureNums"))
604 featureNumStr = (kkuint16)line.ExtractTokenUint (
"\t\n\r");
611 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** 'maxFeatureNum' was not defined." << endl << endl;
617 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** 'featureNums' was not specified." << endl << endl;
625 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** 'featureNums' included invalid features." << endl << endl;
631 kkuint16 lastNum = (*list)[list->size () - 1];
634 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** 'featureNums' included features[" << lastNum <<
"] which is larger than maxFeaureNum[" << maxFeatureNum <<
"]" << endl << endl;
638 else if (list->size () != nof)
640 _log.Level (-1) << endl <<
"FeatureNumList::Load ***ERROR*** numOfFeatures[" << nof <<
"] did not agree with number of features specified in FeatureNums" << endl << endl;
647 AllocateArraySize (nof + 1);
648 VectorUint16::const_iterator idx;
649 for (idx = list->begin (); idx != list->end (); ++idx)
661 ofstream outFile (fileName.Str ());
662 outFile <<
"MaxFeatureNum" <<
"\t" << maxFeatureNum << endl;
663 outFile <<
"NumOfFeatures" <<
"\t" << numOfFeatures << endl;
664 outFile <<
"FeatureNums" <<
"\t" << ToString () << endl;
673 o <<
"<FeatureNumList>" <<
"\t" 674 <<
"MaxFeatureNum" <<
"\t" << maxFeatureNum <<
"\t" 675 <<
"NumOfFeatures" <<
"\t" << numOfFeatures <<
"\t" 676 <<
"FeatureNums" <<
"\t" << ToString ()
677 <<
"</FeatureNumList>";
689 featureNums =
new kkuint16[numOfFeatures];
690 featureNumsAllocatedSize = numOfFeatures;
693 for (
kkint32 x = 0; x < numOfFeatures; ++x)
694 featureNums[x] = _features
[x];
715 if (featureNums[x] < _features.featureNums[x])
718 else if (featureNums[x] > _features.featureNums[x])
724 if (x < numOfFeatures)
727 else if (x < _features.numOfFeatures)
738 if (numOfFeatures != _features.numOfFeatures)
774 ostream& operator<< ( ostream& os,
783 ostream& operator<< ( ostream& os,
801 kkuint16 mfn = Max (maxFeatureNum, rightSide.MaxFeatureNum ());
802 kkuint16 bestCaseNof = Min (numOfFeatures, rightSide.NumOfFeatures ());
804 result.AllocateArraySize (bestCaseNof);
809 while ((l < numOfFeatures) && (r < rightSide.numOfFeatures))
811 if (featureNums[l] < rightSide.featureNums[r])
814 else if (featureNums[l] > rightSide.featureNums[r])
842 for (l = 0; l < numOfFeatures; l++)
859 for (
kkuint16 x = 0; x < rightNumOfFeatures; ++x)
913 while ((l < numOfFeatures) && (r < rightSide.numOfFeatures))
915 if (featureNums[l] < rightSide.featureNums[r])
921 else if (featureNums[l] > rightSide.featureNums[r])
948 if (numToKeep > numOfFeatures)
952 <<
"FeatureNumList::RandomlySelectFeatures *** ERROR ***" << endl
954 <<
"NumToKeep[" << numToKeep <<
"] Is greater than NumOfFeatures[" << numOfFeatures <<
"]" << endl
957 numToKeep = numOfFeatures;
966 for (x = 0; x < numOfFeatures; x++)
967 selectedFeatures[x] = featureNums[x];
971 for (x = 0; x < numToKeep; x++)
974 z = selectedFeatures[x];
975 selectedFeatures[x] = selectedFeatures[y];
976 selectedFeatures[y] = z;
980 for (x = 0; x < numToKeep; x++)
983 delete [] selectedFeatures;
985 return randomlySelectedFeatures;
996 while (fni < numOfFeatures)
998 while (x < featureNums[fni])
1006 while (x < maxFeatureNum)
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
void Save(const KKStr &fileName)
bool EqualIgnoreCase(const char *s2) const
FeatureNumListPtr RandomlySelectFeatures(kkint32 numToKeep) const
Generates a new FeatureNumList object that will select at random 'numToKeep' features from this insta...
FeatureNumList operator+(kkuint16 rightSide) const
Returns new FeatureNumList that is a union of this instance and 'rightSide'.
kkuint32 NumOfFields() const
FeatureNumList operator+(const FeatureNumList &rightSide) const
Returns new FeatureNumList that is a union of this instance and 'rightSide'.
kkint32 LRand48()
A implementations of the Unix version of rand48 returning a 32 bit integer.
bool Test(kkuint16 _featureNum) const
Indicates whether feature '_featureNum' 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.
void ReSet()
Set all bits to '0'.
unsigned __int16 kkuint16
16 bit unsigned integer.
FeatureNumList & operator=(const FeatureNumListPtr _features)
bool InList(kkuint16 featureNum) const
returns true if '_featureNum' is one of the selected features.
KKStr ToHexString() const
const AttributeTypeVector & AttributeVector() const
FeatureNumList & operator+=(kkuint16 featureNum)
Returns this FeatureNumList that is a union of this instance and 'rightSide'.
void SaveXML(std::ostream &o)
FeatureNumList(const BitString &bitString)
Constructs a 'FeatureNumList' instance using the set bits in 'bitString' to indicate which features a...
unsigned __int32 kkuint32
FeatureNumList(const FeatureNumList &featureNumList)
Copy constructor.
kkuint16 * CreateFeatureNumArray() const
Allocates a array of kkint32'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)
Allows you to manage very long bit strings.
FeatureNumList Complement() const
Perform a complement of selected features. That is if a feature is selected turn it off and if it is ...
kkuint16 operator[](kkint32 idx) const
Returns back the selected feature.
KKStrParser(const KKStr &_str)
bool IsSubSet(const FeatureNumList &z)
Returns true if 'z' 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 'featureNum' to the list of selected features. If it is already selected nothing happens...
void UnSet(kkuint16 featureNum)
Turns off specified feature 'featureNum'; if 'featureNum' is not turned on then nothing happens; same...
kkuint32 BitLen() const
Returns the length of the bit-string.
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index 'firstChar' and ending at 'lastInd...
void SetAllFeatures(FileDescPtr fileDesc)
Selects all features except those flagged as 'IgnoreAttribute' in the associated FileDesc.
KKStr ToHexString(FileDescPtr fileDesc) const
Uses 'fileDesc' 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 'std::string' strings.
void ListOfSetBits16(VectorUint16 &setBits) const
kkint32 LocateCharacter(char ch) const
Returns index of 1st occurrence of 'ch' otherwise -1.
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 'bitNum' to '1'.
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.
bool AllFeaturesSelected(FileDescPtr fileDesc) const
Returns true if all features are selected.
FeatureNumList(const KKStr &_featureListStr, bool &_valid)
Constructs a 'FeatureNumList' instance from a string that contains a list of selected features...
FeatureNumList * FeatureNumListPtr
FILE * osFOPEN(const char *fileName, const char *mode)
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.
void EncodeProblem(const struct svm_paramater ¶m, 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 '0'.
Class that manages the extraction of tokens from a String without being destructive to the original s...
KKException(const KKStr &_exceptionStr)
std::vector< kkuint16 > VectorUint16
Vector of unsigned 16 bit integers.
FeatureNumList & operator+=(const FeatureNumList &rightSide)
Returns this FeatureNumList that is a union of this instance and 'rightSide'.
KKStr SubStrPart(kkint32 firstChar) const
returns a SubString consisting of all characters starting at index 'firstChar' until the end of the s...
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)