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
|
|
|
|
|
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/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;
|
|
|
|
}
|
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>
|
2003-10-17 22:29:52 +02:00
|
|
|
class EmptyStorage {
|
|
|
|
public:
|
|
|
|
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> >
|
|
|
|
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) {
|
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];
|
|
|
|
}
|
|
|
|
T* begin() { return data; }
|
2003-10-20 12:26:31 +02:00
|
|
|
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) {
|
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) {
|
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
|
|
|
}
|
|
|
|
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-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;
|
|
|
|
}
|
2003-04-26 12:08:11 +02:00
|
|
|
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:
|
2003-10-20 12:26:31 +02:00
|
|
|
SortedArray(MemoryPool* p, int s) : Array<Value>(p, s) {}
|
2003-11-03 18:14:45 +01:00
|
|
|
SortedArray(MemoryPool* p) : Array<Value>(p) {}
|
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-10-20 12:26:31 +02:00
|
|
|
if (Cmp::compare(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 &&
|
2003-10-20 12:26:31 +02:00
|
|
|
!Cmp::compare(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:
|
2003-10-20 12:26:31 +02:00
|
|
|
HalfStaticArray(MemoryPool* p) : Array<T,InlineStorage<T, InlineCapacity> > (p) {}
|
2003-10-17 22:29:52 +02:00
|
|
|
HalfStaticArray(MemoryPool* p, int InitialCapacity) :
|
2003-10-20 12:26:31 +02:00
|
|
|
Array<T, InlineStorage<T, InlineCapacity> > (p, InitialCapacity) {}
|
2003-10-17 22:29:52 +02:00
|
|
|
};
|
|
|
|
|
2003-04-25 16:47:10 +02:00
|
|
|
} // Firebird
|
|
|
|
|
|
|
|
#endif
|
2003-10-20 12:26:31 +02:00
|
|
|
|