static int Equal(TreeMap *t1,TreeMap *t2) { struct Node *pt1,*pt2; if (t1 == t2) return 1; if (t1 == NULL || t2 == NULL) return 0; if (t1->count != t2->count) return 0; if (t1->Allocator != t2->Allocator) return 0; if (t1->compare != t2->compare) return 0; if (t1->ElementSize != t2->ElementSize) return 0; if (t1->Flags != t2->Flags) return 0; pt1 = bt_first(t1); pt2 = bt_first(t2); while (pt1 && pt2) { if (t1->compare(pt1->data,pt2->data,t1->aux)) break; pt1 = bt_next(t1,pt1); pt2 = bt_next(t2,pt2); } return 1; }
static int Save(TreeMap *src,FILE *stream, SaveFunction saveFn,void *arg) { struct Node *rvp; if (src == NULL) { iError.RaiseError("Save",CONTAINER_ERROR_BADARG); return CONTAINER_ERROR_BADARG; } if (stream == NULL) { src->RaiseError("Save",CONTAINER_ERROR_BADARG); return CONTAINER_ERROR_BADARG; } if (saveFn == NULL) { saveFn = DefaultSaveFunction; } if (fwrite(&TreeMapGuid,sizeof(guid),1,stream) <= 0) return EOF; if (arg == NULL) { arg = &src->ElementSize; } if (fwrite(src,1,sizeof(TreeMap),stream) <= 0) return EOF; rvp = bt_first(src); while (rvp) { char *p = rvp->data; if (saveFn(p,arg,stream) <= 0) return EOF; rvp = bt_next(src,rvp); } return 1; }
u64 bt_next (BtTree_s *tree, u64 key) { BtNode_s *node = tree->root; BtNode_s *nextrt = NULL; if (key == 0) return bt_first(tree); while (node) { if (key < node->key) { nextrt = node; node = node->left; } else if (key == node->key) { if (node->right) { node = node->right; while (node->left) node = node->left; return node->key; } else { if (nextrt) { return nextrt->key; } else { return 0; } } } else { node = node->right; } } return 0; }
static void *GetFirst(Iterator *itrav) { struct TreeMapIterator *trav = (struct TreeMapIterator *)itrav; if (trav->timestamp != trav->bst_table->timestamp) return NULL; trav->bst_node = bt_first(trav->bst_table); if (trav->bst_node) return trav->bst_node->data; return NULL; }
static TreeMap *Copy(TreeMap *src) { TreeMap *result; struct Node *pSrc; if (src == NULL) { iError.RaiseError("Copy",CONTAINER_ERROR_BADARG); return NULL; } pSrc = bt_first(src); result = CreateWithAllocator(src->ElementSize,src->Allocator); while (pSrc) { iTreeMap.Add(result,pSrc->data,NULL); pSrc = bt_next(src,pSrc); } return result; }
/* Returns the node in BT following P in in-order. If P is null, returns the minimum node in BT. Returns a null pointer if P is the maximum node in BT or if P is null and BT is empty. */ static struct Node *bt_next (const TreeMap *bt, const struct Node *p) { if (p == NULL) return bt_first (bt); else if (p->down[1] == NULL) { struct Node *q; for (q = p->up; ; p = q, q = q->up) if (q == NULL || p == q->down[0]) return q; } else { p = p->down[1]; while (p->down[0] != NULL) p = p->down[0]; return (struct Node *)p; } }
/* Returns the node in BT following P in in-order. If P is null, returns the minimum node in BT. Returns a null pointer if P is the maximum node in BT or if P is null and BT is empty. */ struct bt_node * bt_next (const struct bt *bt, const struct bt_node *p) { if (p == NULL) return bt_first (bt); else if (p->down[1] == NULL) { struct bt_node *q; for (q = p->up; ; p = q, q = q->up) if (q == NULL || p == q->down[0]) return q; } else { p = p->down[1]; while (p->down[0] != NULL) p = p->down[0]; return CONST_CAST (struct bt_node *, p); } }
int cdx_calcfiltlist(ClipMachine * ClipMachineMemory, RDD_DATA * rd, RDD_ORDER * ro, RDD_FILTER * fp, const char *__PROC__) { int i, er; BTREE *bt; void *key = malloc(sizeof(unsigned int) + ro->bufsize); ClipVar vv, *vp; if (fp->list) { bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare); for (i = 0; i < fp->listlen; i++) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, fp->list[i], 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } free(fp->list); } else { #if 1 unsigned int bytes = ((fp->size + 1) >> 5) + 1; int i, b, bb, t, tt; fp->listlen = 0; for (i = 0; i < bytes; i++) { if (fp->rmap[i]) { for (b = (i << 2), bb = 0; bb < 4; b++, bb++) { if (((char *) fp->rmap)[b]) { for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++) { if (_rm_getbit(fp->rmap, fp->size, t)) fp->listlen++; } } } } } if (fp->listlen > 100) { free(key); return 0; } #else fp->listlen = 0; for (i = 1; i <= fp->size; i++) if (_rm_getbit(fp->rmap, fp->size, i)) fp->listlen++; #endif bt = bt_create(0, fp->listlen, sizeof(unsigned int) + ro->bufsize, _cdx_compare); #if 1 for (i = 0; i < bytes; i++) { if (fp->rmap[i]) { for (b = (i << 2), bb = 0; bb < 4; b++, bb++) { if (((char *) fp->rmap)[b]) { for (t = (b << 3) + 1, tt = 0; tt < 8; t++, tt++) { if (_rm_getbit(fp->rmap, fp->size, t)) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, t, 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } } } } } } #else for (i = 1; i <= fp->size; i++) { if (_rm_getbit(fp->rmap, fp->size, i)) { if ((er = rd->vtbl->rawgo(ClipMachineMemory, rd, i, 0, __PROC__))) return er; if (ro->simpexpr) { if ((er = rd->vtbl->getvalue(ClipMachineMemory, rd, ro->simpfno, &vv, __PROC__))) return er; vp = &vv; } else { if ((er = rdd_calc(ClipMachineMemory, rd->area, &ro->block, &vv, 0))) return er; vp = _clip_vptr(&vv); } memcpy(key, &rd->recno, sizeof(unsigned int)); if ((er = cdx_formatkey(ClipMachineMemory, ro, vp, key + sizeof(unsigned int), __PROC__))) return er; _clip_destroy(ClipMachineMemory, &vv); bt_add(bt, ro, key); } } #endif } fp->list = malloc(sizeof(unsigned int) * (fp->listlen + 1)); if (!ro->descend) { bt_first(bt); if (bt_key(bt)) { i = 0; fp->list[i] = *(unsigned int *) bt_key(bt); while (!bt_next(bt)) { i++; fp->list[i] = *(unsigned int *) bt_key(bt); } } } else { bt_last(bt); if (bt_key(bt)) { i = 0; fp->list[i] = *(unsigned int *) bt_key(bt); while (!bt_prev(bt)) { i++; fp->list[i] = *(unsigned int *) bt_key(bt); } } } bt_destroy(bt); free(key); return 0; }