8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 23:23:04 +01:00
firebird-mirror/src/common/classes/array.h

223 lines
5.7 KiB
C
Raw Normal View History

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>
class InlineStorage {
public:
T* getStorage() {
return buffer;
}
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>
2003-10-17 22:29:52 +02:00
class EmptyStorage {
public:
T* getStorage() { return NULL; }
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> >
class Array : private Storage {
2003-04-25 16:47:10 +02:00
public:
2003-10-17 22:29:52 +02:00
Array(MemoryPool* p) :
count(0), capacity(getStorageSize()), data(getStorage()), pool(p) {}
Array(MemoryPool* p, int InitialCapacity) : count(0),
capacity(getStorageSize()), data(getStorage()), pool(p)
{
ensureCapacity(InitialCapacity);
}
~Array()
{
if (data != getStorage())
pool->deallocate(data);
}
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() {
fb_assert(count > 0);
return *data;
}
2004-02-28 20:19:03 +01:00
T& back() {
fb_assert(count > 0);
2003-12-01 18:53:44 +01:00
return *(data + count - 1);
}
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);
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);
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);
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) {
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; }
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;
T* data;
2003-04-25 16:47:10 +02:00
MemoryPool* pool;
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;
}
T* newdata = reinterpret_cast<T*>
(pool->allocate(sizeof(T) * newcapacity
#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())
pool->deallocate(data);
2003-04-25 16:47:10 +02:00
data = newdata;
capacity = newcapacity;
}
}
};
// Dynamic sorted array of simple objects
template <typename Value,
typename Key = Value,
typename KeyOfValue = DefaultKeyValue<Value>,
typename Cmp = DefaultComparator<Key> >
class SortedArray : public Array<Value> {
public:
SortedArray(MemoryPool* p, int s) : Array<Value>(p, s) {}
SortedArray(MemoryPool* p) : Array<Value>(p) {}
2003-04-25 16:47:10 +02:00
bool find(const Key& item, int& pos) {
int highBound = count, lowBound = 0;
2003-04-25 16:47:10 +02:00
while (highBound > lowBound) {
int temp = (highBound + lowBound) >> 1;
if (Cmp::greaterThan(item, KeyOfValue::generate(this, data[temp])))
lowBound = temp + 1;
2003-04-25 16:47:10 +02:00
else
highBound = temp;
}
pos = lowBound;
return highBound != count &&
!Cmp::greaterThan(KeyOfValue::generate(this, data[lowBound]), item);
2003-04-25 16:47:10 +02:00
}
int add(const Value& item) {
int pos;
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>
class HalfStaticArray : public Array<T, InlineStorage<T, InlineCapacity> > {
2003-10-17 22:29:52 +02:00
public:
HalfStaticArray(MemoryPool* p) : Array<T,InlineStorage<T, InlineCapacity> > (p) {}
2003-10-17 22:29:52 +02:00
HalfStaticArray(MemoryPool* p, int InitialCapacity) :
Array<T, InlineStorage<T, InlineCapacity> > (p, 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