Source: classlib/include/classlib/dict.h
|
|
|
|
/***************************************************************************
dict.h - dizionari chiave -> valore
-------------------
begin : ven dic 7 17:40:01 CET 2001
copyright : (C) 2001 by Nicola De Nisco
email : nicola@winada.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#if !defined( __CLASSLIB_DICT_H )
#define __CLASSLIB_DICT_H
#if !defined( __CLASSLIB_DEFS_H )
#include "classlib/defs.h"
#endif // __CLASSLIB_DEFS_H
#if !defined( __CLASSLIB_SHDDEL_H )
#include "classlib/shddel.h"
#endif // __CLASSLIB_SHDDEL_H
#if !defined( __CLASSLIB_HASHIMP_H )
#include "classlib/hashimp.h"
#endif // __CLASSLIB_HASHIMP_H
template class TMDictionaryAsHashTableIterator;
/**
template class TMDictionaryAsHashTable
template class TMDictionaryAsHashTableIterator
Managed dictionary and iterator
Implements the dictionary container using a hash table. Assumes
that T is of class TAssociation.
*/
template class TMDictionaryAsHashTable :
public TShouldDelete
{
public:
friend class TMDictionaryAsHashTableIterator;
TMDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
HashTable(size)
{
}
~TMDictionaryAsHashTable()
{
Flush();
}
int Add( const T& t )
{
return Find( t ) ? 0 : HashTable.Add( t );
}
int Detach( const T& t, DeleteType dt = DefDelete );
T *Find( const T& t )
{
return HashTable.Find( t );
}
/** empty the array; if the ownership is on also the elements in the array will be destroyed
*/
void Flush( DeleteType dt = DefDelete );
/** auto iterator: use an iterator function calling it for every element of the bag;
the function must have a prototype like this: void iterFunc(T&, void* args); */
void ForEach( void (*func)(T&, void *),
void *args )
{
HashTable.ForEach( func, args );
}
unsigned GetItemsInContainer() const
{
return HashTable.GetItemsInContainer();
}
int IsEmpty() const
{
return HashTable.IsEmpty();
}
protected:
TMHashTableImp HashTable;
private:
static void DeleteElement( T& t, void * );
};
template
int TMDictionaryAsHashTable::Detach( const T& t, DeleteType dt /*= DefDelete*/ )
{
if( DelObj(dt) )
{
T *assoc = Find(t);
if( assoc )
assoc->DeleteElements();
}
return HashTable.Detach( t );
}
template
void TMDictionaryAsHashTable::Flush( DeleteType dt /*= DefDelete*/ )
{
if( DelObj(dt) )
{
HashTable.ForEach( DeleteElement, 0 );
}
HashTable.Flush();
}
template
void TMDictionaryAsHashTable::DeleteElement( T& t, void * )
{
t.DeleteElements();
}
template
class TMDictionaryAsHashTableIterator :
public TMHashTableIteratorImp
{
public:
TMDictionaryAsHashTableIterator( TMDictionaryAsHashTable& t ) :
TMHashTableIteratorImp( t.HashTable )
{
}
};
/**
template class TDictionaryAsHashTable
template class TDictionaryAsHashTableIterator
Standard dictionary and iterator
*/
template class TDictionaryAsHashTable :
public TMDictionaryAsHashTable
{
public:
TDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
TMDictionaryAsHashTable(size)
{
}
};
/**
template class TDictionaryAsHashTable
template class TDictionaryAsHashTableIterator
Standard dictionary and iterator
*/
template
class TDictionaryAsHashTableIterator :
public TMDictionaryAsHashTableIterator
{
public:
TDictionaryAsHashTableIterator( TDictionaryAsHashTable& t ) :
TMDictionaryAsHashTableIterator( t )
{
}
};
template class TMIDictionaryAsHashTableIterator;
/**
template class TMIDictionaryAsHashTable
template class TMIDictionaryAsHashTableIterator
Managed indirect dictionary and iterator
*/
template class TMIDictionaryAsHashTable :
public TShouldDelete
{
public:
friend class TMIDictionaryAsHashTableIterator;
TMIDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
HashTable(size)
{
}
~TMIDictionaryAsHashTable()
{
Flush();
}
int Add( T *t )
{
return Find( t ) ? 0 : HashTable.Add( t );
}
int Detach( T *t, DeleteType dt = DefDelete );
T *Find( T *t )
{
return HashTable.Find( t );
}
void Flush( DeleteType dt = DefDelete );
void ForEach( void (*func)(T&, void *),
void *args )
{
HashTable.ForEach( func, args );
}
unsigned GetItemsInContainer() const
{
return HashTable.GetItemsInContainer();
}
int IsEmpty() const
{
return HashTable.IsEmpty();
}
protected:
TMIHashTableImp HashTable;
private:
static void DeleteElement( T& t, void * );
};
template
int TMIDictionaryAsHashTable::Detach( T *t, DeleteType dt /*= DefDelete*/ )
{
if( DelObj(dt) )
{
T *assoc = Find(t);
if( assoc )
assoc->DeleteElements();
}
return HashTable.Detach( t, DelObj(dt) );
}
template
void TMIDictionaryAsHashTable::Flush( DeleteType dt /*= DefDelete*/ )
{
if( DelObj(dt) )
{
HashTable.ForEach( DeleteElement, 0 );
}
HashTable.Flush(DelObj(dt));
}
template
void TMIDictionaryAsHashTable::DeleteElement( T& t, void * )
{
t.DeleteElements();
}
/**
template class TMIDictionaryAsHashTable
template class TMIDictionaryAsHashTableIterator
Managed indirect dictionary and iterator
*/
template class TMIDictionaryAsHashTableIterator :
public TMIHashTableIteratorImp
{
public:
TMIDictionaryAsHashTableIterator( TMIDictionaryAsHashTable& t ) :
TMIHashTableIteratorImp( t.HashTable )
{
}
};
template class TIDictionaryAsHashTableIterator;
/**
template class TIDictionaryAsHashTable
template class TIDictionaryAsHashTableIterator
Standard indirect dictionary and iterator
*/
template class TIDictionaryAsHashTable :
public TMIDictionaryAsHashTable
{
public:
friend class TIDictionaryAsHashTableIterator;
TIDictionaryAsHashTable( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
TMIDictionaryAsHashTable(size)
{
}
};
template class TIDictionaryAsHashTableIterator :
public TMIDictionaryAsHashTableIterator
{
public:
TIDictionaryAsHashTableIterator( TIDictionaryAsHashTable& t ) :
TMIDictionaryAsHashTableIterator( t )
{
}
};
/**
template class TDictionary
template class TDictionaryIterator
Easy names for TDictionaryAsHashTable and
TDictionaryAsHashTableIterator
*/
template class TDictionary :
public TDictionaryAsHashTable
{
public:
TDictionary( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
TDictionaryAsHashTable( size )
{
}
};
/**
template class TDictionary
template class TDictionaryIterator
Easy names for TDictionaryAsHashTable and
TDictionaryAsHashTableIterator
*/
template class TDictionaryIterator :
public TDictionaryAsHashTableIterator
{
public:
TDictionaryIterator( const TDictionary& a ) :
TDictionaryAsHashTableIterator(a)
{
}
};
///////////////////////////////////////////////////////////////////////////////////////
/**
template class TIDictionary
template class TIDictionaryIterator
Easy names for TIDictionaryAsHashTable and
TIDictionaryAsHashTableIterator
*/
template class TIDictionary :
public TIDictionaryAsHashTable
{
public:
TIDictionary( unsigned size = DEFAULT_HASH_TABLE_SIZE ) :
TIDictionaryAsHashTable( size )
{
}
};
/**
template class TIDictionary
template class TIDictionaryIterator
Easy names for TIDictionaryAsHashTable and
TIDictionaryAsHashTableIterator
*/
template class TIDictionaryIterator :
public TIDictionaryAsHashTableIterator
{
public:
TIDictionaryIterator( const TIDictionary& a ) :
TIDictionaryAsHashTableIterator(a)
{
}
};
#endif // __CLASSLIB_DICT_H
Generated by: nicola on gulliver.wadahome.it on Sun May 25 13:54:34 2003, using kdoc 2.0a53. |