Dillo v3.2.0-88-g47ab7c70
Loading...
Searching...
No Matches
containers.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
5 * Copyright 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "lout/object.hh"
22#include "lout/container.hh"
23
24using namespace lout::object;
25using namespace lout::container::typed;
26
27class ReverseComparator: public Comparator
28{
29private:
30 Comparator *reversed;
31
32public:
33 ReverseComparator (Comparator *reversed) { this->reversed = reversed; }
34 int compare(Object *o1, Object *o2) { return - reversed->compare (o1, o2); }
35};
36
38{
39 puts ("--- testHashSet ---");
40
41 HashSet<String> h(true);
42
43 h.put (new String ("one"));
44 h.put (new String ("two"));
45 h.put (new String ("three"));
46
47 char *p;
48 puts (p = h.toString());
49 dFree(p);
50}
51
53{
54 puts ("--- testHashTable ---");
55
56 HashTable<String, Integer> h(true, true);
57
58 h.put (new String ("one"), new Integer (1));
59 h.put (new String ("two"), new Integer (2));
60 h.put (new String ("three"), new Integer (3));
61
62 char *p;
63 puts (p = h.toString());
64 dFree(p);
65
66 h.put (new String ("one"), new Integer (4));
67 h.put (new String ("two"), new Integer (5));
68 h.put (new String ("three"), new Integer (6));
69
70 puts (p = h.toString());
71 dFree(p);
72}
73
75{
76 ReverseComparator reverse (&standardComparator);
77
78 puts ("--- testVector (1) ---");
79
80 Vector<String> v (true, 1);
81
82 char *p;
83
84 v.put (new String ("one"));
85 v.put (new String ("two"));
86 v.put (new String ("three"));
87 puts (p = v.toString());
88 dFree(p);
89
90 v.sort (&reverse);
91 puts (p = v.toString());
92 dFree(p);
93
94 v.sort ();
95 puts (p = v.toString());
96 dFree(p);
97}
98
100{
101 puts ("--- testVector (2) ---");
102
103 Vector<String> v (1, true);
104 char *p;
105
106 v.insertSorted (new String ("one"));
107 puts (p = v.toString());
108 dFree(p);
109
110 v.insertSorted (new String ("two"));
111 puts (p = v.toString());
112 dFree(p);
113
114 v.insertSorted (new String ("three"));
115 puts (p = v.toString());
116 dFree(p);
117
118 v.insertSorted (new String ("five"));
119 puts (p = v.toString());
120 dFree(p);
121
122 v.insertSorted (new String ("six"));
123 puts (p = v.toString());
124 dFree(p);
125
126 v.insertSorted (new String ("four"));
127 puts (p = v.toString());
128 dFree(p);
129
130 for (int b = 0; b < 2; b++) {
131 bool mustExist = b;
132 printf ("mustExist = %s\n", mustExist ? "true" : "false");
133
134 String k ("alpha");
135 printf (" '%s' -> %d\n", k.chars(), v.bsearch (&k, mustExist));
136
137 for (Iterator<String> it = v.iterator(); it.hasNext(); ) {
138 String *k1 = it.getNext();
139 printf (" '%s' -> %d\n", k1->chars(), v.bsearch (k1, mustExist));
140
141 char buf[64];
142 strcpy (buf, k1->chars());
143 strcat (buf, "-var");
144 String k2 (buf);
145 printf (" '%s' -> %d\n", k2.chars(), v.bsearch (&k2, mustExist));
146 }
147 }
148}
149
151{
152 // Regression test: resulted once incorrectly (0, 2, 3), should
153 // result in (1, 2, 3).
154
155 puts ("--- testVector (3) ---");
156
157 Vector<String> v (true, 1);
158 String k ("omega");
159
160 v.put (new String ("alpha"));
161 printf (" -> %d\n", v.bsearch (&k, false));
162 v.put (new String ("beta"));
163 printf (" -> %d\n", v.bsearch (&k, false));
164 v.put (new String ("gamma"));
165 printf (" -> %d\n", v.bsearch (&k, false));
166}
167
169{
170 puts ("--- testStackAsQueue ---");
171
172 Stack<Integer> s (true);
173
174 for (int i = 1; i <= 10; i++)
175 s.pushUnder (new Integer (i));
176
177 while (s.size () > 0) {
178 Integer *i = s.getTop ();
179 printf ("%d\n", i->getValue ());
180 s.pop ();
181 }
182}
183
184int main (int argc, char *argv[])
185{
186 testHashSet ();
187 testHashTable ();
188 testVector1 ();
189 testVector2 ();
190 testVector3 ();
192
193 return 0;
194}
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:87
virtual int compare(Object *o1, Object *o2)=0
Compare two objects o1 and o2.
const char * chars()
Definition object.hh:194
An object::Object wrapper for int's.
Definition object.hh:147
This is the base class for many other classes, which defines very common virtual methods.
Definition object.hh:45
char * toString()
Use object::Object::intoStringBuffer to return a textual representation of the object.
Definition object.cc:83
An object::Object wrapper for strings (char*).
Definition object.hh:206
void testStackAsQueue()
void testVector2()
Definition containers.cc:99
void testVector1()
Definition containers.cc:74
void testVector3()
void testHashTable()
Definition containers.cc:52
void testHashSet()
Definition containers.cc:37
void dFree(void *mem)
Definition dlib.c:68
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:31
StandardComparator standardComparator
Definition object.cc:149