2004-11-24 21:38:31 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2004-12-17 06:41:47 +01:00
|
|
|
#include "../common/classes/fb_string.h"
|
2004-11-24 21:38:31 +01:00
|
|
|
#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
|
|
|
|
{
|
2004-11-24 21:38:31 +01:00
|
|
|
public:
|
|
|
|
typedef typename KeyValuePair::first_type KeyType;
|
|
|
|
typedef typename KeyValuePair::second_type ValueType;
|
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
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;
|
|
|
|
|
2004-11-24 21:38:31 +01:00
|
|
|
GenericMap() : tree(&getPool()), mCount(0) { }
|
2008-12-19 15:57:01 +01:00
|
|
|
|
2008-03-08 08:34:44 +01:00
|
|
|
explicit GenericMap(MemoryPool& a_pool)
|
|
|
|
: AutoStorage(a_pool), tree(&getPool()), mCount(0) { }
|
2008-12-19 15:57:01 +01:00
|
|
|
|
2008-03-08 08:34:44 +01:00
|
|
|
~GenericMap()
|
|
|
|
{
|
2006-09-07 05:30:31 +02:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2007-05-13 03:37:54 +02:00
|
|
|
void assign(GenericMap& v)
|
|
|
|
{
|
2007-05-13 15:44:10 +02:00
|
|
|
clear();
|
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
GenericMap::Accessor accessor(&v);
|
|
|
|
|
|
|
|
for (bool found = accessor.getFirst(); found; found = accessor.getNext())
|
2007-05-13 03:37:54 +02:00
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
const KeyValuePair* const current_pair = accessor.current();
|
2007-06-18 14:52:07 +02:00
|
|
|
put(current_pair->first, current_pair->second);
|
2007-05-13 03:37:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-08 08:34:44 +01:00
|
|
|
void takeOwnership(GenericMap& from)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
fb_assert(this != &from);
|
|
|
|
|
2006-09-07 05:30:31 +02:00
|
|
|
clear();
|
|
|
|
|
|
|
|
tree = from.tree;
|
|
|
|
mCount = from.mCount;
|
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&from.tree);
|
|
|
|
|
|
|
|
if (treeAccessor.getFirst()) {
|
2006-09-07 05:30:31 +02:00
|
|
|
while (true) {
|
2008-12-19 15:57:01 +01:00
|
|
|
bool haveMore = treeAccessor.fastRemove();
|
2006-09-07 05:30:31 +02:00
|
|
|
if (!haveMore)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
from.mCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the map
|
2008-03-08 08:34:44 +01:00
|
|
|
void clear()
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&tree);
|
|
|
|
|
|
|
|
if (treeAccessor.getFirst()) {
|
2004-11-24 21:38:31 +01:00
|
|
|
while (true) {
|
2008-12-19 15:57:01 +01:00
|
|
|
KeyValuePair* temp = treeAccessor.current();
|
|
|
|
bool haveMore = treeAccessor.fastRemove();
|
2004-11-24 21:38:31 +01:00
|
|
|
delete temp;
|
2007-07-26 03:23:18 +02:00
|
|
|
if (!haveMore)
|
|
|
|
break;
|
2004-11-24 21:38:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-07 05:30:31 +02:00
|
|
|
mCount = 0;
|
2006-09-03 03:09:23 +02:00
|
|
|
}
|
|
|
|
|
2004-11-24 21:38:31 +01:00
|
|
|
// Returns true if value existed
|
2008-03-08 08:34:44 +01:00
|
|
|
bool remove(const KeyType& key)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&tree);
|
|
|
|
|
|
|
|
if (treeAccessor.locate(key)) {
|
|
|
|
KeyValuePair* var = treeAccessor.current();
|
|
|
|
treeAccessor.fastRemove();
|
2004-11-24 21:38:31 +01:00
|
|
|
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)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&tree);
|
|
|
|
|
|
|
|
if (treeAccessor.locate(key)) {
|
|
|
|
treeAccessor.current()->second = value;
|
2004-11-24 21:38:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-26 03:23:18 +02:00
|
|
|
KeyValuePair* var = FB_NEW(getPool()) KeyValuePair(getPool(), key, value);
|
2004-11-24 21:38:31 +01:00
|
|
|
tree.add(var);
|
|
|
|
mCount++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-24 15:44:20 +02:00
|
|
|
// 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)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&tree);
|
|
|
|
|
|
|
|
if (treeAccessor.locate(key)) {
|
|
|
|
return NULL;
|
2007-07-24 15:44:20 +02:00
|
|
|
}
|
|
|
|
|
2007-07-26 03:23:18 +02:00
|
|
|
KeyValuePair* var = FB_NEW(getPool()) KeyValuePair(getPool());
|
2007-07-24 15:44:20 +02:00
|
|
|
var->first = key;
|
|
|
|
tree.add(var);
|
|
|
|
mCount++;
|
|
|
|
return &var->second;
|
|
|
|
}
|
|
|
|
|
2004-11-24 21:38:31 +01:00
|
|
|
// Returns true if value is found
|
2008-03-08 08:34:44 +01:00
|
|
|
bool get(const KeyType& key, ValueType& value)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
ValuesTree::Accessor treeAccessor(&tree);
|
|
|
|
|
|
|
|
if (treeAccessor.locate(key)) {
|
|
|
|
value = treeAccessor.current()->second;
|
2004-11-24 21:38:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
// 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
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
if (treeAccessor.locate(key)) {
|
|
|
|
return &treeAccessor.current()->second;
|
|
|
|
}
|
2006-07-30 22:31:25 +02:00
|
|
|
|
2008-12-19 15:57:01 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-30 22:31:25 +02:00
|
|
|
|
2005-05-28 00:45:31 +02:00
|
|
|
bool exist(const KeyType& key)
|
|
|
|
{
|
2008-12-19 15:57:01 +01:00
|
|
|
return ValuesTree::Accessor(&tree).locate(key);
|
2005-05-28 00:45:31 +02:00
|
|
|
}
|
|
|
|
|
2004-11-24 21:38:31 +01:00
|
|
|
size_t count() const { return mCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValuesTree tree;
|
|
|
|
size_t mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef GenericMap<Pair<Full<string, string> > > StringMap;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|