// IAPS - Image analysis program system // // File name : exDList.cc // Description: an example of usage of the double-linked list // Last change: 22.08.1994 // Author : Radek Marik // Libraries : Mlist, Mstd // ------------------------------------------------------------------ // // // Modifications: // // // // This example is designed to test the classes of double-linked // list containing data. That means reference // counted list and its iterators. #include // cout #include "amma/DList.hh" // list #include "amma/CDLIter.hh" // iterator of constant list #include "amma/DLIter.hh" // iterator of list typedef DListC IntListC; // abbreviations typedef ConstDLIterC IntCIterC; typedef DLIterC IntIterC; int main(int argc, char * []) //=========================== { if (argc>1) { // Check if AMMA_CHECK works properly // ================================== IntListC ll1; // an empty list cerr << "imposible: " << ll1[0] << '\n'; return 0; } // Body of double-linked list // ========================== IntListC list; // create a list // put in some elements list.InsLast(1) .InsLast(2) .InsLast(3) .InsLast(4) .InsLast(5); // list operations without an iterator cout << "Double-linked list\n" << list << '\n' << "Copy the list\n" << list.Copy() << '\n' << "First item: " << list.First() << '\n' << "Last item: " << list.Last() << '\n' << "The first item (i=0):" << list[0] << '\n' << "Second item (i=1): " << list[1] << '\n' << "Get the first item: " << list.GetFirst() << '\n' << "Get the last item: " << list.GetLast() << '\n' << "Delete the 1. item:\n" << list.DelFirst() << '\n' << "Delete the last item:\n" << list.DelLast() << '\n' << "Insert first (10):\n" << list.InsFirst(10) << '\n' << "Insert last (20):\n" << list.InsLast(20) << '\n'; // combining of lists IntListC cList; // make another list cList.InsFirst(7).InsLast(7); cout << "Insert a copy of the second list to the end:\n" << (list += cList) << '\n' << "Move a list to the beginning:\n" << list.MoveFirst(cList) << '\n' << "Reverse list\n" << list.Reverse() << '\n' << "Empty list\n" << list.Empty() << '\n'; // Operations with constant iterator // ================================= list.InsLast(1) .InsLast(2) .InsLast(3) .InsLast(4) .InsLast(5); // make a list IntCIterC i(list); // create an iterator // i.Data() = 4; // *** Compiler Error code: // assignment of read-only location cout << "List\n" << i.List() << '\n' << "Last item: " << i.Last().Data() << '\n' << "First item: " << i.First().Data() << '\n' << "Second item: " << i.Next().Data() << '\n' << "Step back: " << i.Prev().Data() << '\n' << "Crc step back: " << i.PrevCrc().Data() << '\n' << "Crc step forward: " << i.NextCrc().Data() << '\n' << "The 4-th item (i=3):" << i.Nth(3).Data() << '\n' << "2 steps back: " << i.RelNth(-2).Data() << '\n' << "Current position: " << i.Index() << '\n' << "Is it first? " << i.IsFirst() << '\n' << "Is is last? " << i.IsLast() << '\n' << "Can it access data? " << i.IsElm() << '\n' << "Are they same? " << (i == IntCIterC(list)) << '\n' << "Are they different? " << (i != i.Copy().Last()) << '\n' << "\n\n"; // Do something for the whole list ... add items to another list for (i.First(); i.IsElm(); i.Next()) cList.InsLast(i.Data()+10); cout << "Derived list\n" << cList << '\n'; // Operations with parts of the list. // ================================== IntListC c1List(cList.Copy()); // another list cout << "The current list:\n" << list << '\n' << "Move head before 3. el.:\n" << list.MakeFirst(i.Nth(2)) << '\n' << "Move head after 3. el.:\n" << list.MakeLast(i.Nth(2)) << '\n' << "Move 3.el to be 1. el.:\n" << list.MoveFirst(i.Nth(2)) << '\n' << "Move 3.el to be the last el.:\n" << list.MoveLast(i.Nth(2)) << '\n' << "Head of the list(..-2): \n" << list.Head(i.Nth(2)) << '\n' << "Tail of the list(3-..): \n" << list.Tail(i.Nth(2)) << '\n' << "Current list:\n" << list << '\n' << "Move list to be first\n" << list.MoveFirst(cList) << '\n' << "Concatenation:\n" << (list += c1List) << '\n'; // Operations with non-constant iterator // ===================================== list.Tail(i.Nth(6)); // remove the tail of the list to be short IntIterC j(list); // create an iterator j.Data() = 100; cout << "List\n" << j.List() << '\n' << "Last item: " << j.Last().Data() << '\n' << "First item: " << j.First().Data() << '\n' << "Second item: " << j.Next().Data() << '\n' << "Step back: " << j.Prev().Data() << '\n' << "Crc step back: " << j.PrevCrc().Data() << '\n' << "Crc step forward: " << j.NextCrc().Data() << '\n' << "The 4-th item (i=3):" << j.Nth(3).Data() << '\n' << "2 steps back: " << j.RelNth(-2).Data() << '\n' << "Current position: " << j.Index() << '\n' << "Is it first? " << j.IsFirst() << '\n' << "Is is last? " << j.IsLast() << '\n' << "Can it access data? " << j.IsElm() << '\n' << "Are they same? " << (j == IntCIterC(list)) << '\n' << "Are they different? " << (j != j.Copy().Last()) << '\n' << "\n\n"; // Do something for the whole list ... add items to another list for (j.First(); j.IsElm(); j.Next()) cList.InsLast(j.Data()+10); cout << "Derived list\n" << cList << '\n'; // Operations with parts of the list. // ================================== IntListC c2List(cList.Copy()); // another list j.Nth(1); // set the iterator to point to the 3.element cout << "The current list:\n" << list << '\n' << "The current element: " << j.Data() << '\n' << "Move 3.el after iterator.:\n" << j.MoveAft(j.Copy().Nth(2)).List() << '\n' << "Move 3.el before iterator:\n" << j.MoveBef(j.Copy().Nth(2)).List() << '\n' << "Delete current element:\n" << j.Del().List() << '\n' << "Delete current el. & next:\n"<< j.DelMoveNext().List() << '\n' << "Insert after current el.:\n" << j.InsAft(33).List() << '\n' << "Insert before curr. elm.:\n" << j.InsBef(44).List() << '\n' << "Move head before c. el.:\n" << list.MakeFirst(j) << '\n' << "Move head after c. el.:\n" << list.MakeLast(j) << '\n' << "Move c.el to be 1. el.:\n" << list.MoveFirst(j) << '\n' << "Move c.el to be the last el.:\n" << list.MoveLast(j) << '\n' << "Head of the list(..-1): \n" << list.Head(j.Nth(1)) << '\n' << "Tail of the list(2-..): \n" << list.Tail(j.Nth(1)) << '\n' << "Current list:\n" << list << '\n' << "Move list to be first\n" << list.MoveFirst(cList) << '\n' << "Concatenation:\n" << (list += c2List) << '\n'; // Sharing of lists // ================ list.Tail(j.Nth(5)); // throw away the tail of the list to be shorter IntListC c3List(list); // another handle of the list cout << "Current list\n" << list << '\n' << "The same list\n" << c3List << '\n' << "The first list changed\n" << list.DelFirst() << '\n' << "The second list\n" << c3List << '\n'; // Complex list // ============ DListC< DListC > superList; superList.InsLast(list.Copy()).InsLast(list).InsLast(c3List); cout << "List of list of integers:\n" << superList << '\n'; cout << "End of Example.\n" << "===============\n"; return(0); } // IAPS - Image analysis program system. // End of file exDList.cc