17 |
7u83 |
1 |
/*
|
|
|
2 |
* Copyright 2019, The PLANIX Project
|
|
|
3 |
*
|
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
|
5 |
* modification, are permitted provided that the following conditions are
|
|
|
6 |
* met:
|
|
|
7 |
*
|
|
|
8 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
9 |
* this list of conditions and the following disclaimer.
|
|
|
10 |
*
|
|
|
11 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
|
14 |
*
|
|
|
15 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
16 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
17 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
18 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
19 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
20 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
21 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
22 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
23 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
24 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
25 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
26 |
*/
|
|
|
27 |
#include <stdio.h>
|
|
|
28 |
#include <string.h>
|
|
|
29 |
#include <errno.h>
|
|
|
30 |
#include <stdlib.h>
|
|
|
31 |
#include <time.h>
|
|
|
32 |
|
|
|
33 |
#include "mavl.h"
|
|
|
34 |
|
|
|
35 |
/*
|
|
|
36 |
* EXAMPLE 1 Create an AVL which stores just integer values.
|
|
|
37 |
*/
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
/* Compare function to compare integers */
|
|
|
41 |
static int
|
|
|
42 |
cmp(const void *v1, const void *v2)
|
|
|
43 |
{
|
|
|
44 |
return *((int *)v1) - *((int *)v2);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/*
|
|
|
48 |
* Number of integer values we want to put into our tree
|
|
|
49 |
*/
|
24 |
7u83 |
50 |
#define NUMVALS 15000000L
|
40 |
7u83 |
51 |
|
|
|
52 |
void prnode(char *dst, struct mavlnode *n)
|
23 |
7u83 |
53 |
{
|
|
|
54 |
void *data = mavlnode_dataptr(n);
|
|
|
55 |
int v = *((int*)(data));
|
|
|
56 |
(void)sprintf(dst,"%d[%d]",v,n->bal);
|
|
|
57 |
}
|
17 |
7u83 |
58 |
|
40 |
7u83 |
59 |
int vals[] = {
|
|
|
60 |
/* 1,2,3,4,5,6,-1 */
|
|
|
61 |
/* 7,6,5,4,3,2,1,-1 */
|
|
|
62 |
1,10,5,-1
|
|
|
63 |
};
|
17 |
7u83 |
64 |
|
|
|
65 |
|
|
|
66 |
int
|
|
|
67 |
main(void)
|
|
|
68 |
{
|
|
|
69 |
int i;
|
|
|
70 |
int insval;
|
|
|
71 |
int exists;
|
|
|
72 |
struct mavl *t;
|
|
|
73 |
clock_t clk;
|
|
|
74 |
int rc;
|
|
|
75 |
|
|
|
76 |
/*
|
|
|
77 |
* Create a MAVL object for integers.
|
|
|
78 |
*/
|
|
|
79 |
t = mavl_create(cmp, NULL, sizeof(int));
|
|
|
80 |
if (t == NULL) {
|
|
|
81 |
(void)fprintf(stderr,
|
|
|
82 |
"Can't create struct mavl*: %s\n",
|
|
|
83 |
strerror(errno));
|
|
|
84 |
return errno;
|
|
|
85 |
}
|
|
|
86 |
|
295 |
7u83 |
87 |
(void)printf("Inserting %ld sorted intergers! ...\n", NUMVALS);
|
17 |
7u83 |
88 |
clk = clock();
|
40 |
7u83 |
89 |
for (i = 0; i<NUMVALS; i++) {
|
|
|
90 |
insval = i;
|
17 |
7u83 |
91 |
(void)mavl_insert(t, &insval, &exists);
|
|
|
92 |
}
|
|
|
93 |
clk = clock() - clk;
|
|
|
94 |
(void)printf("TIME: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
95 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
96 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
97 |
|
24 |
7u83 |
98 |
(void)printf("Verifying ... ");
|
17 |
7u83 |
99 |
clk = clock();
|
19 |
7u83 |
100 |
rc = mavl_verify(t);
|
17 |
7u83 |
101 |
clk = clock() - clk;
|
|
|
102 |
|
|
|
103 |
if (rc)
|
|
|
104 |
(void)printf("ok\n");
|
|
|
105 |
else
|
|
|
106 |
(void)printf("error\n");
|
|
|
107 |
(void)printf("VERIFYING TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
40 |
7u83 |
108 |
(void)printf("DELETING ALL VALUES ... \n");
|
17 |
7u83 |
109 |
clk = clock();
|
|
|
110 |
mavl_del_all(t);
|
|
|
111 |
clk = clock() - clk;
|
|
|
112 |
(void)printf("DELETION TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
113 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
114 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
115 |
|
|
|
116 |
|
295 |
7u83 |
117 |
(void)printf("Inserting %ld random intergers ...\n", NUMVALS);
|
339 |
7u83 |
118 |
srand((unsigned int)time(NULL));
|
39 |
7u83 |
119 |
/*srand(1941);*/
|
17 |
7u83 |
120 |
|
|
|
121 |
clk = clock();
|
|
|
122 |
for (i = 0; i < NUMVALS; i++) {
|
|
|
123 |
insval = rand();
|
|
|
124 |
(void)mavl_insert(t, &insval, &exists);
|
|
|
125 |
}
|
|
|
126 |
clk = clock() - clk;
|
|
|
127 |
(void)printf("TIME: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
128 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
129 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
130 |
|
|
|
131 |
(void)printf("Verifying ... ");
|
|
|
132 |
clk = clock();
|
19 |
7u83 |
133 |
rc = mavl_verify(t);
|
17 |
7u83 |
134 |
clk = clock() - clk;
|
|
|
135 |
|
|
|
136 |
if (rc)
|
|
|
137 |
(void)printf("ok\n");
|
|
|
138 |
else
|
|
|
139 |
(void)printf("error\n");
|
|
|
140 |
(void)printf("VERIFYING TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
141 |
(void)printf("Deleting all values ... \n");
|
|
|
142 |
clk = clock();
|
|
|
143 |
mavl_del_all(t);
|
|
|
144 |
clk = clock() - clk;
|
|
|
145 |
(void)printf("DELETION TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
146 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
147 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
426 |
7u83 |
148 |
|
|
|
149 |
mavl_destroy(t);
|
17 |
7u83 |
150 |
|
|
|
151 |
return 0;
|
|
|
152 |
}
|