Subversion Repositories PlanixRsrch.SVN

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 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
 */
7 7u83 27
#include <stdio.h>
28
#include <string.h>
29
#include <errno.h>
11 7u83 30
#include <stdlib.h>
31
#include <time.h>
7 7u83 32
 
33
#include "mavl.h"
34
 
11 7u83 35
/*
36
 * EXAMPLE 1 Create an AVL which stores just integer values.
37
 */
38
 
39
 
40
/* Compare function to compare integers */
41
static int
7 7u83 42
cmp(const void *v1, const void *v2)
43
{
44
	return *((int *)v1) - *((int *)v2);
45
}
46
 
11 7u83 47
/*
48
 * Number of integer values we want to put into our tree
49
 */
17 7u83 50
#define NUMVALS 100L
7 7u83 51
 
52
int
53
main(void)
54
{
12 7u83 55
	int i,val,prevval;
7 7u83 56
	int insval;
57
	int exists;
58
	struct mavl *t;
11 7u83 59
	void *data;
7 7u83 60
	struct mavliter it;
61
 
11 7u83 62
	/*
63
	 * Create a MAVL object which store object of size of int . Because
64
	 * we do not have to free any memory, wo don't need to define free
65
	 * function, so second argument to mavl_create is NULL.
66
	 */
7 7u83 67
	t = mavl_create(cmp, NULL, sizeof(int));
68
	if (t == NULL) {
11 7u83 69
		(void)fprintf(stderr,
70
			      "Can't create struct mavl*: %s\n",
71
			      strerror(errno));
7 7u83 72
		return errno;
73
	}
74
 
11 7u83 75
	/* Seed the random generator with current time stamp */
76
	srand((unsigned int)time(NULL));
77
 
78
	/* Put some random values into our MAVL tree */
79
	for (i = 0; i < NUMVALS; i++) {
80
		insval = rand();
81
		(void)mavl_insert(t, &insval, &exists);
7 7u83 82
	}
11 7u83 83
	/* Init the iterator */
84
	mavliter_init(&it, t);
85
 
86
	/*
87
	 * Move the iteratyor to the beginning of the MAVL tree. This is not
88
	 * implied by mavliter_init and will cause your program to crash if
89
	 * you don't do it.
90
	 */
7 7u83 91
	(void)mavliter_seek_set(&it);
11 7u83 92
 
12 7u83 93
	prevval=-1;
94
 
11 7u83 95
	/* print out all integers stored in the MAVL tree */
96
	while ((data = mavliter_get(&it)) != NULL) {
97
		val = *((int *)data);
12 7u83 98
		/* Check if values are sorted */
99
		if (val>prevval){
17 7u83 100
			(void)printf("%d ", val); 
12 7u83 101
		}
102
		else {
103
			(void)printf("ERROR: %d ", val);
104
		}
17 7u83 105
		prevval = val;
7 7u83 106
		(void)mavliter_next(&it);
107
	}
108
	(void)printf("\n");
426 7u83 109
 
110
	mavl_destroy(t);
7 7u83 111
	return 0;
112
}