![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
Read data from the CSV(Comma Separated Values) file, and then store them in the SFXArray.
Related Information: SFXFile
Example 13.31. Store the data of CSV file in the array
SFCError error; // error value SFXFile file; // SFXFile instance SFXAnsiStringStreamReader reader; // input stream for file SFXAnsiString stringFromFile; // variable into which data is read from file SFXAnsiString csvElement; SInt32 startOfComma, endOfComma; // position of comma SInt32 sum; // sum of items SFXArray<SInt32> array; // array to store items of SInt32 SFXArray<SInt32>::Enumerator etor; // item enumerator for array to store items of SInt32 // open file in read mode if ((error = file.OpenReadOnly(SFXPath("/data.csv"))) == SFERR_NO_ERROR) { // get input stream (buffer size: 1024) if ((error = file.GetStreamReader(1024, &reader)) == SFERR_NO_ERROR) { // fetch if ((error = reader.Fetch()) == SFERR_NO_ERROR) { // read data from input stream into stringFromFile if ((error = reader.ReadSFXAnsiString(&stringFromFile)) == SFERR_NO_ERROR) { startOfComma = 0; // seek for position of comma endOfComma = stringFromFile.FirstIndexOf(','); while (endOfComma >= 0) { // repeat as long as comma is found // get string between two commas csvElement = stringFromFile.Substring(startOfComma, endOfComma); // convert string into number and store in array if ((error = array.InsertLast(csvElement.AsSInt32())) != SFERR_NO_ERROR) { // error has occured break; } // set startOfComma to seek for next comma startOfComma = endOfComma + 1; // seek for position of comma starting from startOfComma endOfComma = stringFromFile.FirstIndexOf(',', startOfComma); } if (error == SFERR_NO_ERROR) { // get last string csvElement = stringFromFile.Substring(startOfComma, stringFromFile.GetLength()); // convert string into number and store in array if ((error = array.InsertLast(csvElement.AsSInt32())) == SFERR_NO_ERROR) { // stop reading here // get enumerator etor = array.GetFirstEnumerator(); sum = 0; while (etor.HasNext()) { // process each item SInt32 c; c = etor.GetNext(); // get next item sum += c; // add item to sum } TRACE("%d", sum); } } } } reader.Release(); } file.Close(); }
Sort the items by using the insert sorting algorithm. The SFXList class is used to store items since insert operation is very fast.
Example 13.33. Sort the list
SFCError error; // error value SFXList<SInt32> list; // list to store items of SInt32 SFXList<SInt32>::Enumerator etor; // item enumerator SInt32 number[] = {14, 3, 22, -5, 8, 16, 1, 0, -7, -2}; // data for sort SInt32 i, j, ithValue, jthValue; for (i = 0; i < lengthof(number); ++i) { error = list.InsertLast(number[i]);// append item from array to list if (error != SFERR_NO_ERROR) { // if error occurs // error processing ... return; } } // Insert sort for (i = 1; i < list.GetSize(); ++i) { ithValue = list.Get(i); // get i-th item etor = list.GetFirstEnumerator(); // get enumerator for (j = 0; j < i; ++j) { jthValue = etor.GetNext(); // after getting item, advance enumerator next if (jthValue > ithValue) { list.Remove(i); // delete item (after deleting, enumerator is invalid) error = list.Insert(j, ithValue); // insert item if (error != SFERR_NO_ERROR) { // if error occurs // error processing ... return; } break; } } } // display each item etor = list.GetFirstEnumerator(); while (etor.HasNext()) { TRACE("%d", etor.GetNext()); }
The item of which size exceeds 4 bytes cannot be stored in the Collection class. In this case, store and process the pointer to that item in the Collection class.
Example 13.35. Store in the SFXList
SFCError error; // error value SFXList<SFXLinePtr> list; // list to store items of SFXLinePtr SFXList<SFXLinePtr>::Enumerator etor; // item enumerator SFXMTRandom random; // random number SFXLinePtr line, ithLine, jthLine; // pointer to SFXLine instances SInt32 i, j; // generate 10 lines at random and store them in list for (i = 0; i < 10; ++i) { // generate line values at random if ((line = new SFXLine(random.GetSInt16(), random.GetSInt16(), random.GetSInt16(), random.GetSInt16())) != null) { // append line to list error = list.InsertLast(line); } else { // if fail to generate line error = SFERR_NO_MEMORY; } if (error != SFERR_NO_ERROR) { break; } } // sort SFXLine instances stored in list by the X coordinate of starting point if (error == SFERR_NO_ERROR) { // insert sort (compare with the X coordinate of start point) for (i = 1; i < list.GetSize(); ++i) { ithLine = list.Get(i); // get i-th item etor = list.GetFirstEnumerator(); // get enumerator for (j = 0; j < i; ++j) { jthLine = etor.GetNext(); // get item, and then advance enumerator next // compare with the X coordinate of start point if (jthLine->GetStartX() > ithLine->GetStartX()) { list.Remove(i); // delete item (note: its instance is not deleted) error = list.Insert(j, ithLine); // insert item break; } } if (error != SFERR_NO_ERROR) { // break loop if error occurs break; } } } // instances must be deleted after used etor = list.GetFirstEnumerator(); while (etor.HasNext()) { line = etor.GetNext(); delete line; }
Count the occurrences of words in a string by using the SFXHashmap class.
Example 13.36. Count the number of words
SFXAnsiString string("abc def def def ghi ghi"); // string to count
SFXHashmap<SFXAnsiString, SInt32> hashmap; // hashmap of (key: SFXAnsiString, value: SInt32)
SFXAnsiString word; // word in string
Bool isAlphabet = false; // true while alphabetical character is being processed
SInt32 startOfWord, endOfWord;
SInt32 i;
// append dummy non-alphabetic character at the end of string
string += '.';
for (i = 0; i < string.GetLength(); ++i) {
// check whether string[i] is alphabetical or not
if (SFXAscii::IsAlphaDigit(string[i])) {
if (!isAlphabet) {
// when alphabetical character is not being processed
startOfWord = i; // set starting position of word
isAlphabet = true;
}
} else {
// when alphabetical character is being processed
if (isAlphabet) {
endOfWord = i;
// get word from string
word = string.Substring(startOfWord, endOfWord);
if (hashmap.ContainsKey(word)) {
// if word is included
// get current occurrence for this word
SInt32 count = hashmap.Get(word);
// increase count by 1
hashmap.Set(word, count + 1);
} else {
// set count to 1 for first occurrence
hashmap.Set(word, 1);
}
isAlphabet = false;
}
}
}
SInt32 n1 = hashmap.Get("def"); // n1 = 3
SInt32 n2 = hashmap.Get("abc"); // n2 = 1
SInt32 n3 = hashmap.Get("jkl"); // n3 = 0
|
Copyright (C) 2002 - 2008 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|