From: <sp...@us...> - 2004-02-26 20:23:41
|
Update of /cvsroot/rtk/rtk/test/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14933/test/core Added Files: dlist0.cpp Log Message: Added tamplate based double linked list --- NEW FILE: dlist0.cpp --- #include <rtk/DList.h> #include "../test.h" typedef IntDList::Iterator IntIter; typedef IntDList::Filter<int> IntFilter; // a function which prints an int list void print(IntDList& list) { PRINTF(_R("list = ")); IntIter* iter = list.GetIterator(); int v; while (v = iter->Next()) rprintf(_R(" %d"), v); rprintf(_R("\n")); delete iter; } bool is_odd(int num, int) { return (num & 1); } // a function which prints odd numbers void print_odd(IntDList& list) { PRINTF(_R("odds = ")); IntFilter* iter = list.GetFilter<int>(is_odd); int v; while (v = iter->Next()) rprintf(_R(" %d"), v); rprintf(_R("\n")); delete iter; } // A simple class to track (de)allocations class A { public: A() { PRINTF(_R("A::A\n")); } ~A() { PRINTF(_R("A::~A\n")); } }; typedef DListP<A*> DListA; // A simple function to search a substring static bool FindSubStr(RCHAR* str, RCHAR* sub) { return (rstrstr(str, sub) != NULL); } static bool fn_less(int a, int b) { return a < b; } int main(int argc, char* argv[]) { TITLE(_R("Test for the DList template")); rprintf(_R("### Int list ###\n")); STEP(IntDList li); STEP(li.Push(1)); STEP(li.Push(2)); STEP(li.Push(3)); STEP(li.Push(4)); STEP(print(li)); OUT("Pop: %d", li.Pop()); OUT("Pop: %d", li.Pop()); STEP(li.Queue(11)); STEP(li.Queue(12)); OUT("Dequeue: %d", li.Dequeue()); OUT("Dequeue: %d", li.Dequeue()); STEP(print(li)); STEP(li.Add(5, fn_less)); STEP(li.Add(21, fn_less)); STEP(li.Add(17, fn_less)); STEP(li.Add(8, fn_less)); STEP(print(li)); STEP(print_odd(li)); rprintf(_R("### Object list ###\n")); DListA* la = new DListA(); STEP(la->Push(new A())); STEP(la->Push(new A())); STEP(la->Push(new A())); STEP(A* tmp = la->Pop()); STEP(delete la); STEP(delete tmp); return 1; } |