1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
/* Copyright (C) 2006, 2007 William McCune
This file is part of the LADR Deduction Library.
The LADR Deduction Library is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License,
version 2.
The LADR Deduction Library is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the LADR Deduction Library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "../ladr/avltree.h"
#include "../ladr/random.h"
static BOOL Debug = TRUE;
static Ordertype ptr_compare(void *i1, void *i2)
{
return (i1 < i2 ? LESS_THAN : (i1 > i2 ? GREATER_THAN : SAME_AS));
} /* ptr_compare */
int main(int argc, char **argv)
{
int n, i, seed, *a;
Avl_node p = NULL;
if (argc != 3) {
printf("need 2 args: size seed\n");
exit(0);
}
n = atoi(argv[1]);
seed = atoi(argv[2]);
srand(seed);
a = malloc(n * sizeof(int));
random_permutation(a, n);
printf("starting insert\n"); fflush(stdout);
for (i = 0; i < n; i++) {
int x = a[i] + 1;
if (Debug)
printf("inserting %4d\n", x);
p = avl_insert(p, (void *) x, ptr_compare);
if (Debug)
avl_check(p, ptr_compare);
}
if (Debug)
p_avl(p, 0);
printf("finished insert\n"); fflush(stdout);
printf("smallest item is %d.\n", (int) avl_smallest(p));
printf("largest item is %d.\n", (int) avl_largest(p));
printf("position of %d is %.4f.\n",
a[0]+1, avl_position(p, (void *) a[0]+1, ptr_compare));
if (Debug) {
for (i = 1; i <= n; i++) {
double pos = avl_position(p, (void *) i, ptr_compare);
int j = (int) avl_item_at_position(p, pos);
printf("item %d, position %.12f, item=%d.\n", i, pos, j);
}
for (i = 0; i <= 100; i++) {
double d = i / 100.0;
int j = (int) avl_item_at_position(p, d);
double pos = avl_position(p, (void *) j, ptr_compare);
printf("Position %.2f, item %d, position=%.4f.\n", d, j, pos);
}
}
random_permutation(a, n);
printf("starting delete\n"); fflush(stdout);
for (i = 0; i < n; i++) {
int x = a[i] + 1;
if (Debug)
printf("********************************** Removing %d\n", x);
p = avl_delete(p, (void *) x, ptr_compare);
if (Debug)
avl_check(p, ptr_compare);
}
p_avl(p, 0);
exit(0);
} /* main */
|