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

175 lines
4.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): ______________________________________.
*/
#ifndef ARRAY_H
#define ARRAY_H
#include <assert.h>
#include <string.h>
#include "../common/classes/vector.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() {
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() { return 0; }
};
// 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; };
T& operator[](int index) {
assert(index >= 0 && index < count);
return data[index];
}
T* begin() { return data; }
T* end() { return data+count; }
void insert(int index, const T& item) {
assert(index >= 0 && index <= count);
2003-10-17 22:29:52 +02:00
ensureCapacity(count+1);
2003-04-25 16:47:10 +02:00
memmove(data+index+1, data+index, sizeof(T)*(count++-index));
data[index] = item;
}
int add(const T& item) {
2003-10-17 22:29:52 +02:00
ensureCapacity(count+1);
2003-04-25 16:47:10 +02:00
data[count++] = item;
return count;
};
void remove(int index) {
assert(index >= 0 && index < count);
memmove(data+index, data+index+1, sizeof(T)*(--count-index));
}
void shrink(int newCount) {
assert(newCount <= count);
count = newCount;
};
2003-10-17 22:29:52 +02:00
// Grow size of our array and zero-initialize new items
void grow(int newCount) {
assert(newCount >= count);
ensureCapacity(newCount);
memset(data+count, 0, sizeof(T)*(newCount-count));
count = newCount;
}
2003-04-25 16:47:10 +02:00
void join(Array<T>& L) {
2003-10-17 22:29:52 +02:00
ensureCapacity(count+L.count);
2003-04-25 16:47:10 +02:00
memcpy(data + count, L.data, sizeof(T)*L.count);
count += L.count;
}
int getCount() const { return count; }
int getCapacity() const { return capacity; }
protected:
int count, capacity;
2003-10-17 22:29:52 +02:00
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) {}
bool find(const Key& item, int& pos) {
int highBound=count, lowBound=0;
while (highBound > lowBound) {
int temp = (highBound + lowBound) >> 1;
if (Cmp::compare(item, KeyOfValue::generate(this,data[temp])))
lowBound = temp+1;
else
highBound = temp;
}
pos = lowBound;
return highBound != count &&
!Cmp::compare(KeyOfValue::generate(this,data[lowBound]), item);
}
int add(const Value& item) {
int pos;
find(KeyOfValue::generate(this,item),pos);
insert(pos,item);
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> > {
public:
HalfStaticArray(MemoryPool* p) : Array<T,InlineStorage<T,InlineCapacity> > (p) {}
HalfStaticArray(MemoryPool* p, int InitialCapacity) :
Array<T,InlineStorage<T,InlineCapacity> > (p, InitialCapacity) {}
};
2003-04-25 16:47:10 +02:00
} // Firebird
#endif