2003-04-25 16:47:10 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: array.h
|
|
|
|
* DESCRIPTION: dynamic array of simple elements
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy
|
|
|
|
* of the License at http://www.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* Created by: Alex Peshkov <peshkoff@mail.ru>
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*/
|
|
|
|
|
2004-03-07 08:58:55 +01:00
|
|
|
#ifndef CLASSES_ARRAY_H
|
|
|
|
#define CLASSES_ARRAY_H
|
2003-04-25 16:47:10 +02:00
|
|
|
|
2003-11-04 00:59:24 +01:00
|
|
|
#include "../jrd/gdsassert.h"
|
2003-04-25 16:47:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include "../common/classes/alloc.h"
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2003-10-17 22:29:52 +02:00
|
|
|
// Static part of the array
|
|
|
|
template <typename T, int Capacity>
|
2004-03-14 14:07:01 +01:00
|
|
|
class InlineStorage : public AutoStorage {
|
2003-10-17 22:29:52 +02:00
|
|
|
public:
|
2004-03-14 14:07:01 +01:00
|
|
|
explicit InlineStorage(MemoryPool& p) : AutoStorage(p) { }
|
|
|
|
InlineStorage() : AutoStorage() { }
|
|
|
|
protected:
|
2003-10-17 22:29:52 +02:00
|
|
|
T* getStorage() {
|
|
|
|
return buffer;
|
|
|
|
}
|
2003-10-20 12:26:31 +02:00
|
|
|
int getStorageSize() const {
|
2003-10-17 22:29:52 +02:00
|
|
|
return Capacity;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
T buffer[Capacity];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Used when array doesn't have static part
|
2003-04-25 16:47:10 +02:00
|
|
|
template <typename T>
|
2004-03-14 14:07:01 +01:00
|
|
|
class EmptyStorage : public AutoStorage {
|
2003-10-17 22:29:52 +02:00
|
|
|
public:
|
2004-03-14 14:07:01 +01:00
|
|
|
explicit EmptyStorage(MemoryPool& p) : AutoStorage(p) { }
|
|
|
|
EmptyStorage() : AutoStorage() { }
|
|
|
|
protected:
|
2003-10-17 22:29:52 +02:00
|
|
|
T* getStorage() { return NULL; }
|
2003-10-20 12:26:31 +02:00
|
|
|
int getStorageSize() const { return 0; }
|
2003-10-17 22:29:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Dynamic array of simple types
|
|
|
|
template <typename T, typename Storage = EmptyStorage<T> >
|
2004-03-14 14:07:01 +01:00
|
|
|
class Array : protected Storage {
|
2003-04-25 16:47:10 +02:00
|
|
|
public:
|
2004-03-14 14:07:01 +01:00
|
|
|
explicit Array(MemoryPool& p) :
|
|
|
|
Storage(p), count(0), capacity(getStorageSize()), data(getStorage()) { }
|
2004-03-18 06:56:06 +01:00
|
|
|
Array(MemoryPool& p, int InitialCapacity) :
|
2004-03-14 14:07:01 +01:00
|
|
|
Storage(p), count(0), capacity(getStorageSize()), data(getStorage())
|
|
|
|
{
|
|
|
|
ensureCapacity(InitialCapacity);
|
|
|
|
}
|
|
|
|
Array() : count(0),
|
|
|
|
capacity(getStorageSize()), data(getStorage()) { }
|
|
|
|
explicit Array(int InitialCapacity) : count(0),
|
|
|
|
capacity(getStorageSize()), data(getStorage())
|
2003-10-17 22:29:52 +02:00
|
|
|
{
|
|
|
|
ensureCapacity(InitialCapacity);
|
|
|
|
}
|
2003-11-30 22:01:35 +01:00
|
|
|
~Array()
|
|
|
|
{
|
|
|
|
if (data != getStorage())
|
2004-03-14 14:07:01 +01:00
|
|
|
getPool().deallocate(data);
|
2003-11-30 22:01:35 +01:00
|
|
|
}
|
2003-04-25 16:47:10 +02:00
|
|
|
void clear() { count = 0; };
|
2004-02-28 20:19:03 +01:00
|
|
|
protected:
|
|
|
|
const T& getElement(int index) const {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(index >= 0 && index < count);
|
2003-04-25 16:47:10 +02:00
|
|
|
return data[index];
|
|
|
|
}
|
2004-02-28 20:19:03 +01:00
|
|
|
T& getElement(int index) {
|
|
|
|
fb_assert(index >= 0 && index < count);
|
|
|
|
return data[index];
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
const T& operator[](int index) const {
|
|
|
|
return getElement(index);
|
|
|
|
}
|
|
|
|
T& operator[](int index) {
|
|
|
|
return getElement(index);
|
|
|
|
}
|
|
|
|
const T& front() const {
|
|
|
|
fb_assert(count > 0);
|
|
|
|
return *data;
|
|
|
|
}
|
|
|
|
const T& back() const {
|
|
|
|
fb_assert(count > 0);
|
|
|
|
return *(data + count - 1);
|
|
|
|
}
|
|
|
|
const T* begin() const { return data; }
|
|
|
|
const T* end() const { return data + count; }
|
|
|
|
T& front() {
|
2003-11-30 22:01:35 +01:00
|
|
|
fb_assert(count > 0);
|
|
|
|
return *data;
|
|
|
|
}
|
2004-02-28 20:19:03 +01:00
|
|
|
T& back() {
|
2003-11-30 22:01:35 +01:00
|
|
|
fb_assert(count > 0);
|
2003-12-01 18:53:44 +01:00
|
|
|
return *(data + count - 1);
|
2003-11-30 22:01:35 +01:00
|
|
|
}
|
2004-02-28 20:19:03 +01:00
|
|
|
T* begin() { return data; }
|
|
|
|
T* end() { return data + count; }
|
2003-04-25 16:47:10 +02:00
|
|
|
void insert(int index, const T& item) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(index >= 0 && index <= count);
|
2003-10-20 12:26:31 +02:00
|
|
|
ensureCapacity(count + 1);
|
|
|
|
memmove(data + index + 1, data + index, sizeof(T) * (count++ - index));
|
2003-04-25 16:47:10 +02:00
|
|
|
data[index] = item;
|
|
|
|
}
|
|
|
|
int add(const T& item) {
|
2004-02-28 20:19:03 +01:00
|
|
|
ensureCapacity(count + 1);
|
2003-04-25 16:47:10 +02:00
|
|
|
data[count++] = item;
|
|
|
|
return count;
|
|
|
|
};
|
|
|
|
void remove(int index) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(index >= 0 && index < count);
|
2003-10-20 12:26:31 +02:00
|
|
|
memmove(data + index, data + index + 1, sizeof(T) * (--count - index));
|
2003-04-25 16:47:10 +02:00
|
|
|
}
|
2004-02-28 20:19:03 +01:00
|
|
|
void remove(T* itr) {
|
|
|
|
int index = itr - begin();
|
|
|
|
fb_assert(index >= 0 && index < count);
|
|
|
|
memmove(data + index, data + index + 1, sizeof(T) * (--count - index));
|
|
|
|
}
|
2003-04-25 16:47:10 +02:00
|
|
|
void shrink(int newCount) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(newCount <= count);
|
2003-04-25 16:47:10 +02:00
|
|
|
count = newCount;
|
|
|
|
};
|
2003-10-17 22:29:52 +02:00
|
|
|
// Grow size of our array and zero-initialize new items
|
|
|
|
void grow(int newCount) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(newCount >= count);
|
2003-10-17 22:29:52 +02:00
|
|
|
ensureCapacity(newCount);
|
2003-10-20 12:26:31 +02:00
|
|
|
memset(data + count, 0, sizeof(T) * (newCount - count));
|
2003-10-17 22:29:52 +02:00
|
|
|
count = newCount;
|
|
|
|
}
|
2003-04-25 16:47:10 +02:00
|
|
|
void join(Array<T>& L) {
|
2003-10-20 12:26:31 +02:00
|
|
|
ensureCapacity(count + L.count);
|
|
|
|
memcpy(data + count, L.data, sizeof(T) * L.count);
|
2003-04-25 16:47:10 +02:00
|
|
|
count += L.count;
|
|
|
|
}
|
|
|
|
int getCount() const { return count; }
|
|
|
|
int getCapacity() const { return capacity; }
|
2003-11-23 21:17:30 +01:00
|
|
|
void push(const T& item) {
|
|
|
|
add(item);
|
|
|
|
}
|
|
|
|
T pop() {
|
|
|
|
fb_assert(count > 0);
|
|
|
|
count--;
|
|
|
|
return data[count];
|
|
|
|
}
|
2003-04-25 16:47:10 +02:00
|
|
|
protected:
|
|
|
|
int count, capacity;
|
2003-10-20 12:26:31 +02:00
|
|
|
T* data;
|
2003-10-17 22:29:52 +02:00
|
|
|
void ensureCapacity(int newcapacity) {
|
2003-04-25 16:47:10 +02:00
|
|
|
if (newcapacity > capacity) {
|
|
|
|
if (newcapacity < capacity * 2) {
|
|
|
|
newcapacity = capacity * 2;
|
|
|
|
}
|
2003-04-26 12:08:11 +02:00
|
|
|
T* newdata = reinterpret_cast<T*>
|
2004-03-14 14:07:01 +01:00
|
|
|
(getPool().allocate(sizeof(T) * newcapacity
|
2003-04-26 12:08:11 +02:00
|
|
|
#ifdef DEBUG_GDS_ALLOC
|
|
|
|
, 1, __FILE__, __LINE__
|
|
|
|
#endif
|
|
|
|
));
|
|
|
|
memcpy(newdata, data, sizeof(T) * count);
|
2003-10-17 22:29:52 +02:00
|
|
|
if (data != getStorage())
|
2004-03-14 14:07:01 +01:00
|
|
|
getPool().deallocate(data);
|
2003-04-25 16:47:10 +02:00
|
|
|
data = newdata;
|
|
|
|
capacity = newcapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dynamic sorted array of simple objects
|
2004-03-14 14:07:01 +01:00
|
|
|
template <typename Value,
|
|
|
|
typename Storage = EmptyStorage<Value>,
|
2003-04-25 16:47:10 +02:00
|
|
|
typename Key = Value,
|
|
|
|
typename KeyOfValue = DefaultKeyValue<Value>,
|
|
|
|
typename Cmp = DefaultComparator<Key> >
|
2004-03-14 14:07:01 +01:00
|
|
|
class SortedArray : public Array<Value, Storage> {
|
2003-04-25 16:47:10 +02:00
|
|
|
public:
|
2004-03-18 06:56:06 +01:00
|
|
|
SortedArray(MemoryPool& p, int s) : Array<Value, Storage>(p, s) {}
|
2004-03-14 14:07:01 +01:00
|
|
|
explicit SortedArray(MemoryPool& p) : Array<Value, Storage>(p) {}
|
|
|
|
explicit SortedArray(int s) : Array<Value, Storage>(s) {}
|
|
|
|
SortedArray() : Array<Value, Storage>() {}
|
2003-04-25 16:47:10 +02:00
|
|
|
bool find(const Key& item, int& pos) {
|
2003-10-20 12:26:31 +02:00
|
|
|
int highBound = count, lowBound = 0;
|
2003-04-25 16:47:10 +02:00
|
|
|
while (highBound > lowBound) {
|
|
|
|
int temp = (highBound + lowBound) >> 1;
|
2003-12-05 11:35:47 +01:00
|
|
|
if (Cmp::greaterThan(item, KeyOfValue::generate(this, data[temp])))
|
2003-10-20 12:26:31 +02:00
|
|
|
lowBound = temp + 1;
|
2003-04-25 16:47:10 +02:00
|
|
|
else
|
|
|
|
highBound = temp;
|
|
|
|
}
|
|
|
|
pos = lowBound;
|
|
|
|
return highBound != count &&
|
2003-12-05 11:35:47 +01:00
|
|
|
!Cmp::greaterThan(KeyOfValue::generate(this, data[lowBound]), item);
|
2003-04-25 16:47:10 +02:00
|
|
|
}
|
|
|
|
int add(const Value& item) {
|
|
|
|
int pos;
|
2003-10-20 12:26:31 +02:00
|
|
|
find(KeyOfValue::generate(this, item), pos);
|
|
|
|
insert(pos, item);
|
2003-04-25 16:47:10 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-10-17 22:29:52 +02:00
|
|
|
// Nice shorthand for arrays with static part
|
|
|
|
template <typename T, int InlineCapacity>
|
2003-10-20 12:26:31 +02:00
|
|
|
class HalfStaticArray : public Array<T, InlineStorage<T, InlineCapacity> > {
|
2003-10-17 22:29:52 +02:00
|
|
|
public:
|
2004-03-14 14:07:01 +01:00
|
|
|
explicit HalfStaticArray(MemoryPool& p) : Array<T,InlineStorage<T, InlineCapacity> > (p) {}
|
2004-03-18 06:56:06 +01:00
|
|
|
HalfStaticArray(MemoryPool& p, int InitialCapacity) :
|
2003-10-20 12:26:31 +02:00
|
|
|
Array<T, InlineStorage<T, InlineCapacity> > (p, InitialCapacity) {}
|
2004-03-14 14:07:01 +01:00
|
|
|
HalfStaticArray() : Array<T,InlineStorage<T, InlineCapacity> > () {}
|
|
|
|
explicit HalfStaticArray(int InitialCapacity) :
|
|
|
|
Array<T, InlineStorage<T, InlineCapacity> > (InitialCapacity) {}
|
2003-10-17 22:29:52 +02:00
|
|
|
};
|
|
|
|
|
2003-04-25 16:47:10 +02:00
|
|
|
} // Firebird
|
|
|
|
|
2004-03-07 08:58:55 +01:00
|
|
|
#endif // CLASSES_ARRAY_H
|
2003-10-20 12:26:31 +02:00
|
|
|
|