2002-12-14 22:43:18 +01:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: vector.h
|
2003-08-13 13:01:40 +02:00
|
|
|
* DESCRIPTION: static array of simple elements
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
* You may obtain a copy of the Licence at
|
|
|
|
* http://www.gnu.org/licences/lgpl.html
|
|
|
|
*
|
|
|
|
* As a special exception this file can also be included in modules
|
|
|
|
* with other source code as long as that source code has been
|
|
|
|
* released under an Open Source Initiative certificed licence.
|
|
|
|
* More information about OSI certification can be found at:
|
|
|
|
* http://www.opensource.org
|
|
|
|
*
|
|
|
|
* This module is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public Licence for more details.
|
|
|
|
*
|
|
|
|
* This module was created by members of the firebird development
|
|
|
|
* team. All individual contributions remain the Copyright (C) of
|
|
|
|
* those individuals and all rights are reserved. Contributors to
|
|
|
|
* this file are either listed below or can be obtained from a CVS
|
|
|
|
* history command.
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Created by: Nickolay Samofatov <skidder@bssys.com>
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2003-09-08 22:23:46 +02:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
2004-06-13 05:09:29 +02:00
|
|
|
* $Id: vector.h,v 1.9 2004-06-13 03:09:14 brodsom Exp $
|
2002-12-14 22:43:18 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VECTOR_H
|
|
|
|
#define VECTOR_H
|
|
|
|
|
2003-11-04 00:59:24 +01:00
|
|
|
#include "../jrd/gdsassert.h"
|
2002-12-14 22:43:18 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
|
|
|
// Very fast static array of simple types
|
|
|
|
template <typename T, int Capacity>
|
|
|
|
class Vector {
|
|
|
|
public:
|
|
|
|
Vector() : count(0) {}
|
|
|
|
void clear() { count = 0; };
|
|
|
|
T& operator[](int index) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(index >= 0 && index < count);
|
2002-12-14 22:43:18 +01:00
|
|
|
return data[index];
|
|
|
|
}
|
|
|
|
T* begin() { return data; }
|
2003-12-22 11:00:59 +01:00
|
|
|
T* end() { return data + count; }
|
2002-12-14 22:43:18 +01:00
|
|
|
void insert(int index, const T& item) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(index >= 0 && index <= count);
|
|
|
|
fb_assert(count < Capacity);
|
2003-12-22 11:00:59 +01:00
|
|
|
memmove(data + index + 1, data + index, sizeof(T) * (count++ - index));
|
2002-12-14 22:43:18 +01:00
|
|
|
data[index] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
int add(const T& item) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(count < Capacity);
|
2002-12-14 22:43:18 +01: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-12-22 11:00:59 +01:00
|
|
|
memmove(data + index, data + index + 1, sizeof(T) * (--count - index));
|
2002-12-14 22:43:18 +01:00
|
|
|
}
|
|
|
|
void shrink(int newCount) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(newCount <= count);
|
2002-12-14 22:43:18 +01:00
|
|
|
count = newCount;
|
|
|
|
};
|
2003-12-22 11:00:59 +01:00
|
|
|
void join(Vector<T, Capacity>& L) {
|
2003-11-04 00:59:24 +01:00
|
|
|
fb_assert(count + L.count <= Capacity);
|
2003-12-22 11:00:59 +01:00
|
|
|
memcpy(data + count, L.data, sizeof(T) * L.count);
|
2002-12-14 22:43:18 +01:00
|
|
|
count += L.count;
|
|
|
|
}
|
|
|
|
int getCount() const { return count; }
|
|
|
|
int getCapacity() const { return Capacity; }
|
|
|
|
protected:
|
|
|
|
int count;
|
|
|
|
T data[Capacity];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template for default value comparsion
|
|
|
|
template <typename T>
|
|
|
|
class DefaultComparator {
|
|
|
|
public:
|
2003-12-05 11:35:47 +01:00
|
|
|
static bool greaterThan(const T& i1, const T& i2) {
|
2002-12-14 22:43:18 +01:00
|
|
|
return i1 > i2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template to convert value to index directly
|
|
|
|
template <typename T>
|
|
|
|
class DefaultKeyValue {
|
|
|
|
public:
|
2004-04-18 04:50:38 +02:00
|
|
|
static const T& generate(const void* sender, const T& Item) { return Item; }
|
2002-12-14 22:43:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fast sorted array of simple objects
|
|
|
|
// It is used for B+ tree nodes lower, but can still be used by itself
|
|
|
|
template <typename Value, int Capacity, typename Key = Value,
|
|
|
|
typename KeyOfValue = DefaultKeyValue<Value>,
|
|
|
|
typename Cmp = DefaultComparator<Key> >
|
|
|
|
class SortedVector : public Vector<Value, Capacity> {
|
|
|
|
public:
|
|
|
|
SortedVector() : Vector<Value, Capacity>() {}
|
2004-04-18 04:50:38 +02:00
|
|
|
bool find(const Key& item, int& pos) const {
|
2004-05-03 06:25:06 +02:00
|
|
|
int highBound = this->count, lowBound = 0;
|
2002-12-14 22:43:18 +01:00
|
|
|
while (highBound > lowBound) {
|
2003-12-22 11:00:59 +01:00
|
|
|
const int temp = (highBound + lowBound) >> 1;
|
2004-05-03 06:25:06 +02:00
|
|
|
if (Cmp::greaterThan(item, KeyOfValue::generate(this, this->data[temp])))
|
2003-12-22 11:00:59 +01:00
|
|
|
lowBound = temp + 1;
|
2002-12-14 22:43:18 +01: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);
|
2002-12-14 22:43:18 +01:00
|
|
|
}
|
|
|
|
int add(const Value& item) {
|
|
|
|
int pos;
|
2003-12-22 11:00:59 +01:00
|
|
|
find(KeyOfValue::generate(this, item), pos);
|
|
|
|
insert(pos, item);
|
2002-12-14 22:43:18 +01:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-06-13 05:09:29 +02:00
|
|
|
} // namespace Firebird
|
2002-12-14 22:43:18 +01:00
|
|
|
|
|
|
|
#endif
|
2003-12-22 11:00:59 +01:00
|
|
|
|