Debian Codegraph @

avlsort.c

raw symbols SCIP
1 /*****************************************************************************
2
3 avlsort.c - Example program for the AVL-tree library.
4
5 Copyright (C) 1998 Michael H. Buselli <[email protected]>
6 Copyright (C) 2000-2002 Wessel Dankers <[email protected]>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 *****************************************************************************/
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30
31 #include "avl.h"
32
33 void avl_insert_always(avl_tree_t *avltree, void *item) {
34 avl_node_t *newnode, *node;
35
36 newnode = avl_init_node(malloc(sizeof(avl_node_t)), item);
37 if(newnode) {
38 switch(avl_search_closest(avltree, newnode->item, &node)) {
39 case -1:
40 avl_insert_before(avltree, node, newnode);
41 return;
42 case 0:
43 case 1:
44 avl_insert_after(avltree, node, newnode);
45 }
46
47 }
48 }
49
50 int main(int argc, char **argv) {
51 avl_tree_t *t;
52 avl_node_t *c;
53 char *s, *n;
54 char *ob, *bb, *eb;
55 int r, bs;
56 int l, u;
57 struct stat st;
58 int mmapped = 0;
59
60 t = avl_alloc_tree((avl_compare_t)strcmp, NULL);
61
62 u = 0;
63 if(!fstat(STDIN_FILENO, &st) && S_ISREG(st.st_mode)) {
64 bs = st.st_size;
65 bb = eb = mmap(NULL, bs+1, PROT_READ|PROT_WRITE, MAP_PRIVATE, STDIN_FILENO, 0);
66 if(bb) {
67 mmapped = 1;
68 eb = bb+bs;
69 } else {
70 bb = eb = malloc(bs+1);
71 if(!bb) {
72 perror("malloc()");
73 exit(2);
74 }
75 while(bs > 0) {
76 r = read(STDIN_FILENO, eb, bs);
77 if(r <= 0) {
78 perror("moo");
79 exit(2);
80 }
81 eb += r;
82 bs -= r;
83 }
84 }
85 } else {
86 bs = 65536; r = 0;
87 bb = eb = ob = malloc(bs);
88 if(!bb) {
89 perror("malloc()");
90 exit(2);
91 }
92 for(;;) {
93 eb += r;
94 r = read(STDIN_FILENO, eb, 65536);
95 if(r < 0) {
96 perror("moo");
97 exit(2);
98 }
99 if(r == 0)
100 break;
101 bb = realloc(ob = bb, bs += r);
102 if(!bb) {
103 fprintf(stderr, "realloc() to %d bytes failed.\n", bs);
104 exit(2);
105 }
106 if(bb != ob) {
107 eb += bb - ob;
108 u++;
109 }
110 }
111 }
112
113 if(*(eb-1) != '\n')
114 *eb++ = '\n';
115 bs = eb - bb;
116 bb = s = mmapped?(ob=bb):realloc(ob = bb, bs);
117 if(bb != ob)
118 u++;
119 eb = bb + bs;
120 if(mmapped)
121 fprintf(stderr, "Finished reading input (mmap()ed)\n");
122 else
123 fprintf(stderr, "Finished reading input (%d relocations)\n", u);
124
125 l = 0;
126 for(n = s = bb; s<eb; s = n) {
127 n = strchr(n, '\n');
128 *n++ = '\0';
129 avl_insert_always(t, s);
130 l++;
131 }
132 fprintf(stderr, "Inserted %d lines\n", l);
133
134 l = 0;
135 for(c = t->head; c; c = c->next) {
136 puts(c->item);
137 l++;
138 }
139 fprintf(stderr, "Wrote %d lines, depth %d\n", l, t->top?t->top->depth:0);
140
141 for(; l; l--)
142 avl_delete_node(t, t->top);
143 if(!t->top) fprintf(stderr, "Deleted all lines\n");
144
145 avl_free_nodes(t);
146
147 return 0;
148 }