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 |
*/
|
19 |
7u83 |
50 |
#define NUMVALS 15000000L
|
17 |
7u83 |
51 |
/* #define NUMVALS 60L */
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
void print(struct mavlnode *n, int dir)
|
|
|
55 |
{
|
|
|
56 |
int v;
|
|
|
57 |
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
static int
|
|
|
61 |
verify(struct mavl *t)
|
|
|
62 |
{
|
|
|
63 |
int prevval, val;
|
|
|
64 |
struct mavliter it;
|
|
|
65 |
void *data;
|
|
|
66 |
|
|
|
67 |
prevval = -1;
|
|
|
68 |
|
|
|
69 |
mavliter_init(&it, t);
|
|
|
70 |
(void)mavliter_seek_set(&it);
|
|
|
71 |
|
|
|
72 |
/* print out all integers stored in the MAVL tree */
|
|
|
73 |
while ((data = mavliter_get(&it)) != NULL) {
|
|
|
74 |
val = *((int *)data);
|
|
|
75 |
/* Check if values are sorted */
|
|
|
76 |
if (val <= prevval) {
|
|
|
77 |
return 0;
|
|
|
78 |
}
|
|
|
79 |
prevval = val;
|
|
|
80 |
(void)mavliter_next(&it);
|
|
|
81 |
}
|
|
|
82 |
return 1;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
int
|
|
|
87 |
main(void)
|
|
|
88 |
{
|
|
|
89 |
int i;
|
|
|
90 |
int insval;
|
|
|
91 |
int exists;
|
|
|
92 |
struct mavl *t;
|
|
|
93 |
clock_t clk;
|
|
|
94 |
int rc;
|
|
|
95 |
|
|
|
96 |
/*
|
|
|
97 |
* Create a MAVL object for integers.
|
|
|
98 |
*/
|
|
|
99 |
t = mavl_create(cmp, NULL, sizeof(int));
|
|
|
100 |
if (t == NULL) {
|
|
|
101 |
(void)fprintf(stderr,
|
|
|
102 |
"Can't create struct mavl*: %s\n",
|
|
|
103 |
strerror(errno));
|
|
|
104 |
return errno;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
(void)printf("Inserting %d sorted intergers ...\n", NUMVALS);
|
|
|
108 |
clk = clock();
|
|
|
109 |
for (i = 0; i < NUMVALS; i++) {
|
|
|
110 |
insval = NUMVALS-i;
|
|
|
111 |
(void)mavl_insert(t, &insval, &exists);
|
|
|
112 |
}
|
|
|
113 |
clk = clock() - clk;
|
|
|
114 |
(void)printf("TIME: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
115 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
116 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
117 |
|
|
|
118 |
(void)printf("Verifying ... ");
|
|
|
119 |
clk = clock();
|
19 |
7u83 |
120 |
rc = mavl_verify(t);
|
17 |
7u83 |
121 |
clk = clock() - clk;
|
|
|
122 |
|
|
|
123 |
if (rc)
|
|
|
124 |
(void)printf("ok\n");
|
|
|
125 |
else
|
|
|
126 |
(void)printf("error\n");
|
|
|
127 |
(void)printf("VERIFYING TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
128 |
(void)printf("Deleting all values ... ");
|
|
|
129 |
clk = clock();
|
|
|
130 |
mavl_del_all(t);
|
|
|
131 |
clk = clock() - clk;
|
|
|
132 |
(void)printf("DELETION TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
133 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
134 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
(void)printf("Inserting %d random intergers ...\n", NUMVALS);
|
|
|
138 |
/*srand(time(NULL));*/
|
|
|
139 |
srand(1941);
|
|
|
140 |
|
|
|
141 |
clk = clock();
|
|
|
142 |
for (i = 0; i < NUMVALS; i++) {
|
|
|
143 |
insval = rand();
|
|
|
144 |
(void)mavl_insert(t, &insval, &exists);
|
|
|
145 |
}
|
|
|
146 |
clk = clock() - clk;
|
|
|
147 |
(void)printf("TIME: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
148 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
149 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
150 |
|
|
|
151 |
(void)printf("Verifying ... ");
|
|
|
152 |
clk = clock();
|
19 |
7u83 |
153 |
rc = mavl_verify(t);
|
17 |
7u83 |
154 |
clk = clock() - clk;
|
|
|
155 |
|
|
|
156 |
if (rc)
|
|
|
157 |
(void)printf("ok\n");
|
|
|
158 |
else
|
|
|
159 |
(void)printf("error\n");
|
|
|
160 |
(void)printf("VERIFYING TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
161 |
(void)printf("Deleting all values ... \n");
|
|
|
162 |
clk = clock();
|
|
|
163 |
mavl_del_all(t);
|
|
|
164 |
clk = clock() - clk;
|
|
|
165 |
(void)printf("DELETION TOOK: %0.2f SECONDS\n", (double)clk / (double)CLOCKS_PER_SEC);
|
|
|
166 |
(void)printf("ELEMENTS IN TREE: %d\n", t->count);
|
|
|
167 |
(void)printf("DEPTH = %d\n", mavl_get_depth(t));
|
|
|
168 |
|
|
|
169 |
return 0;
|
|
|
170 |
}
|