void FW::profilePush(const char* id) { if (!s_profileStarted) return; if (!Thread::isMain()) fail("profilePush() can only be used in the main thread!"); // Find or create token. S32 token; S32* found = s_profilePointerToToken.search(id); if (found) token = *found; else { found = s_profileStringToToken.search(id); if (found) token = *found; else { token = s_profileStringToToken.getSize(); s_profileStringToToken.add(id, token); } s_profilePointerToToken.add(id, token); } // Find or create timer. Vec2i timerKey(-1, token); if (s_profileStack.getSize()) timerKey.x = s_profileStack.getLast(); S32 timerIdx; found = s_profileTimerHash.search(timerKey); if (found) timerIdx = *found; else { timerIdx = s_profileTimers.getSize(); s_profileTimerHash.add(timerKey, timerIdx); ProfileTimer& timer = s_profileTimers.add(); timer.id = id; timer.parent = timerKey.x; if (timerKey.x != -1) s_profileTimers[timerKey.x].children.add(timerIdx); } // Push timer. if (s_profileStack.getSize() == 1) s_profileTimers[s_profileStack[0]].timer.start(); s_profileStack.add(timerIdx); if (s_profileStack.getSize() > 1) s_profileTimers[timerIdx].timer.start(); }
int main() { cout << "Welcome to this awesome Phone Book\n" << endl; Hash hash; int choice; string name; string phone; Node* entry; do { menu(); cin >> choice; switch (choice) { case 1: getchar(); cout << "Name: "; getline(cin, name); cout << "Phone: "; getline(cin, phone); entry = new Node(); entry->name = name; entry->phone = phone; hash.add(entry); break; case 2: getchar(); cout << "Enter full name: "; getline(cin, name); hash.search(name); break; case 3: getchar(); cout << "Name: "; getline(cin, name); hash.remove(name); break; case 4: hash.printAll(); break; case 0: hash.clearAll(); break; default: cout << "Invalid entry. Please try again" << endl; break; } } while (choice != 0); cout << "Have a good day!" << endl; return 0; }
void FW::popMemOwner(void) { #if FW_MEM_DEBUG U32 threadID = Thread::getID(); Array<const char*>* stack = s_memOwnerStacks.search(threadID); if (stack) { stack->removeLast(); if (!stack->getSize()) { s_memOwnerStacks.remove(threadID); if (!s_memOwnerStacks.getSize()) s_memOwnerStacks.reset(); } } #endif }
void FW::pushMemOwner(const char* id) { #if !FW_MEM_DEBUG FW_UNREF(id); #else s_lock.enter(); s_memPushingOwner = true; U32 threadID = Thread::getID(); Array<const char*>* stack = s_memOwnerStacks.search(threadID); if (!stack) { stack = &s_memOwnerStacks.add(threadID); stack->clear(); } stack->add(id); s_memPushingOwner = false; s_lock.leave(); #endif }
int main ( ) { Hash hashTable; cout << setprecision ( 10 ); cout << "Test 1 - print empty table" << endl; hashTable.print ( ); cout << "-------------------------------------------------------------" << endl; cout << "Test 2 - processing input file" << endl; hashTable.processFile ( "dict5.txt" ); hashTable.print ( ); cout << "-------------------------------------------------------------" << endl; cout << "Test 3 - searching" << endl; if ( hashTable.search ( "heath" ) ) cout << "Passed - searching for valid item" << endl; else cout << "FAILED - searching for valid item" << endl; if ( hashTable.search ( "hello" ) ) cout << "Passed - searching for valid item" << endl; else cout << "FAILED - searching for valid item" << endl; if ( hashTable.search ( "ttttt" ) ) cout << "Passed - searching for valid item" << endl; else cout << "FAILED - searching for valid item" << endl; if ( hashTable.search ( "empty" ) ) cout << "FAILED - searching for invalid item" << endl; else cout << "Passed - searching for invalid item" << endl; cout << "-------------------------------------------------------------" << endl; cout << "Test 4 - testing remove" << endl; hashTable.remove ( "happy" ); hashTable.print ( ); cout << endl; hashTable.remove ( "hello" ); hashTable.remove ( "harps" ); hashTable.print ( ); cout << endl; hashTable.remove ( "heath" ); hashTable.remove ( "heath" ); hashTable.remove ( "heath" ); hashTable.print ( ); cout << endl; hashTable.remove ( "rrrrr" ); hashTable.remove ( "ooooo" ); hashTable.print ( ); cout << "-------------------------------------------------------------" << endl; cout << "Test 5 - output to file" << endl; hashTable.output ( "hash.out" ); cout << endl << endl; cout << "-------------------------------------------------------------" << endl; cout << "Test 6 - Stats" << endl; hashTable.printStats ( ); cout << "-------------------------------------------------------------" << endl; return 1; }
Image* FW::importTiffImage(InputStream& stream) { // Detect endianess and check format. U8 endianTag = stream.readU8(); Input in(stream, (endianTag == 'I'), 1); if ((endianTag != 'I' && endianTag != 'M') || in.readU8() != endianTag || in.readU16() != 42) setError("Not a TIFF file!"); // Read directory header. U32 dirOfs = in.readU32(); in.seek(dirOfs); int numEntries = in.readU16(); if (!dirOfs || !numEntries) setError("Corrupt TIFF directory!"); // Read directory entries. Hash<U32, Array<U32> > entries; for (int i = 0; i < numEntries && !hasError(); i++) { U16 tag = in.readU16(); U16 type = in.readU16(); U32 count = in.readU32(); U32 ofs = in.readU32(); // Check type and count. int typeSize; switch (type) { case 1: typeSize = 1; break; // BYTE case 3: typeSize = 2; break; // SHORT case 4: typeSize = 4; break; // LONG default: typeSize = 0; break; } if (!typeSize) continue; // Seek to the value. U32 oldOfs = in.tell(); if (typeSize * count <= 4) in.seek(oldOfs - 4); else in.seek(ofs); // Read value. Array<U32>& data = entries.add(tag); data.resize(count); for (int j = 0; j < (int)count; j++) { switch (typeSize) { case 1: data[j] = in.readU8(); break; case 2: data[j] = in.readU16(); break; case 4: data[j] = in.readU32(); break; default: FW_ASSERT(false); break; } } in.seek(oldOfs); } // Find relevant entries and check their sizes. const Array<U32>* width = entries.search(256); // ImageWidth const Array<U32>* height = entries.search(257); // ImageLength const Array<U32>* numBits = entries.search(258); // BitsPerSample const Array<U32>* compression = entries.search(259); // Compression const Array<U32>* photometric = entries.search(262); // PhotometricInterpretation const Array<U32>* stripOfs = entries.search(273); // StripOffsets const Array<U32>* numChannels = entries.search(277); // SamplesPerPixel const Array<U32>* stripBytes = entries.search(279); // StripByteCounts const Array<U32>* predictor = entries.search(317); // Predictor const Array<U32>* extraSamples = entries.search(338); // ExtraSamples const Array<U32>* sampleFormat = entries.search(339); // SampleFormat if (!width || width->getSize() != 1 || !height || height->getSize() != 1 || !numBits || numBits->getSize() == 0 || !compression || compression->getSize() != 1 || !photometric || photometric->getSize() != 1 || !stripOfs || stripOfs->getSize() == 0 || !numChannels || numChannels->getSize() != 1 || !stripBytes || stripBytes->getSize() != stripOfs->getSize() || (predictor && predictor->getSize() != 1) || (extraSamples && extraSamples->getSize() != 1) || (sampleFormat && sampleFormat->getSize() != 1)) { setError("Corrupt TIFF directory!"); } if (hasError()) return NULL; // Interpret size. Vec2i size(width->get(0), height->get(0)); if (size.min() <= 0) setError("Invalid TIFF size!"); // Interpret compression. bool packBits = false; switch (compression->get(0)) { case 1: packBits = false; break; case 32773: packBits = true; break; default: setError("Unsupported TIFF compression %d!", compression->get(0)); break; } if (predictor && predictor->get(0) != 1) setError("Unsupported TIFF predictor %d!", predictor->get(0)); // Read color format. bool floats = false; if (sampleFormat) { switch (sampleFormat->get(0)) { case 1: floats = false; break; case 3: floats = true; break; default: setError("Unsupported TIFF sample format %d!", sampleFormat->get(0)); break; } } U32 photo = photometric->get(0); int numColor = numChannels->get(0); int numAlpha = (extraSamples) ? extraSamples->get(0) : 0; if (numBits->getSize() != numColor + numAlpha) setError("Invalid TIFF color format!"); for (int i = 0; i < numBits->getSize(); i++) if (numBits->get(i) != ((floats) ? 32u : 8u)) setError("Invalid TIFF color format!"); // Interpret color format. ImageFormat format; switch (photo) { case 1: // MinIsBlack if ((numColor == 0 && numAlpha == 1) || (numColor == 1 && numAlpha == 0)) format = (floats) ? ImageFormat::A_F32 : ImageFormat::A8; else setError("Unsupported TIFF monochrome color format!"); break; case 2: // RGB if (numColor == 3 && numAlpha == 0) format = (floats) ? ImageFormat::RGB_Vec3f : ImageFormat::R8_G8_B8; else if ((numColor == 3 && numAlpha == 1) || (numColor == 4 && numAlpha == 0)) format = (floats) ? ImageFormat::RGBA_Vec4f : ImageFormat::R8_G8_B8_A8; else setError("Unsupported TIFF RGB color format!"); break; default: setError("Unsupported TIFF photometric interpretation %d!", photo); } // Error => fail. if (hasError()) return NULL; // Create image. Image* image = new Image(size, format); U8* data = image->getMutablePtr(); const U8* dataEnd = data + image->getStride() * size.y; // Read each strip of image data. U8* dst = data; for (int i = 0; i < stripOfs->getSize() && !hasError(); i++) { int size = stripBytes->get(i); in.seek(stripOfs->get(i)); const U8* src = in.read(size); const U8* srcEnd = src + size; // Uncompressed => just read the bytes. if (!packBits) { dst += size; if (dst > dataEnd) break; memcpy(dst - size, src, size); continue; } // PackBits => read each RLE packet. while (src < srcEnd) { int n = *src++; if (n < 128) { n++; dst += n; src += n; if (dst > dataEnd || src > srcEnd) break; memcpy(dst - n, src - n, n); } else if (n > 128) { n = 257 - n; dst += n; src++; if (dst > dataEnd || src > srcEnd) break; memset(dst - n, src[-1], n); } } if (dst > dataEnd || src != srcEnd) setError("Corrupt TIFF image data!"); } if (dst != dataEnd) setError("Corrupt TIFF image data!"); // Float-based format => fix endianess. if (floats) for (U8* ptr = data; ptr < dataEnd; ptr += 4) if (endianTag == 'I') *(U32*)ptr = ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24); else *(U32*)ptr = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; // Handle errors. if (hasError()) { delete image; return NULL; } return image; }
int main(int argc, char *argv[]) { ofstream log; // to record log file log.open("log.txt", std::fstream::app); CommandLineParser clp(argv[1],','); //instantiating the class CommandLineParser if (argc == 1) { log << "Error: no argument is passed.\n" << endl; log.close(); return -1; } //use CommandLineParser to get the script name char *script = clp.extract("script"); if (script == NULL || *script == '\0') { log << "Error: no script file specified.\n" << endl; log.close(); return -1; } //use ScriptParser to get the commands from script file ScriptParser SParser = ScriptParser(); ifstream indata(script); if (!indata.is_open()) // check if script file is correctly opened. { log << "Error: Script file \'" << script << "\' cannot be opened or does not exist.\n" << endl; log.close(); return -1; } //start parsing script file int i = 0; int n; int size; int k; string line; // store each line in script file WordList *L; // array of word lists FreqList *F; // array of frequency lists which store the information of words and its frequency string *S; // array of strings which store the name of lists Hash HT; // hash table to store all the words L = new WordList[1000]; F = new FreqList[1000]; S = new string[1000]; //initiate temporary lists and list nodes WordNode *tempWordNode; FreqNode *tempFreqNode; WordList tempWordList; WordList *tempWordListpt; FreqList tempFreqList; FreqList tempFreqList2; string tempString; Node *tempNode; int pos1; int pos2; log << "Begin parsing script file \'" << script << "\':\n" << endl; while(getline(indata, line)) { log << line << '\n'; SParser.ParseLine(line); //process to determine if listID exits int ID = -1; // cmd.listID, after following checking process, if it's still -1, then cmd.listID is a new list, otherwise cmd.listID is the word list L[ID] or frequency list F[ID] int ID2 = -1; // cmd.value1 (if it stores list name), same procesure as above int ID3 = -1; // cmd.value2 (if it stores list name), same procesure as above for (int j = 0; j < i; j++) { ID = (SParser.cmd.listID == S[j])?j:ID; // check if cmd.listID is already in S[] } if (SParser.operationCode() != 3 && SParser.operationCode() != 8 && SParser.operationCode() != 9 && SParser.operationCode() != 10 && SParser.operationCode() != 13 && SParser.operationCode() != 0 && ID == -1) { log << "Error: invalid operation, because list \'" << SParser.cmd.listID << "\' is not created yet.\n" << endl; if (SParser.operationCode() == 4 || SParser.operationCode() == 5) { ofstream output; output.open(SParser.cmd.value1.c_str()); output << "List " << SParser.cmd.listID << " does not exist" << endl; output.close(); } continue; } if ((SParser.operationCode() == 3 || SParser.operationCode() == 8 || SParser.operationCode() == 9 || SParser.operationCode() == 10 || SParser.operationCode() == 13) && ID > -1) { log << "Error: invalid operation, because list \'" << SParser.cmd.listID << "\' already exists.\n" << endl; continue; } // do all the list operations, such as read, insert, delete, write, intersection, union, load, filter, index and seach switch (SParser.operationCode()) { case 1: n = L[ID].insert(SParser.cmd.value2, SParser.cmd.value1); if (n == -1) { log << "Warning: in insertion, " << '\'' << SParser.cmd.value1 << '\'' << " cannot be found, so insertion fails.\n"; } else { log << '\'' << SParser.cmd.value2 << '\'' << " has been inserted after the first appeared word " << '\'' << SParser.cmd.value1 << "\'.\n"; F[ID].clear(); // update frequency list based on updated word list F[ID].frequency_unsorted(L[ID]); F[ID].sort(); } log << "Number of bytes used by list \'" << S[ID] << "\' is: " << L[ID].listSize() << ".\n" << endl; break; case 2: n = L[ID].erase(SParser.cmd.value1); if (n == 0) log << "Waring: "; log << n << " word(s) " << '\'' << SParser.cmd.value1 << '\'' << " have been deleted.\n"; F[ID].clear(); // update frequency list based on updated word list F[ID].frequency_unsorted(L[ID]); F[ID].sort(); log << "Number of bytes used by list \'" << S[ID] << "\' is: " << L[ID].listSize() << ".\n" << endl; break; case 3: S[i] = SParser.cmd.listID; // create a new listname in S[] n = L[i].read(SParser.cmd.value1); // create a new word list in L[] if (n == 0) { log << "Input file \'" << SParser.cmd.value1 << "\' is successfully read into list.\n"; } else { log << "Error: fail to open input file \'" << SParser.cmd.value1 << "\'.\n" << endl; continue; } F[i].frequency_unsorted(L[i]); // create a new frequency list in F[] F[i].sort(); // sort the frequency list log << "Number of bytes used by list \'" << S[i] << "\' is: " << F[i].listSize() << ".\n" << endl; i++; break; case 4: n = F[ID].output_forward(SParser.cmd.value1); if (n == 0) { log << "List is successfully written to file " << '\'' << SParser.cmd.value1 << '\'' << " in forward order.\n"; } else { log << "Error: fail to create output file " << SParser.cmd.value1 << ".\n"; } log << "Number of bytes used by list \'" << S[ID] << "\' is: " << F[ID].listSize() << ".\n" << endl; break; case 5: n = F[ID].output_backward(SParser.cmd.value1); if (n == 0) { log << "List is successfully written to file " << '\'' << SParser.cmd.value1 << '\'' << " in reverse order.\n"; } else { log << "Error: fail to create output file " << SParser.cmd.value1 << ".\n"; } log << "Number of bytes used by list \'" << S[ID] << "\' is: " << F[ID].listSize() << ".\n" << endl; break; case 6: for (int j = 0; j < i; j++) { ID2 = (SParser.cmd.value1 == S[j])?j:ID2; ID3 = (SParser.cmd.value2 == S[j])?j:ID3; } if (ID2 < 0 ) { log << "Error: invalid operation, because list \'" << SParser.cmd.value1 << "\' is not created yet.\n" << endl; continue; } else if (ID3 != -1 ) { log << "Error: invalid operation, because list \'" << SParser.cmd.value2 << "\' already exists.\n" << endl; continue; } else { S[i] = SParser.cmd.value2; size = (F[ID].size() > F[ID2].size())?F[ID].size():F[ID2].size(); if (size < 100) F[i].list_union(F[ID],F[ID2],F[ID].head,F[ID2].head); else F[i].list_union_nonrec(F[ID],F[ID2]); log << "Number of bytes used by list \'" << S[i] << "\' is: " << F[i].listSize() << ".\n" << endl; i++; } break; case 7: for (int j = 0; j < i; j++) { ID2 = (SParser.cmd.value1 == S[j])?j:ID2; ID3 = (SParser.cmd.value2 == S[j])?j:ID3; } if (ID2 < 0 ) { log << "Error: invalid operation, because list \'" << SParser.cmd.value1 << "\' is not created yet.\n" << endl; continue; } else if (ID3 != -1 ) { log << "Error: invalid operation, because list \'" << SParser.cmd.value2 << "\' already exists.\n" << endl; continue; } else { S[i] = SParser.cmd.value2; size = (F[ID].size() > F[ID2].size())?F[ID].size():F[ID2].size(); if (size < 100) F[i].list_intersection(F[ID],F[ID2],F[ID].head,F[ID2].head); else F[i].list_intersection_nonrec(F[ID],F[ID2]); log << "Number of bytes used by list \'" << S[i] << "\' is: " << F[i].listSize() << ".\n" << endl; i++; } break; case 8: string *expression; expression = analysis(SParser.cmd.value1); if (expression == NULL) { log << "Error: invalid or too long arithmetic expression.\n" << endl; continue; } S[i] = SParser.cmd.listID; n = arithmetic(expression, S, F, i); if (n == -1) { log << "Error: one or more lists in above arithmetic expression do not exist, or invalid arithmetic expression which makes stack overflow or underflow.\n" << endl; continue; } log << "Number of bytes used by list \'" << S[i] << "\' is: " << F[i].listSize() << ".\n" << endl; i++; delete[] expression; break; case 9: log << "Error: this program does not support \'check\' operation." << endl; break; case 10: S[i] = SParser.cmd.listID; // create a new listname in S[] n = L[i].read_keepUpper(SParser.cmd.value1); // create a new word list in L[] if (n != 0) { log << "Error: fail to open input file \'" << SParser.cmd.value1 << "\'.\n" << endl; continue; } tempWordNode = L[i].head; while (tempWordNode) { n = tempWordList.read(tempWordNode->word); if (n == 0) { F[i].insert("*"+tempWordNode->word,1); tempFreqList.frequency_unsorted(tempWordList); tempFreqList.sort(); F[i].append(tempFreqList,tempFreqList.head); tempWordList.clear(); tempFreqList.clear(); } if (n != 0) { log << "Error: fail to open file \'" << tempWordNode->word << "\', so words in this file are ignored." << endl; } tempWordNode = tempWordNode->pnext; } /*cout << "At very beginning:" << endl; F[i].print(); cout << endl;*/ // then, eliminate all the nodes without any letter. tempFreqNode = F[i].head; while (tempFreqNode) { k = 0; tempString = tempFreqNode->word; for (int j = 0; j < tempString.length(); j++) { if (isalpha(tempString[j])) { k = 1; break; } } tempFreqNode = tempFreqNode->pnext; if (k == 0) F[i].deletenode(tempFreqNode->pprev); } log << "Files specified in \'" << SParser.cmd.value1 << "\' are successfully loaded into list \'" << SParser.cmd.listID << "\'.\n" << endl; /*cout << "Before filter:" << endl; F[i].print(); cout << endl;*/ i++; break; case 11: n = tempWordList.read(SParser.cmd.value1); if (n != 0) { log << "Error: fail to open file \'" << SParser.cmd.value1 << "\'.\n" << endl; continue; } tempFreqList.frequency_unsorted(tempWordList); tempFreqList.sort(); tempFreqNode = tempFreqList.head; while (tempFreqNode) { F[ID].erase(tempFreqNode->word); tempFreqNode = tempFreqNode->pnext; } tempWordList.clear(); tempFreqList.clear(); log << "Words from file \'" << SParser.cmd.value1 << "\' are successfully eliminated from list \'" << SParser.cmd.listID << "\'.\n" << endl; /*cout << "After filter:" << endl; F[ID].print(); cout << endl;*/ break; case 12: tempFreqNode = F[ID].head; while (tempFreqNode) { if (tempFreqNode->word[0] == '*') tempString = (tempFreqNode->word).substr(1,(tempFreqNode->word).length()-1); else HT.insert(tempFreqNode->word,tempString); tempFreqNode = tempFreqNode->pnext; } log << "Hash table is sucessfully established (or updated).\n" << endl; /*cout << "Words are stored in hash table as:" << endl; for (int j = 0; j < HT.size; j++) { tempNode = (HT.Table[j]).head; if (tempNode == NULL) emp++; if (tempNode != NULL) occ++; while (tempNode) { if (tempNode->pnext != NULL) con++; cout << "position: " << j << ", word: " << tempNode->IndexWord << endl; // cout << "file(s): "; // (tempNode->FileName).print(); tempNode = tempNode->pnext; } }*/ break; case 13: S[i] = SParser.cmd.listID; tempString = SParser.cmd.value1; transform(tempString.begin(),tempString.end(),tempString.begin(),::tolower); //transfer all the key words into lower case pos1 = 0; while (pos1 != string::npos) { pos2 = tempString.find(","); tempWordList.insert(tempString.substr(pos1,pos2)); if (pos2 == string::npos) pos1 = pos2; else { pos1 = 0; tempString = tempString.substr(pos2+1,tempString.length()-pos2-1); } } tempWordNode = tempWordList.head; while (tempWordNode) { tempWordListpt = HT.search(tempWordNode->word); tempFreqList.frequency_unsorted(*tempWordListpt); tempFreqList.sort(); if (tempWordNode->pprev == NULL) { F[i].copy(tempFreqList); tempFreqList.clear(); } else { tempFreqList2.copy(F[i]); F[i].clear(); F[i].list_intersection_nonrec(tempFreqList,tempFreqList2); tempFreqList.clear(); tempFreqList2.clear(); } tempWordNode = tempWordNode->pnext; } tempWordList.clear(); i++; log << "Names of those files which contain the words: '" << SParser.cmd.value1 << "' are successfully found and written into list " << SParser.cmd.listID << ".\n" << endl; break; case 0: log << "Error: above line is not a valid script form.\n" << endl; break; } } log << "End parsing script file \'" << script << "\'.\n" <<endl; log.close(); delete[] L; delete[] F; delete[] S; return 0; }