/* test/demo of generic lists */ #include #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \ else _assert(#ex, __FILE__,__LINE__); } #include #include "list.h" #include "algo.h" bool int_compare(int a, int b) { return a < b; } int inc(int x) { return x + 1; } void print(list& l) { for (list::iterator it = l.begin(); it != l.end(); it++) cout << *it << " "; cout << "\n"; } int is_odd(int x) { return x & 1; } int is_even(int x) { return (x & 1) == 0; } void sequence(list& a, int lo, int hi) { back_insert_iterator > it(a); while (lo <= hi) *it++ = lo++; } int old_rand = 9999; int get_rand() { old_rand = ((long)old_rand * (long)1243) % (long)971; return old_rand; } void randseq(list& a, int n) { back_insert_iterator > it(a); while (--n >= 0) *it++ = get_rand() % 50; } int array1 [] = { 9, 16, 36 }; int array2 [] = { 1, 4 }; int test_splice () { list l1 (array1, array1 + 3); list l2 (array2, array2 + 2); list::iterator i1 = l1.begin (); l1.splice (i1, l2); list::iterator i2 = l1.begin (); while (i2 != l1.end ()) cout << *i2++ << endl; return 0; } main() { list a; int i; list::iterator it, bit; sequence(a, 1, 20); cout << "\nlist a = sequence(1, 20);\n"; print(a); for (it = a.begin (), i = 0; it != a.end (); it++, i++) assert (*it == i + 1); list b; randseq(b, 20); cout << "\nlist b = randseq(20);\n"; print(b); list c; c.insert (c.end(), a.begin(), a.end()); c.insert (c.end(), b.begin(), b.end()); cout << "\nlist c = a and b;\n"; print(c); list d; for (it = a.begin(); it != a.end(); it++) d.insert(d.end (), inc(*it)); cout << "\nlist d = map(inc, a);\n"; print(d); list e; back_insert_iterator > e_insertor (e); reverse_copy (a.begin(), a.end (), e_insertor); cout << "\nlist e = reverse(a);\n"; print(e); list f; for (it = a.begin(); it != a.end(); it++) if (is_odd (*it)) f.insert(f.end (), *it); cout << "\nlist f = select(is_odd, a);\n"; print(f); list ff; for (it = f.begin(); it != f.end(); it++) if (is_even (*it)) ff.insert(ff.end (), *it); assert(ff.empty()); int red = 0; for (it = a.begin(); it != a.end(); it++) red += *it; cout << "\nint red = a.reduce(plus, 0);\n"; cout << red; it = a.begin(); ++it; ++it; int second = *it; cout << "\nint second = a[2];\n"; cout << second; list g; for (it = a.begin(), bit = b.begin(); it != a.end () && bit != b.end (); ) g.insert (g.end (), *it++ + *bit++); cout << "\nlist g = combine(plus, a, b);\n"; print(g); #if 1 for (it = g.begin(); it != g.end(); ) { bit = it++; if (is_odd (*bit)) g.erase (bit); } #else g.remove_if (is_odd); #endif cout << "\ng.del(is_odd);\n"; print(g); ff.erase (ff.begin (), ff.end()); for (it = g.begin(); it != g.end(); it++) if (is_odd (*it)) ff.insert (ff.end (), *it); assert(ff.empty()); b.sort(); for (it = b.begin(); bit = it++, it != b.end (); ) assert (*it >= *bit); cout << "\nb.sort(int_compare);\n"; print(b); list h; back_insert_iterator > h_insertor (h); merge (a.begin (), a.end (), b.begin (), b.end (), h_insertor, int_compare); cout << "\nlist h = merge(a, b, int_compare);\n"; print(h); for (it = h.begin(); bit = it++, it != h.end (); ) assert (*it >= *bit); cout << "\nh via iterator:\n"; for (it = h.begin(); it != h.end (); it++) cout << *it << ", "; cout << "\n"; test_splice (); cout << "\ndone\n"; } template class __malloc_alloc_template<0>; template class __default_alloc_template;