From 1a925afa384b3f7da76aa09982bc575545dd0607 Mon Sep 17 00:00:00 2001 From: alexpeshkoff Date: Sun, 14 Mar 2004 13:19:47 +0000 Subject: [PATCH] Added capability to have following kinds of arrays: 1) sorted or unsorted 2) POD or objects 3) with or without inline storage 4) allocating from auto- or permanent-pool All array constructors take MemoryPool& instead of MemoyPool* --- src/common/classes/objects_array.h | 64 +++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/src/common/classes/objects_array.h b/src/common/classes/objects_array.h index 3e4eb761c4..7dd6d2f093 100644 --- a/src/common/classes/objects_array.h +++ b/src/common/classes/objects_array.h @@ -33,14 +33,14 @@ namespace Firebird { - template - class ObjectsArray : private HalfStaticArray + template > > + class ObjectsArray : protected A { private: - typedef HalfStaticArray inherited; + typedef A inherited; public: class iterator { - friend class ObjectsArray; + friend class ObjectsArray; private: ObjectsArray *lst; int pos; @@ -60,15 +60,19 @@ namespace Firebird T* pointer = lst->getPointer(pos); return *pointer; } - bool operator!=(const iterator& v) const {return pos != v.pos;} + bool operator!=(const iterator& v) const + { + fb_assert(lst == v.lst); + return pos != v.pos; + } }; public: void insert(int index, const T& item) { - T* data = FB_NEW(*pool) T(pool, item); + T* data = FB_NEW(getPool()) T(getPool(), item); inherited::insert(index, data); } int add(const T& item) { - T* data = FB_NEW(*pool) T(pool, item); + T* data = FB_NEW(getPool()) T(getPool(), item); return inherited::add(data); }; void push(const T& item) { @@ -112,8 +116,8 @@ namespace Firebird T* getPointer(int index) { return inherited::getElement(index); } - inline ObjectsArray(MemoryPool* p) : - HalfStaticArray(p) {} + explicit ObjectsArray(MemoryPool& p) : A(p) { } + ObjectsArray() : A() { } ~ObjectsArray() { for (int i = 0; i < getCount(); i++) { delete getPointer(i); @@ -121,6 +125,48 @@ namespace Firebird } int getCount() const {return inherited::getCount();} int getCapacity() const {return inherited::getCapacity();} + void clear() { + for (int i = 0; i < getCount(); i++) { + delete getPointer(i); + } + inherited::clear(); + } + }; + + // Template to convert object value to index directly + template + class ObjectKeyValue { + public: + static const T& generate(void* sender, const T* Item) { return Item; } + }; + + // Template for default value comparsion + template + class ObjectComparator { + public: + static bool greaterThan(const T& i1, const T& i2) { + return *i1 > *i2; + } + }; + + // Dynamic sorted array of simple objects + template , + typename Key = Value*, + typename KeyOfValue = ObjectKeyValue, + typename Cmp = DefaultComparator > + class SortedObjectsArray : public ObjectsArray > { + private: + typedef ObjectsArray > inherited; + public: + explicit SortedObjectsArray(MemoryPool& p) : + ObjectsArray >(p) { } + bool find(const Key& item, int& pos) { + return inherited::find(item, pos); + } }; }