00001
00002
00003
00004
00005
00006
00007
00008 #include <utility/ostream.h>
00009 #include <utility/list.h>
00010
00011 __USING_SYS;
00012
00013 struct Integer1 {
00014 Integer1(int _i) : i(_i), e(this) {}
00015
00016 int i;
00017 List<Integer1>::Element e;
00018 };
00019
00020 struct Integer2 {
00021 Integer2(int _i) : i(_i), e(this) {}
00022
00023 int i;
00024 Ordered_List<Integer2>::Element e;
00025 };
00026
00027 struct Integer3 {
00028 Integer3(int _i) : i(_i), e(this) {}
00029
00030 int i;
00031 Relative_List<Integer3>::Element e;
00032 };
00033
00034 struct Integer4 {
00035 Integer4(int _i, int _s) : e((Integer4 *)_i, _s) {}
00036
00037 Grouping_List<Integer4>::Element e;
00038 };
00039
00040 int main()
00041 {
00042 OStream cout;
00043
00044 cout << "List Utility Test\n";
00045
00046 List<Integer1> l1;
00047 Integer1 i1(1), i2(2), i3(3), i4(4);
00048 cout << "This is a list of integers:\n";
00049 cout << "Inserting the integer " << i1.i << " into the list.\n";
00050 l1.insert_tail(&i1.e);
00051 cout << "Inserting the integer " << i2.i << " into the list.\n";
00052 l1.insert_tail(&i2.e);
00053 cout << "Inserting the integer " << i3.i << " into the list.\n";
00054 l1.insert_tail(&i3.e);
00055 cout << "Inserting the integer " << i4.i << " into the list.\n";
00056 l1.insert_tail(&i4.e);
00057 cout << "The list has now " << l1.size() << " elements.\n";
00058 cout << "Removing the element whose value is " << i2.i << "("
00059 << l1.remove(&i2)->object()->i << ")" << " from the list.\n";
00060 cout << "Removing the list's head (" << l1.remove_head()->object()->i
00061 << ")" << ".\n";
00062 cout << "Removing the element whose value is " << i4.i << "("
00063 << l1.remove(&i4)->object()->i << ")" << " from the list.\n";
00064 cout << "Removing the list's tail (" << l1.remove_tail()->object()->i
00065 << ")" << ".\n";
00066 cout << "The list has now " << l1.size() << " elements.\n";
00067
00068 Ordered_List<Integer2> l2;
00069 Integer2 j1(1), j2(2), j3(3), j4(4);
00070 cout << "This is an ordered list of integers:\n";
00071 cout << "Inserting the integer " << j1.i
00072 << " into the list with order 2.\n";
00073 l2.insert(&j1.e, 2);
00074 cout << "Inserting the integer " << j2.i
00075 << " into the list with order 3.\n";
00076 l2.insert(&j2.e, 3);
00077 cout << "Inserting the integer " << j3.i
00078 << " into the list with order 4.\n";
00079 l2.insert(&j3.e, 4);
00080 cout << "Inserting the integer " << j4.i
00081 << " into the list with order 1.\n";
00082 l2.insert(&j4.e, 1);
00083 cout << "The list has now " << l2.size() << " elements.\n";
00084 cout << "Removing the element whose value is " << j2.i << "("
00085 << l2.remove(&j2)->object()->i << ")" << " from the list.\n";
00086 cout << "Removing the list's head (" << l2.remove_head()->object()->i
00087 << ")" << ".\n";
00088 cout << "Removing the list's head (" << l2.remove_head()->object()->i
00089 << ")" << ".\n";
00090 cout << "Removing the list's tail (" << l2.remove_tail()->object()->i
00091 << ")" << " from the list.\n";
00092 cout << "The list has now " << l2.size() << " elements.\n";
00093
00094 Relative_List<Integer3> l3;
00095 Integer3 k1(1), k2(2), k3(3), k4(4);
00096 cout << "This is an list of integers with relative ordering:\n";
00097 cout << "Inserting the integer " << k1.i
00098 << " into the list with order 2.\n";
00099 l3.insert(&k1.e, 2);
00100 cout << "Inserting the integer " << k2.i
00101 << " into the list with order 3.\n";
00102 l3.insert(&k2.e, 3);
00103 cout << "Inserting the integer " << k3.i
00104 << " into the list with order 4.\n";
00105 l3.insert(&k3.e, 4);
00106 cout << "Inserting the integer " << k4.i
00107 << " into the list with order 1.\n";
00108 l3.insert(&k4.e, 1);
00109 cout << "The list has now " << l3.size() << " elements.\n";
00110 cout << "Removing the element whose value is " << k2.i << "("
00111 << l3.remove(&k2)->object()->i << ")" << " from the list.\n";
00112 cout << "Removing the list's head (" << l3.remove_head()->object()->i
00113 << ")" << ".\n";
00114 cout << "Removing the list's head (" << l3.remove_head()->object()->i
00115 << ")" << ".\n";
00116 cout << "Removing the list's tail (" << l3.remove_tail()->object()->i
00117 << ")" << " from the list.\n";
00118 cout << "The list has now " << l3.size() << " elements.\n";
00119
00120 Grouping_List<int> l4;
00121 Grouping_List<int>::Element m1((int *)0, 2), m2((int *)16, 2), m3((int *)8, 2), m4((int *)24, 2);
00122 Grouping_List<int>::Element * d1, * d2;
00123 cout << "This is a grouping list of integers:\n";
00124 cout << "Inserting the integer group beginning with " << m1.object()
00125 << " and spanning " << m1.size() << " elements into the list.\n";
00126 l4.insert_merging(&m1, &d1, &d2);
00127 if(d1)
00128 cout << "Element whose valeu was " << d1->object() << " merged\n";
00129 if(d2)
00130 cout << "Element whose valeu was " << d2->object() << " merged\n";
00131 cout << "Inserting the integer group beginning with " << m2.object()
00132 << " and spanning " << m2.size() << " elements into the list.\n";
00133 l4.insert_merging(&m2, &d1, &d2);
00134 if(d1)
00135 cout << "Element whose valeu was " << d1->object() << " merged\n";
00136 if(d2)
00137 cout << "Element whose valeu was " << d2->object() << " merged\n";
00138 cout << "Inserting the integer group beginning with " << m3.object()
00139 << " and spanning " << m3.size() << " elements into the list.\n";
00140 l4.insert_merging(&m3, &d1, &d2);
00141 if(d1)
00142 cout << "Element whose valeu was " << d1->object() << " merged\n";
00143 if(d2)
00144 cout << "Element whose valeu was " << d2->object() << " merged\n";
00145 cout << "Inserting the integer group beginning with " << m4.object()
00146 << " and spanning " << m4.size() << " elements into the list.\n";
00147 l4.insert_merging(&m4, &d1, &d2);
00148 if(d1)
00149 cout << "Element whose valeu was " << d1->object() << " merged\n";
00150 if(d2)
00151 cout << "Element whose valeu was " << d2->object() << " merged\n";
00152 cout << "The list has now " << l4.size() << " elements that group "
00153 << l4.grouped_size() << " elements in total.\n";
00154 cout << "Removing one element from the list: ";
00155 d1 = l4.search_decrementing(1);
00156 cout << d1->object() + d1->size() << ".\n";
00157 if(!d1->size())
00158 cout << "Element whose valeu was " << d1->object() << " deleted\n";
00159 cout << "Allocating six more elements from the list: ";
00160 d1 = l4.search_decrementing(6);
00161 cout << d1->object() + d1->size() << ".\n";
00162 if(!d1->size())
00163 cout << "Element whose valeu was " << d1->object() << " deleted\n";
00164 cout << "Allocating one element from the list: ";
00165 d1 = l4.search_decrementing(1);
00166 cout << d1->object() + d1->size() << ".\n";
00167 if(!d1->size())
00168 cout << "Element whose valeu was " << d1->object() << " deleted\n";
00169 cout << "The list has now " << l4.size() << " elements that group "
00170 << l4.grouped_size() << " elements in total.\n";
00171
00172 return 0;
00173 }