8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-02-01 08:03:02 +01:00
firebird-mirror/src/common/classes/GenericMap.h

234 lines
5.1 KiB
C
Raw Normal View History

/*
* PROGRAM: Client/Server Common Code
* MODULE: StringMap.h
* DESCRIPTION: Secure handling of clumplet buffers
*
* The contents of this file are subject to the Initial
* Developer's 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.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* 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 Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
#ifndef STRINGMAP_H
#define STRINGMAP_H
#include "../common/classes/fb_string.h"
#include "../common/classes/fb_pair.h"
#include "../common/classes/tree.h"
namespace Firebird {
//
// Generic map which allows to have POD and non-POD keys and values.
// The class is memory pools friendly.
//
// Usage
//
// POD key (integer), non-POD value (string):
// GenericMap<Pair<Right<int, string> > >
//
// non-POD key (string), POD value (integer):
// GenericMap<Pair<Left<string, int> > >
//
// non-POD key (string), non-POD value (string):
// GenericMap<Pair<Full<string, string> > >
//
2008-05-14 15:11:41 +02:00
template <typename KeyValuePair, typename KeyComparator = DefaultComparator<typename KeyValuePair::first_type> >
2008-03-08 08:34:44 +01:00
class GenericMap : public AutoStorage
{
public:
typedef typename KeyValuePair::first_type KeyType;
typedef typename KeyValuePair::second_type ValueType;
typedef BePlusTree<KeyValuePair*, KeyType, MemoryPool, FirstObjectKey<KeyValuePair>, KeyComparator> ValuesTree;
class Accessor
{
public:
Accessor(GenericMap* map) : m_Accessor(&map->tree) {}
KeyValuePair* current() const { return m_Accessor.current(); }
bool getFirst() { return m_Accessor.getFirst(); }
bool getNext() { return m_Accessor.getNext(); }
private:
Accessor(const Accessor&);
Accessor& operator=(const Accessor&);
typename ValuesTree::Accessor m_Accessor;
};
friend class Accessor;
GenericMap() : tree(&getPool()), mCount(0) { }
2008-03-08 08:34:44 +01:00
explicit GenericMap(MemoryPool& a_pool)
: AutoStorage(a_pool), tree(&getPool()), mCount(0) { }
2008-03-08 08:34:44 +01:00
~GenericMap()
{
clear();
}
void assign(GenericMap& v)
{
2007-05-13 15:44:10 +02:00
clear();
GenericMap::Accessor accessor(&v);
for (bool found = accessor.getFirst(); found; found = accessor.getNext())
{
const KeyValuePair* const current_pair = accessor.current();
2007-06-18 14:52:07 +02:00
put(current_pair->first, current_pair->second);
}
}
2008-03-08 08:34:44 +01:00
void takeOwnership(GenericMap& from)
{
fb_assert(this != &from);
clear();
tree = from.tree;
mCount = from.mCount;
ValuesTree::Accessor treeAccessor(&from.tree);
if (treeAccessor.getFirst()) {
while (true) {
bool haveMore = treeAccessor.fastRemove();
if (!haveMore)
break;
}
}
from.mCount = 0;
}
// Clear the map
2008-03-08 08:34:44 +01:00
void clear()
{
ValuesTree::Accessor treeAccessor(&tree);
if (treeAccessor.getFirst()) {
while (true) {
KeyValuePair* temp = treeAccessor.current();
bool haveMore = treeAccessor.fastRemove();
delete temp;
2007-07-26 03:23:18 +02:00
if (!haveMore)
break;
}
}
mCount = 0;
2006-09-03 03:09:23 +02:00
}
// Returns true if value existed
2008-03-08 08:34:44 +01:00
bool remove(const KeyType& key)
{
ValuesTree::Accessor treeAccessor(&tree);
if (treeAccessor.locate(key)) {
KeyValuePair* var = treeAccessor.current();
treeAccessor.fastRemove();
delete var;
mCount--;
return true;
}
return false;
}
// Returns true if value existed previously
2008-03-08 08:34:44 +01:00
bool put(const KeyType& key, const ValueType& value)
{
ValuesTree::Accessor treeAccessor(&tree);
if (treeAccessor.locate(key)) {
treeAccessor.current()->second = value;
return true;
}
2007-07-26 03:23:18 +02:00
KeyValuePair* var = FB_NEW(getPool()) KeyValuePair(getPool(), key, value);
tree.add(var);
mCount++;
return false;
}
// Returns pointer to the added empty value or null when key already exists
2008-03-08 08:34:44 +01:00
ValueType* put(const KeyType& key)
{
ValuesTree::Accessor treeAccessor(&tree);
if (treeAccessor.locate(key)) {
return NULL;
}
2007-07-26 03:23:18 +02:00
KeyValuePair* var = FB_NEW(getPool()) KeyValuePair(getPool());
var->first = key;
tree.add(var);
mCount++;
return &var->second;
}
// Returns true if value is found
2008-03-08 08:34:44 +01:00
bool get(const KeyType& key, ValueType& value)
{
ValuesTree::Accessor treeAccessor(&tree);
if (treeAccessor.locate(key)) {
value = treeAccessor.current()->second;
return true;
}
return false;
}
// Returns pointer to the found value or null otherwise
ValueType* get(const KeyType& key)
{
ValuesTree::Accessor treeAccessor(&tree);
2008-12-05 01:56:15 +01:00
if (treeAccessor.locate(key)) {
return &treeAccessor.current()->second;
}
return NULL;
}
2005-05-28 00:45:31 +02:00
bool exist(const KeyType& key)
{
return ValuesTree::Accessor(&tree).locate(key);
2005-05-28 00:45:31 +02:00
}
size_t count() const { return mCount; }
private:
ValuesTree tree;
size_t mCount;
};
typedef GenericMap<Pair<Full<string, string> > > StringMap;
}
#endif