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

319 lines
8.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, size_t Capacity>
class InlineStorage : public AutoStorage {
2003-10-17 22:29:52 +02:00
public:
explicit InlineStorage(MemoryPool& p) : AutoStorage(p) { }
InlineStorage() : AutoStorage() { }
protected:
2003-10-17 22:29:52 +02:00
T* getStorage() {
return buffer;
}
size_t 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>
class EmptyStorage : public AutoStorage {
2003-10-17 22:29:52 +02:00
public:
explicit EmptyStorage(MemoryPool& p) : AutoStorage(p) { }
EmptyStorage() : AutoStorage() { }
protected:
2003-10-17 22:29:52 +02:00
T* getStorage() { return NULL; }
size_t 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 : protected Storage {
2003-04-25 16:47:10 +02:00
public:
explicit Array(MemoryPool& p) :
2004-05-03 06:25:06 +02:00
Storage(p), count(0), capacity(this->getStorageSize()), data(this->getStorage()) { }
Array(MemoryPool& p, size_t InitialCapacity) :
2004-05-03 06:25:06 +02:00
Storage(p), count(0), capacity(this->getStorageSize()), data(this->getStorage())
{
ensureCapacity(InitialCapacity);
}
Array() : count(0),
2004-05-03 06:25:06 +02:00
capacity(this->getStorageSize()), data(this->getStorage()) { }
explicit Array(size_t InitialCapacity) : count(0),
2004-05-03 06:25:06 +02:00
capacity(this->getStorageSize()), data(this->getStorage())
2003-10-17 22:29:52 +02:00
{
ensureCapacity(InitialCapacity);
}
~Array()
{
freeData();
}
void clear() { count = 0; }
2004-02-28 20:19:03 +01:00
protected:
const T& getElement(size_t index) const {
2004-07-22 09:17:58 +02:00
fb_assert(index < count);
2003-04-25 16:47:10 +02:00
return data[index];
}
T& getElement(size_t index) {
2004-07-22 09:17:58 +02:00
fb_assert(index < count);
2004-02-28 20:19:03 +01:00
return data[index];
}
void freeData()
{
2004-05-03 06:25:06 +02:00
if (data != this->getStorage())
this->getPool().deallocate(data);
}
2004-02-28 20:19:03 +01:00
public:
typedef T* iterator;
typedef const T* const_iterator;
Array<T, Storage>& operator =(const Array<T, Storage>& L)
{
ensureCapacity(L.count);
memcpy(data, L.data, sizeof(T) * L.count);
count = L.count;
2004-04-18 05:34:22 +02:00
return *this;
}
const T& operator[](size_t index) const {
2004-02-28 20:19:03 +01:00
return getElement(index);
}
T& operator[](size_t index) {
2004-02-28 20:19:03 +01:00
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; }
void insert(size_t index, const T& item) {
2004-07-22 09:17:58 +02:00
fb_assert(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;
}
void insert(size_t index, const Array<T, Storage>& L) {
2004-07-22 09:17:58 +02:00
fb_assert(index <= count);
ensureCapacity(count + L.count);
memmove(data + index + L.count, data + index, sizeof(T) * (count - index));
memcpy(data + index, L.data, L.count);
count += L.count;
}
void insert(size_t index, const T* items, size_t itemsSize) {
fb_assert(index <= count);
ensureCapacity(count + itemsSize);
memmove(data + index + itemsSize, data + index, sizeof(T) * (count - index));
memcpy(data + index, items, sizeof(T) * itemsSize);
count += itemsSize;
}
size_t 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(size_t index) {
2004-07-22 09:17:58 +02:00
fb_assert(index < count);
memmove(data + index, data + index + 1, sizeof(T) * (--count - index));
2003-04-25 16:47:10 +02:00
}
void removeRange(size_t from, size_t to) {
fb_assert(from <= to);
fb_assert(to <= count);
memmove(data + from, data + to, sizeof(T) * (count - to));
count -= (to - from);
}
void removeCount(size_t index, size_t n) {
fb_assert(index + n <= count);
memmove(data + index, data + index + n, sizeof(T) * (count - index - n));
count -= n;
}
2004-02-28 20:19:03 +01:00
void remove(T* itr) {
2004-07-22 09:17:58 +02:00
const size_t index = itr - begin();
fb_assert(index < count);
2004-02-28 20:19:03 +01:00
memmove(data + index, data + index + 1, sizeof(T) * (--count - index));
}
void shrink(size_t 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(size_t 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;
}
// Resize array according to STL's vector::resize() rules
void resize(size_t newCount, const T& val) {
if (newCount > count) {
ensureCapacity(newCount);
while (count < newCount) {
data[count++] = val;
}
}
else {
count = newCount;
}
}
void resize(size_t newCount) {
if (newCount > count) {
grow(newCount);
}
else {
count = newCount;
}
}
// Resize array according to STL's vector::resize() rules
void join(const Array<T, Storage>& 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;
}
size_t getCount() const { return count; }
size_t getCapacity() const { return capacity; }
void push(const T& item) {
add(item);
}
void push(const T* items, size_t itemsSize) {
ensureCapacity(count + itemsSize);
memcpy(data + count, items, sizeof(T) * itemsSize);
count += itemsSize;
}
T pop() {
fb_assert(count > 0);
count--;
return data[count];
}
// prepare array to be used as a buffer of capacity items
T* getBuffer(size_t capacityL) {
2004-05-12 21:23:17 +02:00
ensureCapacity(capacityL);
count = capacityL;
return data;
}
// clear array and release dinamically allocated memory
void free()
{
clear();
freeData();
2004-05-03 06:25:06 +02:00
capacity = this->getStorageSize();
data = this->getStorage();
}
2003-04-25 16:47:10 +02:00
protected:
size_t count, capacity;
T* data;
void ensureCapacity(size_t newcapacity) {
2003-04-25 16:47:10 +02:00
if (newcapacity > capacity) {
if (newcapacity < capacity * 2) {
newcapacity = capacity * 2;
}
T* newdata = reinterpret_cast<T*>
2004-05-03 06:25:06 +02:00
(this->getPool().allocate(sizeof(T) * newcapacity
#ifdef DEBUG_GDS_ALLOC
, 1, __FILE__, __LINE__
#endif
));
memcpy(newdata, data, sizeof(T) * count);
freeData();
2003-04-25 16:47:10 +02:00
data = newdata;
capacity = newcapacity;
}
}
};
// Dynamic sorted array of simple objects
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> >
class SortedArray : public Array<Value, Storage> {
2003-04-25 16:47:10 +02:00
public:
SortedArray(MemoryPool& p, size_t s) : Array<Value, Storage>(p, s) {}
explicit SortedArray(MemoryPool& p) : Array<Value, Storage>(p) {}
explicit SortedArray(size_t s) : Array<Value, Storage>(s) {}
SortedArray() : Array<Value, Storage>() {}
bool find(const Key& item, size_t& pos) const {
size_t highBound = this->count, lowBound = 0;
2003-04-25 16:47:10 +02:00
while (highBound > lowBound) {
2004-07-22 09:17:58 +02:00
const size_t temp = (highBound + lowBound) >> 1;
2004-05-03 06:25:06 +02:00
if (Cmp::greaterThan(item, KeyOfValue::generate(this, this->data[temp])))
lowBound = temp + 1;
2003-04-25 16:47:10 +02:00
else
highBound = temp;
}
pos = lowBound;
2004-05-03 06:25:06 +02:00
return highBound != this->count &&
!Cmp::greaterThan(KeyOfValue::generate(this, this->data[lowBound]), item);
2003-04-25 16:47:10 +02:00
}
size_t add(const Value& item) {
size_t 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, size_t InlineCapacity>
class HalfStaticArray : public Array<T, InlineStorage<T, InlineCapacity> > {
2003-10-17 22:29:52 +02:00
public:
2004-11-24 10:22:07 +01:00
explicit HalfStaticArray(MemoryPool& p) : Array<T, InlineStorage<T, InlineCapacity> > (p) {}
HalfStaticArray(MemoryPool& p, size_t InitialCapacity) :
Array<T, InlineStorage<T, InlineCapacity> > (p, InitialCapacity) {}
2004-11-24 10:22:07 +01:00
HalfStaticArray() : Array<T, InlineStorage<T, InlineCapacity> > () {}
explicit HalfStaticArray(size_t InitialCapacity) :
Array<T, InlineStorage<T, InlineCapacity> > (InitialCapacity) {}
2003-10-17 22:29:52 +02:00
};
} // namespace Firebird
2003-04-25 16:47:10 +02:00
2004-03-07 08:58:55 +01:00
#endif // CLASSES_ARRAY_H