1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
\r
2 * This file is part of clib library
\r
3 * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
\r
32 delete_e ( void *ptr ) {
\r
37 compare_e ( void *left, void *right ) {
\r
38 char *l = (char*)left;
\r
39 char *r = (char*)right;
\r
40 return strcmp ( (const char *)l, (const char *) r );
\r
43 compare_int ( void *left, void *right ) {
\r
44 int *l = (int*)left;
\r
45 int *r = (int*)right;
\r
49 else if ( *l > *r )
\r
56 int test[] = { 0,1,2,3,4,5,6,7,8,9 };
\r
58 int size = sizeof (test) /sizeof(test[0]);
\r
61 struct clib_set *pSet = new_clib_set ( compare_int, NULL);
\r
63 for ( index = 0; index < size; index++ ) {
\r
64 int v = test[index];
\r
65 insert_clib_set ( pSet, &v, sizeof(int));
\r
67 for ( index = 0; index < size; index++ ) {
\r
68 int v = test[index];
\r
69 assert ( clib_true == exists_clib_set ( pSet, &v));
\r
73 find_clib_set ( pSet, &index, &outKey);
\r
74 assert ( 9 == *(int*)outKey);
\r
76 delete_clib_set(pSet);
\r
79 typedef struct test {
\r
88 {"A for APPLE"},{"B for BALL"},{"C for CAT"}, {"D for DOG"},
\r
89 {"E for ELEPHANT"},{"F for FISH"},{"G for GOAT"},
\r
90 {"H for HORSE"},{"I for ICECREAM"},{"J for JOKER"},
\r
91 {"K for KITE"},{"L for LAMB"},{"M for MONKEY"},
\r
92 {"N for NEST"},{"O for ORANGE"},{"P for POT"},
\r
93 {"Q for QUEEN"},{"R for RAT"},{"S for SHEEP"},
\r
94 {"T for TABLE"},{"U for UMBRELLA"},{"V for VIOLIN"},{"W for WAX"},
\r
95 {"X for XEROX"},{"Y for YUMMY"},{"Z for ZEBRA"}
\r
97 struct clib_set *pSet = new_clib_set ( compare_e, delete_e);
\r
98 size = sizeof ( ti ) / sizeof ( ti[0]);
\r
100 for ( index = 0; index < size; index++ ){
\r
101 char *temp = clib_strdup ( ti[index].string );
\r
102 insert_clib_set ( pSet, temp, strlen(temp) + 1 );
\r
105 for ( index = 0; index < size; index++ ){
\r
106 v = ti[index].string;
\r
107 assert ( clib_true == exists_clib_set ( pSet, v));
\r
109 delete_clib_set(pSet);
\r