OSDN Git Service

Add support for 64 bit Windows.
[ffftp/ffftp.git] / putty / TREE234.H
1 /*\r
2  * tree234.h: header defining functions in tree234.c.\r
3  * \r
4  * This file is copyright 1999-2001 Simon Tatham.\r
5  * \r
6  * Permission is hereby granted, free of charge, to any person\r
7  * obtaining a copy of this software and associated documentation\r
8  * files (the "Software"), to deal in the Software without\r
9  * restriction, including without limitation the rights to use,\r
10  * copy, modify, merge, publish, distribute, sublicense, and/or\r
11  * sell copies of the Software, and to permit persons to whom the\r
12  * Software is furnished to do so, subject to the following\r
13  * conditions:\r
14  * \r
15  * The above copyright notice and this permission notice shall be\r
16  * included in all copies or substantial portions of the Software.\r
17  * \r
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\r
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
21  * NONINFRINGEMENT.  IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR\r
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF\r
23  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
25  * SOFTWARE.\r
26  */\r
27 \r
28 #ifndef TREE234_H\r
29 #define TREE234_H\r
30 \r
31 /*\r
32  * This typedef is opaque outside tree234.c itself.\r
33  */\r
34 typedef struct tree234_Tag tree234;\r
35 \r
36 typedef int (*cmpfn234) (void *, void *);\r
37 \r
38 /*\r
39  * Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and\r
40  * lookups by key will fail: you can only look things up by numeric\r
41  * index, and you have to use addpos234() and delpos234().\r
42  */\r
43 tree234 *newtree234(cmpfn234 cmp);\r
44 \r
45 /*\r
46  * Free a 2-3-4 tree (not including freeing the elements).\r
47  */\r
48 void freetree234(tree234 * t);\r
49 \r
50 /*\r
51  * Add an element e to a sorted 2-3-4 tree t. Returns e on success,\r
52  * or if an existing element compares equal, returns that.\r
53  */\r
54 void *add234(tree234 * t, void *e);\r
55 \r
56 /*\r
57  * Add an element e to an unsorted 2-3-4 tree t. Returns e on\r
58  * success, NULL on failure. (Failure should only occur if the\r
59  * index is out of range or the tree is sorted.)\r
60  * \r
61  * Index range can be from 0 to the tree's current element count,\r
62  * inclusive.\r
63  */\r
64 void *addpos234(tree234 * t, void *e, int index);\r
65 \r
66 /*\r
67  * Look up the element at a given numeric index in a 2-3-4 tree.\r
68  * Returns NULL if the index is out of range.\r
69  * \r
70  * One obvious use for this function is in iterating over the whole\r
71  * of a tree (sorted or unsorted):\r
72  * \r
73  *   for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);\r
74  * \r
75  * or\r
76  * \r
77  *   int maxcount = count234(tree);\r
78  *   for (i = 0; i < maxcount; i++) {\r
79  *       p = index234(tree, i);\r
80  *       assert(p != NULL);\r
81  *       consume(p);\r
82  *   }\r
83  */\r
84 void *index234(tree234 * t, int index);\r
85 \r
86 /*\r
87  * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not\r
88  * found. e is always passed as the first argument to cmp, so cmp\r
89  * can be an asymmetric function if desired. cmp can also be passed\r
90  * as NULL, in which case the compare function from the tree proper\r
91  * will be used.\r
92  * \r
93  * Three of these functions are special cases of findrelpos234. The\r
94  * non-`pos' variants lack the `index' parameter: if the parameter\r
95  * is present and non-NULL, it must point to an integer variable\r
96  * which will be filled with the numeric index of the returned\r
97  * element.\r
98  * \r
99  * The non-`rel' variants lack the `relation' parameter. This\r
100  * parameter allows you to specify what relation the element you\r
101  * provide has to the element you're looking for. This parameter\r
102  * can be:\r
103  * \r
104  *   REL234_EQ     - find only an element that compares equal to e\r
105  *   REL234_LT     - find the greatest element that compares < e\r
106  *   REL234_LE     - find the greatest element that compares <= e\r
107  *   REL234_GT     - find the smallest element that compares > e\r
108  *   REL234_GE     - find the smallest element that compares >= e\r
109  * \r
110  * Non-`rel' variants assume REL234_EQ.\r
111  * \r
112  * If `rel' is REL234_GT or REL234_LT, the `e' parameter may be\r
113  * NULL. In this case, REL234_GT will return the smallest element\r
114  * in the tree, and REL234_LT will return the greatest. This gives\r
115  * an alternative means of iterating over a sorted tree, instead of\r
116  * using index234:\r
117  * \r
118  *   // to loop forwards\r
119  *   for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)\r
120  *       consume(p);\r
121  * \r
122  *   // to loop backwards\r
123  *   for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)\r
124  *       consume(p);\r
125  */\r
126 enum {\r
127     REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE\r
128 };\r
129 void *find234(tree234 * t, void *e, cmpfn234 cmp);\r
130 void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation);\r
131 void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index);\r
132 void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp, int relation,\r
133                     int *index);\r
134 \r
135 /*\r
136  * Delete an element e in a 2-3-4 tree. Does not free the element,\r
137  * merely removes all links to it from the tree nodes.\r
138  * \r
139  * delpos234 deletes the element at a particular tree index: it\r
140  * works on both sorted and unsorted trees.\r
141  * \r
142  * del234 deletes the element passed to it, so it only works on\r
143  * sorted trees. (It's equivalent to using findpos234 to determine\r
144  * the index of an element, and then passing that index to\r
145  * delpos234.)\r
146  * \r
147  * Both functions return a pointer to the element they delete, for\r
148  * the user to free or pass on elsewhere or whatever. If the index\r
149  * is out of range (delpos234) or the element is already not in the\r
150  * tree (del234) then they return NULL.\r
151  */\r
152 void *del234(tree234 * t, void *e);\r
153 void *delpos234(tree234 * t, int index);\r
154 \r
155 /*\r
156  * Return the total element count of a tree234.\r
157  */\r
158 int count234(tree234 * t);\r
159 \r
160 #endif                          /* TREE234_H */\r