Dillo v3.1.1-111-gd4f56d0d
Loading...
Searching...
No Matches
containers.cc
Go to the documentation of this file.
1#include "lout/object.hh"
2#include "lout/container.hh"
3
4using namespace lout::object;
5using namespace lout::container::typed;
6
7class ReverseComparator: public Comparator
8{
9private:
10 Comparator *reversed;
11
12public:
13 ReverseComparator (Comparator *reversed) { this->reversed = reversed; }
14 int compare(Object *o1, Object *o2) { return - reversed->compare (o1, o2); }
15};
16
18{
19 puts ("--- testHashSet ---");
20
21 HashSet<String> h(true);
22
23 h.put (new String ("one"));
24 h.put (new String ("two"));
25 h.put (new String ("three"));
26
27 puts (h.toString());
28}
29
31{
32 puts ("--- testHashTable ---");
33
34 HashTable<String, Integer> h(true, true);
35
36 h.put (new String ("one"), new Integer (1));
37 h.put (new String ("two"), new Integer (2));
38 h.put (new String ("three"), new Integer (3));
39
40 puts (h.toString());
41
42 h.put (new String ("one"), new Integer (4));
43 h.put (new String ("two"), new Integer (5));
44 h.put (new String ("three"), new Integer (6));
45
46 puts (h.toString());
47}
48
50{
51 ReverseComparator reverse (&standardComparator);
52
53 puts ("--- testVector (1) ---");
54
55 Vector<String> v (true, 1);
56
57 v.put (new String ("one"));
58 v.put (new String ("two"));
59 v.put (new String ("three"));
60 puts (v.toString());
61
62 v.sort (&reverse);
63 puts (v.toString());
64
65 v.sort ();
66 puts (v.toString());
67}
68
70{
71 puts ("--- testVector (2) ---");
72
73 Vector<String> v (true, 1);
74
75 v.insertSorted (new String ("one"));
76 puts (v.toString());
77
78 v.insertSorted (new String ("two"));
79 puts (v.toString());
80
81 v.insertSorted (new String ("three"));
82 puts (v.toString());
83
84 v.insertSorted (new String ("five"));
85 puts (v.toString());
86
87 v.insertSorted (new String ("six"));
88 puts (v.toString());
89
90 v.insertSorted (new String ("four"));
91 puts (v.toString());
92
93 for (int b = 0; b < 2; b++) {
94 bool mustExist = b;
95 printf ("mustExist = %s\n", mustExist ? "true" : "false");
96
97 String k ("alpha");
98 printf (" '%s' -> %d\n", k.chars(), v.bsearch (&k, mustExist));
99
100 for (Iterator<String> it = v.iterator(); it.hasNext(); ) {
101 String *k1 = it.getNext();
102 printf (" '%s' -> %d\n", k1->chars(), v.bsearch (k1, mustExist));
103
104 char buf[64];
105 strcpy (buf, k1->chars());
106 strcat (buf, "-var");
107 String k2 (buf);
108 printf (" '%s' -> %d\n", k2.chars(), v.bsearch (&k2, mustExist));
109 }
110 }
111}
112
114{
115 // Regression test: resulted once incorrently (0, 2, 3), should
116 // result in (1, 2, 3).
117
118 puts ("--- testVector (3) ---");
119
120 Vector<String> v (true, 1);
121 String k ("omega");
122
123 v.put (new String ("alpha"));
124 printf (" -> %d\n", v.bsearch (&k, false));
125 v.put (new String ("beta"));
126 printf (" -> %d\n", v.bsearch (&k, false));
127 v.put (new String ("gamma"));
128 printf (" -> %d\n", v.bsearch (&k, false));
129}
130
132{
133 puts ("--- testStackAsQueue ---");
134
135 Stack<Integer> s (true);
136
137 for (int i = 1; i <= 10; i++)
138 s.pushUnder (new Integer (i));
139
140 while (s.size () > 0) {
141 Integer *i = s.getTop ();
142 printf ("%d\n", i->getValue ());
143 s.pop ();
144 }
145}
146
147int main (int argc, char *argv[])
148{
149 testHashSet ();
150 testHashTable ();
151 testVector1 ();
152 testVector2 ();
153 testVector3 ();
155
156 return 0;
157}
int main(void)
Definition bookmarks.c:1613
Typed version of container::untyped::HashSet.
Definition container.hh:514
Typed version of container::untyped::HashTable.
Definition container.hh:536
void put(K *key, V *value)
Definition container.hh:542
Typed version of container::untyped::Iterator.
Definition container.hh:395
Typed version of container::untyped::Stack.
Definition container.hh:552
Typed version of container::untyped::Vector.
Definition container.hh:447
void put(T *newElement, int newPos=-1)
Definition container.hh:452
int bsearch(T *key, bool mustExist, int start, int end, object::Comparator *comparator=&object::standardComparator)
Definition container.hh:468
void sort(object::Comparator *comparator=&object::standardComparator)
Definition container.hh:465
int insertSorted(T *newElement, object::Comparator *comparator=&object::standardComparator)
Definition container.hh:456
Used for other orders as the one defined by Comparable.
Definition object.hh:67
virtual int compare(Object *o1, Object *o2)=0
Compare two objects o1 and o2.
const char * chars()
Definition object.hh:174
An object::Object wrapper for int's.
Definition object.hh:127
This is the base class for many other classes, which defines very common virtual methods.
Definition object.hh:25
const char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition object.cc:82
An object::Object wrapper for strings (char*).
Definition object.hh:186
void testStackAsQueue()
void testVector2()
Definition containers.cc:69
void testVector1()
Definition containers.cc:49
void testVector3()
void testHashTable()
Definition containers.cc:30
void testHashSet()
Definition containers.cc:17
This namespace provides thin wrappers, implemented as C++ templates, to gain type-safety.
Definition container.hh:387
Here, some common classes (or interfaces) are defined, to standardize the access to other classes.
Definition object.cc:30
StandardComparator standardComparator
Definition object.cc:149