📄 data.cc
字号:
while (h != invObjHandle) { Object *o = get(h); s.putObject(o, "element", hom_objid); h = findNext(h); } }}uint SLinkedList::count() const{ return ecount;}int SLinkedList::compareObjects(const Object *a, const Object *b) const{ return autoCompare(a, b);}void SLinkedList::forceSetByIdx(int idx, Object *obj){ // FIXME: throw NotImplementedException(HERE);}Object *SLinkedList::get(ObjHandle h) const{ SLinkedListNode *n = handleToNative(h); return validHandle(h) ? n->obj : NULL;}uint SLinkedList::getObjIdx(ObjHandle g) const{ int i = 0; ObjHandle h = findFirst(); Object *obj = get(g); while (h != invObjHandle) { if (compareObjects(get(h), obj) == 0) return i; i++; h = findNext(h); } return invIdx;}ObjHandle SLinkedList::findByIdx(int i) const{ ObjHandle h = findFirst(); while (h != invObjHandle) { if (!i--) break; h = findNext(h); } return h;}ObjHandle SLinkedList::findFirst() const{ return nativeToHandle(first);}ObjHandle SLinkedList::findLast() const{ return nativeToHandle(last);}ObjHandle SLinkedList::findNext(ObjHandle h) const{ if (!validHandle(h)) return findFirst(); SLinkedListNode *n = handleToNative(h); return nativeToHandle(n->next);}ObjHandle SLinkedList::findPrev(ObjHandle g) const{ if (!validHandle(g)) return findLast(); SLinkedListNode *ng = handleToNative(g); if (ng == first) return invObjHandle; ObjHandle h = findFirst(); while (h != invObjHandle) { SLinkedListNode *nh = handleToNative(h); if (nh->next == ng) return nativeToHandle(nh); h = findNext(h); } return invObjHandle;}bool SLinkedList::del(ObjHandle h){ if (!h) return false; freeObj(remove(h)); return true;}ObjHandle SLinkedList::insert(Object *obj){ SLinkedListNode *n = allocNode(); n->obj = obj; n->next = NULL; if (last) { last->next = n; last = n; } else { first = last = n; } ecount++; notifyInsertOrSet(obj); return nativeToHandle(n);}Object *SLinkedList::remove(ObjHandle h){ if (!validHandle(h)) return NULL; SLinkedListNode *n = handleToNative(h); Object *o = n->obj; if (n == first) { if (n == last) { first = NULL; last = NULL; } else { first = n->next; } } else { SLinkedListNode *p = handleToNative(findPrev(h)); p->next = n->next; if (n == last) last = p; } deleteNode(n); ecount--; return o;}void SLinkedList::insertAt(ObjHandle h, Object *obj){ // FIXME: nyi throw NotImplementedException(HERE);#if 0 uint i = ((uint)h)-1; // WRONG! if (i>ecount-1) { insert(obj); return; } ObjHandle q = i ? findByIdx(i-1) : invObjHandle; SLinkedListNode *n; if (q != invObjHandle) { n = handleToNative(q); } else if (i==0) { n = NULL; } else { insert(obj); return; } SLinkedListNode *m = allocNode(); m->obj = obj; if (n) { m->next = n->next; n->next = m; if (n == last) last = m; } else { m->next = first; first = m; if (!last) last = m; } ecount++; notifyInsertOrSet(obj);#endif}bool SLinkedList::moveTo(ObjHandle from, ObjHandle to){ // FIXME: throw NotImplementedException(HERE);}bool SLinkedList::set(ObjHandle h, Object *obj){ SLinkedListNode *n = handleToNative(h); if (!n) return false; freeObj(n->obj); n->obj = obj; return true;}bool SLinkedList::swap(ObjHandle h, ObjHandle i){ SLinkedListNode *H = handleToNative(h); SLinkedListNode *I = handleToNative(i); Object *t = H->obj; H->obj = I->obj; I->obj = t; return true;}bool SLinkedList::validHandle(ObjHandle h) const{ return h != invObjHandle;}SLinkedListNode* SLinkedList::handleToNative(ObjHandle h) const{ return (SLinkedListNode*)h;}ObjHandle SLinkedList::nativeToHandle(SLinkedListNode *n) const{ return (ObjHandle)n;}/* * DLinkedList */DLinkedList::DLinkedList(bool oo){ own_objects = oo; ecount = 0; first = last = NULL;}DLinkedList::~DLinkedList(){ delAll();}void DLinkedList::delAll(){ DLinkedListNode *n = first, *m; while (n) { m = n->next; freeObj(n->obj); deleteNode(n); n = m; } ecount = 0; first = last = NULL;}DLinkedListNode *DLinkedList::allocNode() const{ return new DLinkedListNode;}void DLinkedList::deleteNode(DLinkedListNode *node) const{ delete node;}void DLinkedList::freeObj(Object *obj) const{ if (own_objects && obj) { obj->done(); delete obj; }}DLinkedList *DLinkedList::clone() const{ DLinkedList *l = new DLinkedList(own_objects); DLinkedListNode *n = first, *m; while (n) { m = n->next; Object *o = m->obj; if (own_objects) o = o->clone(); l->insert(o); n = m; } return l;}void DLinkedList::load(ObjectStream &s){ own_objects = true; first = last = NULL; ecount = 0; int ecount; GET_INT32D(s, ecount); if (ecount) { GET_INT32X(s, hom_objid); for (int i=0; i<ecount; i++) { Object *obj = s.getObjectInternal("element", hom_objid); insert(obj); } } else { hom_objid = OBJID_TEMP; }}ObjectID DLinkedList::getObjectID() const{ return OBJID_DLINKED_LIST;}void DLinkedList::store(ObjectStream &s) const{ PUT_INT32D(s, ecount); if (ecount) { putIDComment(s, hom_objid); PUT_INT32X(s, hom_objid); ObjHandle h = findFirst(); assert(h == invObjHandle || hom_objid != OBJID_TEMP); while (h != invObjHandle) { Object *o = get(h); s.putObject(o, "element", hom_objid); h = findNext(h); } }}uint DLinkedList::count() const{ return ecount;}int DLinkedList::compareObjects(const Object *a, const Object *b) const{ return autoCompare(a, b);}void DLinkedList::forceSetByIdx(int idx, Object *obj){ // FIXME: throw NotImplementedException(HERE);}Object *DLinkedList::get(ObjHandle h) const{ DLinkedListNode *n = handleToNative(h); return validHandle(h) ? n->obj : NULL;}uint DLinkedList::getObjIdx(ObjHandle g) const{ int i = 0; ObjHandle h = findFirst(); Object *obj = get(g); while (h != invObjHandle) { if (compareObjects(get(h), obj) == 0) return i; i++; h = findNext(h); } return invIdx;}ObjHandle DLinkedList::findByIdx(int i) const{ ObjHandle h = findFirst(); while (h != invObjHandle) { if (!i--) break; h = findNext(h); } return h;}ObjHandle DLinkedList::findFirst() const{ return nativeToHandle(first);}ObjHandle DLinkedList::findLast() const{ return nativeToHandle(last);}ObjHandle DLinkedList::findNext(ObjHandle h) const{ if (!validHandle(h)) return findFirst(); DLinkedListNode *n = handleToNative(h); return nativeToHandle(n->next);}ObjHandle DLinkedList::findPrev(ObjHandle h) const{ if (!validHandle(h)) return findLast(); DLinkedListNode *n = handleToNative(h); return nativeToHandle(n->prev);}bool DLinkedList::del(ObjHandle h){ if (!h) return false; freeObj(remove(h)); return true;}ObjHandle DLinkedList::insert(Object *obj){ DLinkedListNode *n = allocNode(); n->obj = obj; n->next = NULL; if (last) { last->next = n; n->prev = last; last = n; } else { first = last = n; } ecount++; notifyInsertOrSet(obj); return nativeToHandle(n);}Object *DLinkedList::remove(ObjHandle h){ if (!validHandle(h)) return NULL; DLinkedListNode *n = handleToNative(h); Object *o = n->obj; if (n == first) { if (n == last) { first = NULL; last = NULL; } else { first = n->next; first->prev = NULL; } } else if (n == last) { last = n->prev; last->next = NULL; } else { n->prev->next = n->next; n->next->prev = n->prev; } deleteNode(n); ecount--; return o;}void DLinkedList::insertAt(ObjHandle h, Object *obj){ // FIXME: nyi throw NotImplementedException(HERE);}bool DLinkedList::moveTo(ObjHandle from, ObjHandle to){ // FIXME: throw NotImplementedException(HERE);}bool DLinkedList::set(ObjHandle h, Object *obj){ DLinkedListNode *n = handleToNative(h); if (!n) return false; freeObj(n->obj); n->obj = obj; return true;}bool DLinkedList::swap(ObjHandle h, ObjHandle i){ DLinkedListNode *H = handleToNative(h); DLinkedListNode *I = handleToNative(i); Object *t = H->obj; H->obj = I->obj; I->obj = t; return true;}bool DLinkedList::validHandle(ObjHandle h) const{ return h != invObjHandle;}DLinkedListNode* DLinkedList::handleToNative(ObjHandle h) const{ return (DLinkedListNode*)h;}ObjHandle DLinkedList::nativeToHandle(DLinkedListNode *n) const{ return (ObjHandle)n;}/* * Queue */Queue::Queue(bool own_objects) : SLinkedList(own_objects){}ObjectID Queue::getObjectID() const{ return OBJID_QUEUE;}/* * BinaryTree */BinaryTree::BinaryTree(bool oo, Comparator comp){ root = NULL; own_objects = oo; compare = comp; ecount = 0;}BinaryTree::~BinaryTree(){ delAll();}void BinaryTree::delAll(){ freeAll(root); root = NULL; ecount = 0;}BinTreeNode *BinaryTree::allocNode() const{ return new BinTreeNode;}void BinaryTree::deleteNode(BinTreeNode *node) const{ delete node;}BinTreeNode **BinaryTree::findNodePtr(BinTreeNode **nodeptr, const Object *obj) const{ BinTreeNode **x = nodeptr; while (x) { int c = compareObjects((*x)->key, obj); if (c < 0) { x = &(*x)->right; } else if (c > 0) { x = &(*x)->left; } else break; } return x;}BinTreeNode *BinaryTree::findNode(BinTreeNode *node, const Object *obj) const{ while (node) { int c = compareObjects(node->key, obj); if (c < 0) { node = node->right; } else if (c > 0) { node = node->left; } else break; } return node;}BinTreeNode *BinaryTree::findNodeG(BinTreeNode *node, const Object *obj) const{ if (!node) return NULL; BinTreeNode *lastGreater = NULL; while (true) { int c = compareObjects(obj, node->key); if (c < 0) { if (!node->left) return node; lastGreater = node; node = node->left; } else { if (!node->right) return lastGreater; node = node->right; } }}BinTreeNode *BinaryTree::findNodeGE(BinTreeNode *node, const Object *obj) const{ if (!node) return NULL; BinTreeNode *lastGreater = NULL; while (true) { int c = compareObjects(obj, node->key); if (c < 0) { if (!node->left) return node; lastGreater = node; node = node->left; } else if (c > 0) { if (!node->right) return lastGreater; node = node->right; } else { return node; } }}BinTreeNode *BinaryTree::findNodeL(BinTreeNode *node, const Object *obj) const{ if (!node) return NULL; BinTreeNode *lastLower = NULL; while (true) { int c = compareObjects(obj, node->key); if (c <= 0) { if (!node->left) return lastLower; node = node->left; } else { if (!node->right) return node; lastLower = node; node = node->right; } }}BinTreeNode *BinaryTree::findNodeLE(BinTreeNode *node, const Object *obj) const{ if (!node) return NULL; BinTreeNode *lastLower = NULL; while (true) { int c = compareObjects(obj, node->key); if (c < 0) { if (!node->left) return lastLower; node = node->left; } else if (c > 0) { if (!node->right) return node; lastLower = node; node = node->right; } else { return node; } }}void BinaryTree::freeAll(BinTreeNode *n){ if (!n) return; freeAll(n->left); freeObj(n->key); freeAll(n->right); deleteNode(n);}void BinaryTree::freeObj(Object *obj) const{ if (own_objects && obj) { obj->done(); delete obj; }}ObjHandle BinaryTree::findByIdxR(BinTreeNode *n, int &i) const{ if (!n) return invObjHandle; ObjHandle h; if ((h = findByIdxR(n->left, i))) return h; if (i == 0) return nativeToHandle(n); i--; if ((h = findByIdxR(n->right, i))) return h; return invObjHandle;}BinTreeNode *BinaryTree::getLeftmost(BinTreeNode *node) const{ if (node) while (node->left) node = node->left; return node;}BinTreeNode *BinaryTree::getRightmost(BinTreeNode *node) const{ if (node) while (node->right) node = node->right; return node;}BinTreeNode **BinaryTree::getLeftmostPtr(BinTreeNode **p) const{ if (*p) while ((*p)->left) p = &(*p)->left; return p;}BinTreeNode **BinaryTree::getRightmostPtr(BinTreeNode **p) const{ if (*p) while ((*p)->right) p = &(*p)->right; return p;}void BinaryTree::cloneR(BinTreeNode *node){ if (!node) return; Object *o = own_objects ? node->key->clone() : node->key; // SB: nicht gut: (unnoetige compares) insert(o); cloneR(node->left); cloneR(node->right);}BinaryTree *BinaryTree::clone() const{ BinaryTree *c = new BinaryTree(own_objects, compare); c->cloneR(root); return c;}void BinaryTree::loadR(ObjectStream &s, BinTreeNode **n, int l, int r){ if (l > r) { *n = NULL; return; } *n = allocNode(); uint m = (l+r)/2; loadR(s, &(*n)->left, l, m-1); (*n)->key = s.getObjectInternal("element", hom_objid); loadR(s, &(*n)->right, m+1, r);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -