450 |
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 3 Create an AVL which stores integer values and we check
|
|
|
37 |
* mavl_get_ext.
|
|
|
38 |
*/
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
/* Compare function to compare integers */
|
|
|
42 |
static int
|
|
|
43 |
cmp(const void *v1, const void *v2)
|
|
|
44 |
{
|
|
|
45 |
return *((int *)v1) - *((int *)v2);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/*
|
|
|
49 |
* Number of integer values we want to put into our tree
|
|
|
50 |
*/
|
|
|
51 |
#define NUMVALS 100L
|
|
|
52 |
|
|
|
53 |
int
|
|
|
54 |
main(void)
|
|
|
55 |
{
|
|
|
56 |
int i,val,prevval;
|
|
|
57 |
int insval;
|
|
|
58 |
int exists;
|
|
|
59 |
struct mavl *t;
|
|
|
60 |
void *data;
|
|
|
61 |
struct mavliter it;
|
|
|
62 |
int search;
|
|
|
63 |
int *result;
|
|
|
64 |
|
|
|
65 |
/*
|
|
|
66 |
* Create a MAVL object which store object of size of int . Because
|
|
|
67 |
* we do not have to free any memory, wo don't need to define free
|
|
|
68 |
* function, so second argument to mavl_create is NULL.
|
|
|
69 |
*/
|
|
|
70 |
t = mavl_create(cmp, NULL, sizeof(int));
|
|
|
71 |
if (t == NULL) {
|
|
|
72 |
(void)fprintf(stderr,
|
|
|
73 |
"Can't create struct mavl*: %s\n",
|
|
|
74 |
strerror(errno));
|
|
|
75 |
return errno;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/* Seed the random generator with current time stamp */
|
|
|
79 |
srand((unsigned int)time(NULL));
|
|
|
80 |
|
|
|
81 |
/* Put some random values into our MAVL tree */
|
|
|
82 |
for (i = 0; i < NUMVALS; i+=2) {
|
|
|
83 |
insval = i;
|
|
|
84 |
(void)mavl_insert(t, &insval, &exists);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
search = 3;
|
|
|
88 |
result =
|
|
|
89 |
mavl_get_ext(t , &search, MAVL_FIND_LAST);
|
|
|
90 |
|
|
|
91 |
printf("Resul is %d\n",*result);
|
|
|
92 |
|
|
|
93 |
mavl_destroy(t);
|
|
|
94 |
return 0;
|
|
|
95 |
}
|