File: examples.c

package info (click to toggle)
libwn6 6.0-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,996 kB
  • ctags: 3,938
  • sloc: ansic: 45,083; makefile: 926; csh: 274; sh: 12
file content (179 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download | duplicates (4)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/****************************************************************************

COPYRIGHT NOTICE:

  The source code in this file is provided free of charge
  to the author's consulting clients.  It is in the
  public domain and therefore may be used by anybody for
  any purpose.

AUTHOR:

  Will Naylor

****************************************************************************/
#include <stdio.h>

#include "wnlib.h"
#include "wnmem.h"
#include "wnasrt.h"
#include "wnnop.h"
#include "wncmp.h"
#include "wncpy.h"
#include "wnrnd.h"
#include "wnrndd.h"

#include "wnbtr.h"
#include "wnbtrl.h"



#if 0


local void print_handle(wn_bhandle handle)
{
  printf("handle->key=%d\n",handle->key);
}


main()
{
  wn_btree tree;
  wn_bhandle handle;
  int i,num,count,index,index2;
  
  wn_gpmake("general_free");

  wn_mkintbtree(&tree);
  wn_bverify(tree);

  for(i=0;i<1000;i++)
  {
    /*
    num = wn_random_int_between(0,1000000);
    num = 1;
    */
    num = i;
    wn_bins(&handle,tree,(ptr)num);
    handle->contents = (ptr)(num+1);
  }

  wn_bverify(tree);
  printf("inserts finished\n");

  wn_bact(tree,(print_handle),(ptr)1,WN_B_EQ,(ptr)10,WN_B_GT);
  printf("wnbact finished\n");

  for(i=0;i<1000;i++)
  {
    count = wn_bcount(tree);
    wn_assert(count > 0);

    index = wn_random_int_between(0,count);
    wn_bget_handle_of_index(&handle,tree,index);
    wn_bget_index_of_handle(&index2,tree,handle);
    wn_assert(index == index2);
    wn_bdel(handle,tree);
  }

  wn_bverify(tree);

  count = wn_bcount(tree);
  wn_assert(count == 0);

  wn_bverify(tree);
  printf("deletes finished\n");

  wn_gpfree();
}
#endif



#if 1

#define SIZE  1000000


static int count;

local int count_intcmp(int i1,int i2)
{
  ++count;

  return(wn_intcmp(i1,i2));
}


main()
{
  wn_btree tree;
  wn_bhandle handle;
  int i,num,count_ins,count_del;
  
  wn_gpmake("general_free");

  wn_mkbtree(&tree,
	     (int (*)(ptr,ptr))(count_intcmp),
	     (void (*)(ptr *,ptr))(wn_intcpy),
	     (void (*)(ptr))(wn_do_nothing));

  count = 0;

  wn_seed_random_number_generator(0);

  for(i=0;i<SIZE;++i)
  {
    num = (int)wn_random_int();
    /*
    num = i;
    */
    wn_bins(&handle,tree,(ptr)num);
    handle->contents = (ptr)(num+1);
  }

  printf("inserts finished. compare count = %d\n",count);

#if 0
  count = 0;

  wn_seed_random_number_generator(0);

  for(i=0;i<SIZE;i++)
  {
    num = wn_random_int();
    /*
    num = i;
    */
    wn_bget(&handle,tree,num,WN_B_EQ);
  }

  printf("searches finished. compare count = %d\n",count);
#endif

  count_ins = count_del = 0;

  wn_seed_random_number_generator(1);

  for(i=0;i<SIZE;i++)
  {
    num = (int)wn_random_int();

    count = 0;
    wn_bins(&handle,tree,(ptr)num);
    count_ins += count;

    count = 0;
    wn_bdel(handle,tree);
    count_del += count;
  }

  printf("trials finished. ins count = %d,delete count = %d\n",
	 count_ins,count_del);

  wn_gpfree();
}

#endif