void printTree(TreeMap<K, V> t) { typename TreeMap<K, V>::Iterator itr = t.iterator(); while (itr.hasNext()) { typename TreeMap<K, V>::Entry e = itr.next(); cout << "(" << e.getKey() << ", " << e.getValue() << ")\t"; } cout << endl; cout << "End print" << endl; }
int main() { TreeMap<int, int> mapa; mapa.insert(8, 0); mapa.insert(2, 0); mapa.insert(1, 0); mapa.insert(5, 0); mapa.insert(3, 0); cout << mapa.siguienteClave(1) << endl; cout << mapa.siguienteClave(2) << endl; cout << mapa.siguienteClave(3) << endl; cout << mapa.siguienteClave(5) << endl; cout << mapa.siguienteClave(8) << endl; return 0; }
Vector3 * VertexDataBase::Insert(Vector3 vertex){ TreeMap<float,TreeMap<float,Vector3*>*>* xTarget = _database->get(vertex.X); if(xTarget==NULL){ xTarget = new TreeMap<float,TreeMap<float,Vector3*>*>(new FloatComparator()); _database->put(vertex.X,xTarget); } TreeMap<float,Vector3*>* yTarget = xTarget->get(vertex.Y); if(yTarget==NULL){ yTarget = new TreeMap<float,Vector3*>(new FloatComparator()); xTarget->put(vertex.Y,yTarget); } Vector3 * zTarget = yTarget->get(vertex.Z); if(zTarget==NULL){ zTarget = new Vector3(vertex.X,vertex.Y,vertex.Z); _all->push(zTarget); yTarget->put(vertex.Z,zTarget); } return zTarget; }
int main() { char *aName[3] = {"Alice\0", "Bob\0", "Wesker\0"}; TreeMap *pMap; /* You should initialize the DS before any operations. */ int32_t rc = TreeMapInit(&pMap); if (rc != SUCC) return rc; /* You should specify how to compare your items. */ pMap->set_compare(pMap, CompareKey); /* If you plan to delegate the resource clean task to the DS, please set the custom clean method. */ pMap->set_destroy(pMap, DestroyPair); /* Insert key value pairs into the map. */ Employ *pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 1; pEmploy->cYear = 25; pEmploy->cLevel = 100; Pair *pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[0]; pPair->value = (Value)pEmploy; pMap->put(pMap, pPair); pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 2; pEmploy->cYear = 25; pEmploy->cLevel = 90; pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[1]; pPair->value = (Value)pEmploy; pMap->put(pMap, pPair); pEmploy = (Employ*)malloc(sizeof(Employ)); pEmploy->iId = 3; pEmploy->cYear = 25; pEmploy->cLevel = 80; pPair = (Pair*)malloc(sizeof(Pair)); pPair->key = (Key)aName[2]; pPair->value = (Value)pEmploy; pMap->put(pMap, pPair); /* Retrieve the value with the designated key. */ Value value; pMap->get(pMap, (Key)aName[0], &value); assert(((Employ*)value)->iId == 1); assert(((Employ*)value)->cYear == 25); assert(((Employ*)value)->cLevel == 100); /* Retrieve the pairs with minimum and maximum key orders from the map. */ pMap->minimum(pMap, &pPair); assert(strcmp((char*)pPair->key, aName[0]) == 0); assert(((Employ*)pPair->value)->cLevel == 100); pMap->maximum(pMap, &pPair); assert(strcmp((char*)pPair->key, aName[2]) == 0); assert(((Employ*)pPair->value)->cLevel == 80); /* Reference the predecessor and successor pairs with the designated key. */ pMap->predecessor(pMap, (Key)aName[1], &pPair); assert(strcmp((char*)pPair->key, aName[0]) == 0); assert(((Employ*)pPair->value)->iId == 1); pMap->successor(pMap, (Key)aName[1], &pPair); assert(strcmp((char*)pPair->key, aName[2]) == 0); assert(((Employ*)pPair->value)->iId == 3); /* Iterate through the map. */ int32_t idx = 0; pMap->iterate(pMap, true, NULL); while (pMap->iterate(pMap, false, &pPair) == CONTINUE) { assert(strcmp((char*)pPair->key, aName[idx]) == 0); idx++; } /* Reversely iterate through the map.*/ idx = 2; pMap->reverse_iterate(pMap, true, NULL); while (pMap->reverse_iterate(pMap, false, &pPair) == CONTINUE) { assert(strcmp((char*)pPair->key, aName[idx]) == 0); idx--; } /* Remove the key value pair with the designated key. */ pMap->remove(pMap, (Key)aName[1]); /* Check the key existence. */ assert(pMap->find(pMap, (Key)aName[0]) == SUCC); assert(pMap->find(pMap, (Key)aName[1]) == NOKEY); assert(pMap->find(pMap, (Key)aName[2]) == SUCC); /* Check the pair count in the map. */ int32_t iSize = pMap->size(pMap); assert(iSize == 2); /* You should deinitialize the DS after all the relevant tasks. */ TreeMapDeinit(&pMap); return SUCC; }
int wmain(int argc, Chr16** argv) { Lock::setupGlobalLock(); Error error; LogStdout log; class Fatal {}; try { loginfo("Tomazos Resource Compiler 1.0 (c) Andrew Tomazos 2009"); if (argc != 3) { error.what(L"Usage: ResourceCompiler <ResourceDir> <Out.cpp>"); throw Fatal(); } UTF16 sSourceDirPath = argv[1]; UTF16 sOutPath = argv[2]; Path sourceDir(error, sSourceDirPath); if (error) throw Fatal(); UTF16 sFullSourceDirPath = UTF16(sourceDir); if (!sFullSourceDirPath.endsWith(L"\\")) sFullSourceDirPath = sFullSourceDirPath + L"\\"; UTF16List fps; sourceDir.walk(ResourceCompilerWalker(fps)); loginfo("Found %d files", fps.size()); TreeMap<UTF16, Blob> data; { UTF16List::Iterator it(fps); UTF16 sFullFilePath; while (it(sFullFilePath)) { if (!sFullFilePath.startsWith(sFullSourceDirPath)) { Check(); throw Fatal(); } UTF16 sRelFilePath = sFullFilePath.suffix(sFullFilePath.length() - sFullSourceDirPath.length()); Path file(error, sFullFilePath); if (error) throw Fatal(); HInputStream fis; file.readFile(error, fis); if (error) throw Fatal(); Blob sFileContent = fis->slurp(error); if (error) throw Fatal(); loginfo("Found %s (%d bytes)", sRelFilePath.ptr(), sFileContent.length()); data.add(sRelFilePath, sFileContent); } } Path outPath(error, sOutPath); if (error) throw Fatal(); HOutputStream hOut; outPath.overwriteFile(error, hOut); if (error) throw Fatal(); #define ResLine(x) { hOut->twrite(error, UTF8(x)); if (error) throw Fatal(); hOut->twrite(error, UTF8("\r\n")); if (error) throw Fatal(); } #define ResLineF(...) { ResLine(UTF8::format(__VA_ARGS__)); } ResLine("#include \"Runtime.h\""); ResLine(""); ResLine("void ResourceManager::setup()"); ResLine("{"); { TreeMap<UTF16, Blob>::Iterator it(data); UTF16 sPath; Blob bData; int iCount = 0; int iNumResources = data.size(); while (it(sPath, bData)) { iCount++; UTF8 sId = UTF8::format("s_res_data_%d", iCount); ResLineF(" static UInt8 %s[] = { ", sId.ptr()); for (int i = 0; i < bData.length(); i++) { if (i % 16 == 0) { hOut->twrite(error, UTF8(" ")); if (error) throw Fatal(); } hOut->twrite(error, UTF8::format("0x%.2X", UInt32(bData.idx<UInt8>(i)))); if (error) throw Fatal(); if (i != bData.length() - 1) { hOut->twrite(error, UTF8(", ")); if (error) throw Fatal(); } if (i % 16 == 15 || i == bData.length() - 1) ResLine(""); } ResLineF(" };"); ResLine(""); ResLineF(" ResourceManager::instance()->set(%s, %s, %d);", stringToCStringLiteral(sPath).ptr(), sId.ptr(), bData.length()); if (iCount != iNumResources) ResLine(""); } } ResLine("}"); #undef ResLine return 0; } catch (Fatal&) { if (error) logerr("%s", UTF16(error).ptr()); return 1; } }
* 02110-1301 USA * * --------------------------------------------------------------------- */ #include <KarenCore/map.h> #include <KarenCore/test.h> using namespace karen; KAREN_BEGIN_UNIT_TEST(MapTestSuite); KAREN_DECL_TEST(shouldCreateAnEmptyTreeMap, { TreeMap<String, int> d; assertTrue(d.isEmpty()); assertEquals<int>(0, d.size()); }); KAREN_DECL_TEST(shouldInsertOneElement, { TreeMap<String, int> d; d.put("Mark", 45); assertFalse(d.isEmpty()); assertEquals<int>(1, d.size()); assertTrue(d.hasKey("Mark")); assertEquals<int>(45, d["Mark"]); }); KAREN_DECL_TEST(shouldInsertOneElementUsingIndexingOperator,