// This method gets called when ever there was a MISS or Cache need // to be reorganized in case if it reached to its capacity to hold entries. void set(Key key, Value data) { Entry<Key,Value>* node = hash[key]; if(node) { // refresh the link list entries->remove(node); node->data = data; entries->addInFront(node); } else{ if ( cacheFull() ){ node = entries->tail->prev; entries->remove(node); hash.erase(node->key); node->key = key; node->data = data; hash[key] = node; entries->addInFront(node); } else{ node = new Entry<Key,Value>; node->key = key; node->data = data; hash[key] = node; entries->addInFront(node); } } }
int main(int argc, char* argv[]) { DList dl; dl.reverse(); dl.push_back(200); dl.push_back(300); std::cout << dl; dl.push_front(100); std::cout << dl; dl.reverse(); std::cout << dl; // Let compiler figure out the type for dl2 based on expression const decltype(dl) dl2 {10, 20, 30, 40, 50}; std::cout << dl2; //dl2.push_front(0); decltype(dl) dl3 = {1000, 2000, 3000, 4000, 5000}; dl3.reverse(); std::cout << dl3; return 0; }
void test(){ DList d; d.PushFront(1); d.PushFront(2); d.PushFront(3); d.PushFront(4); d.PushFront(5); cout << d; d.Reverse(); cout << d; //d.Sort(); //cout << d; //Node *ret = d.Find(4); //d.Insert(ret, 5); //cout << d; //d.ReMoveALL(3); //cout << d; //d.PopFront(); //cout << d; //d.PopFront(); //cout << d; //d.PopFront(); //cout << d; //d.PopFront(); //cout << d; }
void SIDCacheFlush() { for(SIDCacheItem** i=SIDCache.First();i;i=SIDCache.Next(i)) { delete *i; } SIDCache.Clear(); }
int main() { DList<int> list; int array[BUFF] = {1,3,2,4,7,5,6,9,0,8}; for(const auto &i : array) list.push_back(i); list.print(); return 0; }
const wchar_t* GetNameFromSIDCache(PSID Sid) { LPCWSTR Result=nullptr; for(SIDCacheItem** i=SIDCache.First();i;i=SIDCache.Next(i)) { if (EqualSid((*i)->Sid,Sid)) { Result=(*i)->strUserName; break; } } return Result; }
DList<T>& DList<T>::copy() { first(); DList<T> copyList; Node<T> *tmp = first; while(tmp) { copyList.insert(temp->item); temp = temp->next; } return copyList; }
void test_1() { DList<int> list; int i1 = 1; int i2 = 2; int i3 = 3; int i4 = 1; int i5 = 2; int i6 = 3; list.append(i1); list.append(i2); list.append(i3); list.prepend(i4); list.prepend(i5); list.prepend(i6); }
int main() { DList<int> Lista; //insertamos elementos for(int i = 0; i < 10; i++) { Lista.insertFirst(i); } //Utilizamos el iterador cout<<"Valores lista"<<endl; for(DList<int>::Iterator it(Lista);it.hasCurrent();it.next()) { cout<<it.getCurrent()->getData()<<" "; } cout<<endl; cout<<"Valores menor de la lista"<<endl; cout<<searchMin(Lista)<<endl; //simpleSort(Lista); //quickSort(Lista); //mergeSort(Lista); sort(Lista); cout<<"Valores lista ordenada"<<endl; for(DList<int>::Iterator it(Lista);it.hasCurrent();it.next()) { cout<<it.getCurrent()->getData()<<" "; } cout<<endl; DList<int> lista2; //probamos el intercambio entre listas lista2.swap(Lista); //probamos la asignacion estre listas Lista = lista2; if(lista2 == Lista) { cout<<"Listas son iguales"<<endl; } //Probamos los [] sobre la lista //OJO esta operacion hace muy lento el acceso a la misma for(int i = 0;i < Lista.getSize();i++) { cout<<Lista[i]<<" "; } cout<<endl; return 0; }
void test_4() { DList<int> list; DListIterator<int> iterator; DListIterator<int> iteratorEnd; int i1 = 1; int i2 = 2; int i3 = 3; int i4 = 1; int i5 = 2; int i6 = 3; int i; list.append(i1); list.append(i2); list.append(i3); list.prepend(i4); list.prepend(i5); list.prepend(i6); list.begin(iterator); list.end(iteratorEnd); while (iterator != iteratorEnd) { i = *iterator; printf("%d\n", i); iterator++; } }
// It does the respective part. It checks if the requested key is in the // cache lookup table, if it finds, which is a HIT, it returns the userName // if it was a MISS, it gets the data from the external data source which // could be a database or file in real time, but an In Memory map in our case // and updates the map with set method. Value get(Key key) { prnt(); if(hash.count(key)==1) { Entry<Key,Value>* node = hash[key]; cout << "<HIT> --> requested UserID found in Cache."; entries->remove(node); entries->addInFront(node); return node->data; } else{ // not found in cache, read from external source and create entry cout << "<MISS> --> UserID not found in Cache, Reading from Memory\n"; string userName = externalData.getUserByID(key); if (userName.length() > 0){ set(key, userName); } return userName; } }
void boogie( DList< int > &dl, DList< int > &ans ) { if( !dl.size ) { for( Node< int > *i = ans.first; i; i = i->r ) printf( " %d", i->v ); puts( "" ); return; } Node< int > *i = dl.first; while( i ) { dl.erase( i ); ans.push_back( i->v ); boogie( dl, ans ); ans.pop_back(); dl.restore( i ); i = i->r; } }
int main() { cout << "=============================== PROGRAMACIO II ================================" << endl; cout << endl; cout << endl; DList<int> list; list.PushBack(1); list.PushBack(2); list.PushBack(3); list.PushBack(4); cout << "Count: " << list.Count() << endl; cout << "First: " << list.GetFirst()->data << endl; cout << endl; cout << endl; cout << "===============================================================================" << endl; system("pause"); return 0; }
const wchar_t* AddSIDToCache(const wchar_t *Computer,PSID Sid) { LPCWSTR Result=nullptr; SIDCacheItem* NewItem=new SIDCacheItem(Computer,Sid); if(NewItem->strUserName.IsEmpty()) { delete NewItem; } else { Result=(*SIDCache.Push(&NewItem))->strUserName; } return Result; }
int main(int argc, const char * argv[]) { DList<int> myList; //for(int i = 0; i < 10; ++i) // myList.addFront(i); myList.addBack(50); myList.addBack(100); cout << myList.getFront() << endl; cout << myList.getBack() << endl; while(!myList.isEmpty()) cout << myList.removeFront() << endl; cout << myList.getSize() << endl; return 0; }
int main() { DList list; list.AddHead(3); list.AddHead(2); list.AddHead(1); list.AddTail(5); list.AddTail(6); list.AddTail(7); list.InsertAfter(3, 4); list.showReverse(); return 0; }
int main() { DList<int> list; srand((unsigned)time(NULL)); for(int i = 0 ; i < 5 ; ++i) { list.findWithIndex(i); list.insertNext(rand() % 50); } std::cout << list.getSize() << std::endl; std::cout << "Before Sorting" << std::endl; list.printAll(); list.sorting(comp); std::cout << "\nAfter Sorting" << std::endl; list.printAll(); return 0; }
void UtilTestCase::testDList() { DList<int> l; CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); CPPUNIT_ASSERT(l.removeFirst() == NULL); CPPUNIT_ASSERT(l.removeLast() == NULL); DLink<int> n1(1); CPPUNIT_ASSERT(n1.get() == 1); DLink<int> n2(2); CPPUNIT_ASSERT(n2.get() == 2); n2.set(3); CPPUNIT_ASSERT(n2.get() == 3); // 测试addFirst和addLast l.addFirst(&n1); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getList() == &l); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); l.addLast(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == &n2); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getList() == &l); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == &n1); // 测试moveToFirst和moveToLast l.moveToFirst(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == &n2); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == &n1); l.moveToLast(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == &n2); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == &n1); // 测试removeFirst CPPUNIT_ASSERT(l.removeFirst() == &n1); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); CPPUNIT_ASSERT(l.removeFirst() == &n2); CPPUNIT_ASSERT(n2.getList() == NULL); CPPUNIT_ASSERT(n2.getNext() == NULL); CPPUNIT_ASSERT(n2.getPrev() == NULL); CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); // 测试removeLast l.addLast(&n1); n1.addAfter(&n2); CPPUNIT_ASSERT(l.removeLast() == &n2); CPPUNIT_ASSERT(n2.getList() == NULL); CPPUNIT_ASSERT(n2.getNext() == NULL); CPPUNIT_ASSERT(n2.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(l.removeLast() == &n1); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); // 测试DLink::unLink l.addLast(&n1); n1.addAfter(&n2); n1.unLink(); n1.unLink(); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); }
MacroRecord* GetCurMacro() { return m_MacroQueue.Empty() ? nullptr : m_MacroQueue.First(); }
int main( int argc, char* argv[] ) { // verify argument if ( argc != 2 ) { cerr << "usage: statistics size" << endl; return -1; } // verify size int size = atoi( argv[1] ); if ( size < PATTERN_MAX ) { cerr << "usage: size >= " << PATTERN_MAX << endl; return -1; } // initialize list items srand( 1 ); int *items = new int[size]; initArray( items, size, -1 ); printArray( items, size, "items" ); // initialize access pattern int *pattern = new int[PATTERN_MAX]; initArray( pattern, PATTERN_MAX, size ); printArray( pattern, PATTERN_MAX, "pattern" ); // initialize pattern frequency int *frequency = new int[PATTERN_MAX]; for ( int i = 1; i < PATTERN_MAX; i++ ) frequency[i] = i + frequency[i - 1]; printArray( frequency, PATTERN_MAX, "frequency" ); // generate access sequence int *sequence = new int[SEQ_MAX]; for ( int i = 0; i < SEQ_MAX; i++ ) { int random = rand( ) % ( frequency[PATTERN_MAX - 1] + 1 ); int hit; for ( hit = 0; hit < PATTERN_MAX; hit++ ) { if ( random <= frequency[hit] ) { break; } } sequence[i] = items[pattern[hit]]; } printArray( sequence, SEQ_MAX, "sequence" ); // now conduct performance evaluation // doubly linked list DList<int> dlist; for ( int i = 0; i < size; i++ ) dlist.insert( items[i], i ); for ( int i = 0; i < SEQ_MAX; i++ ) dlist.find( sequence[i] ); cout << "dlist's find cost = " << dlist.getCost( ) << endl; // mtf list MtfList<int> mtflist; for ( int i = 0; i < size; i++ ) mtflist.insert( items[i], i ); for ( int i = 0; i < SEQ_MAX; i++ ) mtflist.find( sequence[i] ); cout << "mtflist's find cost = " << mtflist.getCost( ) << endl; // transpose list TransposeList<int> translist; for ( int i = 0; i < size; i++ ) translist.insert( items[i], i ); for ( int i = 0; i < SEQ_MAX; i++ ) translist.find( sequence[i] ); cout << "translist's find cost = " << translist.getCost( ) << endl; // skip list SList<int> skiplist; for ( int i = 0; i < size; i++ ) skiplist.insert( items[i] ); for ( int i = 0; i < SEQ_MAX; i++ ) skiplist.find( sequence[i] ); cout << "skip's find cost = " << skiplist.getCost( ) << endl; return 0; }
void Delete(HistoryRecord* Item){HistoryList.Delete(Item); ResetPosition(); SaveHistory();}
bool uTest( UnitTest *utest_p) { DList<int> list; list.push_back(5); list.push_back(10); Unit *u = list.first(); UTEST_CHECK( utest_p, u); UTEST_CHECK( utest_p, list.size() == 2); int& f = u->val(); f = 15; int sum = 0; for ( Unit* e = list.first (); e!= 0; e = e->next() ) { sum += e->val(); } UTEST_CHECK( utest_p, sum == 25); u = list.first(); UTEST_CHECK( utest_p, u->next() == list.last() ); UTEST_CHECK( utest_p, u->val() == 15); Unit * last = list.erase( u); UTEST_CHECK( utest_p, last->next() == 0); UTEST_CHECK( utest_p, last->prev() == 0); UTEST_CHECK( utest_p, last == list.first()); UTEST_CHECK( utest_p, last == list.last()); // Test clear list.clear(); UTEST_CHECK( utest_p, list.size() == 0); UTEST_CHECK( utest_p, list.empty() ); UTEST_CHECK( utest_p, list.first() == 0); UTEST_CHECK( utest_p, list.last() == 0); //--- Test insert and reverse list.push_front( 10);// list: 10 list.push_front( 20);// list: 20 10 list.push_front( 40);// list: 40 20 10 u= list.first()->next(); list.insert( u, 30); // list: 40 30 20 10 list.reverse(); // list: 10 20 30 40 UTEST_CHECK( utest_p, list.first()->val() == 10); UTEST_CHECK( utest_p, list.first()->next()->val() == 20); UTEST_CHECK( utest_p, list.first()->next()->next()->val() == 30); UTEST_CHECK( utest_p, list.last()->val() == 40); // list.clear(); UTEST_CHECK(utest_p, list.size() == 0); UTEST_CHECK(utest_p, list.empty()); UTEST_CHECK(utest_p, list.first() == 0); UTEST_CHECK(utest_p, list.last() == 0); for (int i = 0; i < 200; i++) { list.push_back(i); list.push_front(i + 1); } UTEST_CHECK(utest_p, list.size() == 400); UTEST_CHECK(utest_p, list.first()->val() == 200); UTEST_CHECK(utest_p, list.first()->next()->val() == 199); UTEST_CHECK(utest_p, list.last()->val() == 199); for (int i = 0; i < 200; i++) { list.pop_front(); } UTEST_CHECK(utest_p, list.size() == 200); UTEST_CHECK(utest_p, list.first()->val() == 0); UTEST_CHECK(utest_p, list.last()->val() == 199); list.reverse(); UTEST_CHECK(utest_p, list.first()->val() == 199); UTEST_CHECK(utest_p, list.last()->val() == 0); list.erase(list.first()->next()); UTEST_CHECK(utest_p, list.first()->val() == 0); UTEST_CHECK(utest_p, list.first()->next()->val() == 2); list.clear(); UTEST_CHECK(utest_p, list.size() == 0); return utest_p->result(); }
int main () { DList<int> s; int c, age=10, ret=0; char name[16]; c = getchar(); while(c != 'q') { if (c == 'p') { cout << "input name : " << endl; scanf("%s", name); s.xpush(age++, name); s.xprint(s.begin(), s.end()); } else if(c == 'd') { cout << "input del name :" << endl; scanf("%s", name); s.xdelete(s.begin(), s.end(), name); s.xprint(s.begin(), s.end()); } else if(c == 'f') { cout << "input find name :" << endl; scanf("%s", name); ret = s.xfind(s.begin(), s.end(), name); cout << name << "'s age : " << ret << endl; } c=getchar(); } s.xfree(s.begin(), s.end()); return 0; }
// helper to print the cache void print(){ entries->print(); }
int main() { DList <int> *list = new DList<int>(); int a = 34; int b = 35; int c = 36; int d = 37; int e = 38; list->insertOrdered(&c); list->insertOrdered(&e); list->insertOrdered(&b); list->insertOrdered(&d); list->insertOrdered(&a); list->printList(); cout << "First: " << *list->removeFront() << endl; cout << "Last: " << *list->removeBack() << endl; list->findDelete(&c); cout << "Remove Middle element" << endl; list->printList(); cout << "Test removeFront()" << endl; list->removeFront();//two elements left list->removeFront();//one element left list->removeFront();//zero elements left cout << "removeFront() passed" << endl; list->insertBack(&a); list->insertBack(&b); cout << "Test removeBack()" << endl; list->removeBack();//two elements left list->removeBack();//one element left list->removeBack();//zero elements left cout << "removeBack() passed" << endl; cout << "Add all objects in order:" << endl; list->insertOrdered(&c);//36 list->insertOrdered(&e);//38 list->insertOrdered(&a);//34 list->insertOrdered(&d);//37 list->insertOrdered(&b);//35 list->printList(); cout << "Destructor test" << endl; delete list; list = NULL; cout << "All tests passed!" << endl; return 0; }
void RemoveCurMacro() { if (!m_MacroQueue.Empty()) m_MacroQueue.Delete(m_MacroQueue.First()); }