Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "curl_setup.h"
00024
00025 #include "splay.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034 #define compare(i,j) Curl_splaycomparekeys((i),(j))
00035
00036
00037
00038
00039
00040 struct Curl_tree *Curl_splay(struct timeval i,
00041 struct Curl_tree *t)
00042 {
00043 struct Curl_tree N, *l, *r, *y;
00044 long comp;
00045
00046 if(t == NULL)
00047 return t;
00048 N.smaller = N.larger = NULL;
00049 l = r = &N;
00050
00051 for(;;) {
00052 comp = compare(i, t->key);
00053 if(comp < 0) {
00054 if(t->smaller == NULL)
00055 break;
00056 if(compare(i, t->smaller->key) < 0) {
00057 y = t->smaller;
00058 t->smaller = y->larger;
00059 y->larger = t;
00060 t = y;
00061 if(t->smaller == NULL)
00062 break;
00063 }
00064 r->smaller = t;
00065 r = t;
00066 t = t->smaller;
00067 }
00068 else if(comp > 0) {
00069 if(t->larger == NULL)
00070 break;
00071 if(compare(i, t->larger->key) > 0) {
00072 y = t->larger;
00073 t->larger = y->smaller;
00074 y->smaller = t;
00075 t = y;
00076 if(t->larger == NULL)
00077 break;
00078 }
00079 l->larger = t;
00080 l = t;
00081 t = t->larger;
00082 }
00083 else
00084 break;
00085 }
00086
00087 l->larger = t->smaller;
00088 r->smaller = t->larger;
00089 t->smaller = N.larger;
00090 t->larger = N.smaller;
00091
00092 return t;
00093 }
00094
00095
00096
00097
00098
00099
00100 struct Curl_tree *Curl_splayinsert(struct timeval i,
00101 struct Curl_tree *t,
00102 struct Curl_tree *node)
00103 {
00104 static const struct timeval KEY_NOTUSED = {-1, -1};
00105
00106 if(node == NULL)
00107 return t;
00108
00109 if(t != NULL) {
00110 t = Curl_splay(i, t);
00111 if(compare(i, t->key)==0) {
00112
00113
00114
00115
00116 node->same = t;
00117 node->key = i;
00118 node->smaller = t->smaller;
00119 node->larger = t->larger;
00120
00121 t->smaller = node;
00122
00123
00124
00125 t->key = KEY_NOTUSED;
00126
00127
00128 return node;
00129 }
00130 }
00131
00132 if(t == NULL) {
00133 node->smaller = node->larger = NULL;
00134 }
00135 else if(compare(i, t->key) < 0) {
00136 node->smaller = t->smaller;
00137 node->larger = t;
00138 t->smaller = NULL;
00139
00140 }
00141 else {
00142 node->larger = t->larger;
00143 node->smaller = t;
00144 t->larger = NULL;
00145 }
00146 node->key = i;
00147
00148 node->same = NULL;
00149 return node;
00150 }
00151
00152
00153
00154 struct Curl_tree *Curl_splaygetbest(struct timeval i,
00155 struct Curl_tree *t,
00156 struct Curl_tree **removed)
00157 {
00158 struct Curl_tree *x;
00159
00160 if(!t) {
00161 *removed = NULL;
00162 return NULL;
00163 }
00164
00165 t = Curl_splay(i, t);
00166 if(compare(i, t->key) < 0) {
00167
00168 if(t->smaller)
00169 t=Curl_splay(t->smaller->key, t);
00170 else {
00171
00172 *removed = NULL;
00173 return t;
00174 }
00175 }
00176
00177 if(compare(i, t->key) >= 0) {
00178
00179 x = t->same;
00180 if(x) {
00181
00182
00183
00184
00185 x->key = t->key;
00186 x->larger = t->larger;
00187 x->smaller = t->smaller;
00188
00189 *removed = t;
00190 return x;
00191 }
00192
00193 if(t->smaller == NULL) {
00194 x = t->larger;
00195 }
00196 else {
00197 x = Curl_splay(i, t->smaller);
00198 x->larger = t->larger;
00199 }
00200 *removed = t;
00201
00202 return x;
00203 }
00204 else {
00205 *removed = NULL;
00206 return t;
00207 }
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 int Curl_splayremovebyaddr(struct Curl_tree *t,
00223 struct Curl_tree *removenode,
00224 struct Curl_tree **newroot)
00225 {
00226 static const struct timeval KEY_NOTUSED = {-1, -1};
00227 struct Curl_tree *x;
00228
00229 if(!t || !removenode)
00230 return 1;
00231
00232 if(compare(KEY_NOTUSED, removenode->key) == 0) {
00233
00234
00235
00236 if(removenode->smaller == NULL)
00237 return 3;
00238
00239 removenode->smaller->same = removenode->same;
00240 if(removenode->same)
00241 removenode->same->smaller = removenode->smaller;
00242
00243
00244 removenode->smaller = NULL;
00245
00246
00247 *newroot = t;
00248 return 0;
00249 }
00250
00251 t = Curl_splay(removenode->key, t);
00252
00253
00254
00255
00256
00257
00258
00259
00260 if(t != removenode)
00261 return 2;
00262
00263
00264
00265 x = t->same;
00266 if(x) {
00267
00268
00269
00270 x->key = t->key;
00271 x->larger = t->larger;
00272 x->smaller = t->smaller;
00273 }
00274 else {
00275
00276 if(t->smaller == NULL)
00277 x = t->larger;
00278 else {
00279 x = Curl_splay(removenode->key, t->smaller);
00280 x->larger = t->larger;
00281 }
00282 }
00283
00284 *newroot = x;
00285
00286 return 0;
00287 }
00288