home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1997 November
/
PCWorld_1997-11_cd.bin
/
software
/
programy
/
komix
/
DATA.Z
/
PtrDict.cxx
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-30
|
3KB
|
113 lines
/*---------------------------------------------------------------------------
*
* Copyright (c) 1993 by Westmount Technology B.V., Delft, The Netherlands.
*
* This software is furnished under a license and may be used only in
* accordance with the terms of such license and with the inclusion of
* the above copyright notice. This software or any other copies thereof
* may not be provided or otherwise made available to any other person.
* No title to and ownership of the software is hereby transferred.
*
* The information in this software is subject to change without notice
* and should not be construed as a commitment by Westmount Technology B.V.
*
*---------------------------------------------------------------------------
*
* File : @(#)PtrDict.cxx /main/hindenburg/1
* Author : frmo
* Original date : 26-2-1993
* Description : PtrDict (A map of key-value pairs),
* value is a pointer
*
*---------------------------------------------------------------------------
*/
static const char SccsId[]="@(#)PtrDict.cxx /main/hindenburg/1 30 Jul 1996 Copyright 1993 Westmount Technology";
#ifndef PTRDICT_HXX
#include "PtrDict.hxx"
#endif
// Specialized DictSlot used as a search key only
class DictKey: public DictSlot {
public:
DictKey(const Hashable &key) : DictSlot((Hashable *) &key, 0) {}
};
DictSlot::~DictSlot()
{
}
unsigned DictSlot::hash() const
{
return key->hash();
}
int DictSlot::isEqual(const Hashable &other) const
{
const DictSlot &otherSlot = (const DictSlot &)other;
return key->isEqual(*otherSlot.key);
}
void GenPtrDict::set(Hashable *key, void *value)
{
DictSlot *slot = new DictSlot(key, value);
DictSlot *existing = DictSlotTbl::find(*slot);
if (existing) {
delete key;
delete slot;
existing->setValue(value);
} else
DictSlotTbl::add(*slot);
}
void *GenPtrDict::get(const Hashable &key) const
{
DictKey dictKey(key);
DictSlot *slot = DictSlotTbl::find(dictKey);
return slot ? slot->getValue() : 0;
}
void GenPtrDict::remove(const Hashable &key)
{
DictKey dictKey(key);
DictSlot *slot = DictSlotTbl::find(dictKey);
if (slot) {
DictSlotTbl::remove(dictKey);
// key is always copied in "template" instantiations
delete slot->getKey();
delete slot;
}
}
int GenPtrDict::isPresent(const Hashable &key) const
{
DictKey dictKey(key);
return DictSlotTbl::find(dictKey) != 0;
}
const Hashable *GenPtrDict::firstKey()
{
DictSlot *slot = DictSlotTbl::first();
return slot ? slot->getKey() : 0;
}
const Hashable *GenPtrDict::nextKey()
{
DictSlot *slot = DictSlotTbl::next();
return slot ? slot->getKey() : 0;
}
void *GenPtrDict::firstValue()
{
DictSlot *slot = DictSlotTbl::first();
return slot ? slot->getValue() : 0;
}
void *GenPtrDict::nextValue()
{
DictSlot *slot = DictSlotTbl::next();
return slot ? slot->getValue() : 0;
}