📄 data.h
字号:
#define lastThat(XTYPE, X, E, condition) \{ \ XTYPE *Y = NULL; \ foreachbwd(XTYPE, X, E, \ if (condition) { \ Y = X; \ break; \ } \ ) \ X = Y; \}/** * A Container. */class Container: public Enumerator {protected: ObjectID hom_objid; virtual void notifyInsertOrSet(const Object *o);public: Container(BuildCtorArg&a): Enumerator(a) {}; Container(); /* extends Enumerator */ virtual Container * clone() const = 0; /* new *//** * Delete all objects. (ie. remove and free all objects) */ virtual void delAll() = 0;/** * Delete object. * Delete (ie. remove and free) first object like <i>sig</i> in * this structure (if possible). * * @param sig signature of object to delete (may be inserted in the structure) * @returns true if an object has been deleted, false otherwise */ virtual bool delObj(Object *sig);/** * Delete object. * Delete (ie. remove and free) object identified by <i>h</i>. * * @param h object handle of the object to delete * @returns true if the object has been deleted, false otherwise */ virtual bool del(ObjHandle h) = 0;/** * Find or insert object. * Find first object like <i>obj</i> and if that fails, insert <i>obj</i>. * Ie. after call of this function it is guaranteed that <i>contains(obj)</i>. * * @param obj object to find similar one to or object to insert * @param inserted indicates if <i>obj</i> has been inserted * @returns object handle of existing or inserted object (never <i>invObjHandle</i>) */ virtual ObjHandle findOrInsert(Object *obj, bool &inserted);/** * Insert object. * Insert <i>obj</i> * * @param obj object to insert * @returns object handle of inserted object (never <i>invObjHandle</i>) */ virtual ObjHandle insert(Object *obj) = 0;/** * Remove object. * Remove first object like <i>sig</i> from this structure. * Returned object must be freed explicitly. * * @param sig signature of object to remove * @returns removed object */ virtual Object * removeObj(const Object *sig);/** * Remove object. * Remove object identified by <i>h</i>. * Returned object must be freed explicitly. * * @param h object handle of object to remove * @returns removed object */ virtual Object * remove(ObjHandle h) = 0;/** * Insert object. (operator-form) */ inline ObjHandle operator += (Object *obj) { return insert(obj); }/** * Delete object. (operator-form) */ inline bool operator -= (ObjHandle h) { return del(h); }/** * Delete object. (operator-form) */ inline bool operator -= (Object *sig) { return (*this -= find(sig)); }/** * Delete object by index. * * @param idx index of object to delete * @returns true if the object has been deleted, false otherwise */ inline bool operator -= (int idx) { return (*this -= findByIdx(idx)); }};/** * An abstract list */class List: public Container {public: List(BuildCtorArg&a): Container(a) {}; List() {}; /* extends Enumerator */ virtual List * clone() const = 0; /* new *//** * Insert object at position. * Insert object <i>obj</i> at the position specified by <i>h</i>. * if <i>h</i> does not specify a valid object handle (eg. invObjHandle), * this function works like <i>insert(obj)</i>. * * @param h position to insert object at * @param obj pointer to object to insert * @returns true on success, false otherwise */ virtual void insertAt(ObjHandle h, Object *obj) = 0;/** * Move element. * Move element from position <i>from</i> to position <i>to</i>. * * @param from position of element to move * @param to position to move element to * @returns true on success, false otherwise */ virtual bool moveTo(ObjHandle from, ObjHandle to) = 0;/** * Prepend object. * Prepend object <i>obj</i>. * * @param obj pointer to object to prepend * @returns object handle of inserted object (never <i>invObjHandle</i>) */ inline ObjHandle prepend(Object *obj) { insertAt(findFirst(), obj); return findFirst(); }/** * Set element. * Replace element at position <i>h</i> with object <i>obj</i> * and delete replaced object. * * @param h object handle of element to replace * @param obj object to replace element * @returns true on success, false otherwise */ virtual bool set(ObjHandle h, Object *obj) = 0;/** * Force: Set element by index. * Set element at index <i>i</i> to object <i>obj</i> * and delete object previously located at this index if the index is valid. * If the index <i>i</i> does not specify a valid list-index, * the list is extended, so that <i>obj</i> will be the last element * and the newly created entries in the list will be <i>NULL</i>s. * * @param i index at which to set * @param obj object to set */ virtual void forceSetByIdx(int idx, Object *obj) = 0;/** * Swap two element. * Swap element at position <i>h</i> with element at position <i>i</i>. * * @param h handle of one object * @param i handle of the other object * @returns true on success, false otherwise */ virtual bool swap(ObjHandle h, ObjHandle i) = 0;};#define ARRAY_CONSTR_ALLOC_DEFAULT 4/** * An array */class Array: public List {private: bool own_objects; uint ecount; uint acount; Object **elems; virtual int calcNewBufferSize(int curbufsize, int min_newbufsize) const; virtual void checkShrink(); virtual void freeObj(Object *obj); void prepareWriteAccess(int i); void realloc(int n); inline bool validHandle(ObjHandle h) const { return (handleToNative(h) < ecount); } inline uint handleToNative(ObjHandle h) const { return (Object**)h-elems; } inline ObjHandle nativeToHandle(int i) const { return elems+i; }public: Array(BuildCtorArg &a): List(a) {}; Array(bool own_objects, int prealloc = ARRAY_CONSTR_ALLOC_DEFAULT); virtual ~Array(); /* extends Object */ virtual Array * clone() const; virtual void load(ObjectStream &s); virtual ObjectID getObjectID() const; virtual void store(ObjectStream &s) const; /* extends Enumerator */ virtual uint count() const { return ecount; } virtual int compareObjects(const Object *a, const Object *b) const; virtual ObjHandle findByIdx(int i) const; virtual ObjHandle findFirst() const; virtual ObjHandle findLast() const; virtual ObjHandle findNext(ObjHandle h) const; virtual ObjHandle findPrev(ObjHandle h) const; virtual Object * get(ObjHandle h) const; virtual uint getObjIdx(ObjHandle h) const; /* extends Container */ virtual void delAll(); virtual bool del(ObjHandle h); virtual ObjHandle insert(Object *obj); virtual Object * remove(ObjHandle h); /* extends List */ virtual void forceSetByIdx(int idx, Object *obj); virtual void insertAt(ObjHandle h, Object *obj); virtual bool moveTo(ObjHandle from, ObjHandle to); virtual bool set(ObjHandle h, Object *obj); virtual bool swap(ObjHandle h, ObjHandle i); /* new *//** * Delete range of objects. (ie. remove and free all objects) * @return number of objects deleted */ virtual int delRange(int start, int end); inline void insertBefore(int idx, Object *obj) { insertAt(findByIdx(idx), obj); }};/** * A stack */class Stack: public Array {public: Stack(BuildCtorArg &a): Array(a) {}; Stack(bool own_objects); /* new */ virtual Object * pop(); virtual void push(Object *obj); virtual ObjectID getObjectID() const;};/** * SLinkedList's node structure */struct SLinkedListNode { Object *obj; SLinkedListNode *next;};/** * A (simply) linked list */class SLinkedList: public List {private: bool own_objects; uint ecount; SLinkedListNode *first, *last; virtual SLinkedListNode *allocNode() const; virtual void deleteNode(SLinkedListNode *node) const; virtual void freeObj(Object *obj) const; inline bool validHandle(ObjHandle h) const; inline SLinkedListNode *handleToNative(ObjHandle h) const; inline ObjHandle nativeToHandle(SLinkedListNode *n) const;public: SLinkedList(BuildCtorArg&a): List(a) {}; SLinkedList(bool own_objects); virtual ~SLinkedList(); /* extends Object */ virtual SLinkedList * clone() const; virtual void load(ObjectStream &s); virtual ObjectID getObjectID() const; virtual void store(ObjectStream &s) const; /* extends Enumerator */ virtual uint count() const; virtual int compareObjects(const Object *a, const Object *b) const; virtual ObjHandle findByIdx(int i) const; virtual ObjHandle findFirst() const; virtual ObjHandle findLast() const; virtual ObjHandle findNext(ObjHandle h) const; virtual ObjHandle findPrev(ObjHandle h) const; virtual Object * get(ObjHandle h) const; virtual uint getObjIdx(ObjHandle h) const; /* extends Container */ virtual void delAll(); virtual bool del(ObjHandle h); virtual ObjHandle insert(Object *obj); virtual Object * remove(ObjHandle h); /* extends List */ virtual void forceSetByIdx(int idx, Object *obj); virtual void insertAt(ObjHandle h, Object *obj); virtual bool moveTo(ObjHandle from, ObjHandle to); virtual bool set(ObjHandle h, Object *obj); virtual bool swap(ObjHandle h, ObjHandle i);};/** * A queue */class Queue: public SLinkedList {public: Queue(BuildCtorArg&a): SLinkedList(a) {}; Queue(bool own_objects);/* new *//** * De-queue element. * Remove and return next element of the queue. * * @returns next element of the queue */ inline Object * deQueue() { return remove(findFirst()); }/** * En-queue element. * Append element <i>obj</i> to the queue. * * @param obj pointer to object to en-queue */ inline void enQueue(Object *obj) { insert(obj); } virtual ObjectID getObjectID() const;};/** * DLinkedList's node structure */struct DLinkedListNode { Object *obj; DLinkedListNode *prev; DLinkedListNode *next;};/** * A (doubly) linked list */class DLinkedList: public List {private: bool own_objects; uint ecount; DLinkedListNode *first, *last; virtual DLinkedListNode *allocNode() const; virtual void deleteNode(DLinkedListNode *node) const; virtual void freeObj(Object *obj) const; inline bool validHandle(ObjHandle h) const; inline DLinkedListNode *handleToNative(ObjHandle h) const; inline ObjHandle nativeToHandle(DLinkedListNode *n) const;public: DLinkedList(BuildCtorArg&a): List(a) {}; DLinkedList(bool own_objects); virtual ~DLinkedList(); /* extends Object */ virtual DLinkedList * clone() const; virtual void load(ObjectStream &s); virtual ObjectID getObjectID() const; virtual void store(ObjectStream &s) const; /* extends Enumerator */ virtual uint count() const; virtual int compareObjects(const Object *a, const Object *b) const; virtual ObjHandle findByIdx(int i) const; virtual ObjHandle findFirst() const; virtual ObjHandle findLast() const;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -