home *** CD-ROM | disk | FTP | other *** search
- #ifndef __RWTVSLDICT_H__
- #define __RWTVSLDICT_H__
- pragma push_align_members(64);
-
- /*
- * A Singly-linked Key/Value dictionary.
- *
- * $Header: E:/vcs/rw/tvsldict.h_v 1.1 04 Mar 1992 10:16:48 KEFFER $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- *
- * Copyright (C) 1992. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * Stores a *copy* of the inserted items into the collection.
- *
- * Assumes that K and V have:
- * - well-defined copy constructor (T::T(const T&) or equiv.);
- * - well-defined assignment operator (T::operator=(const T&) or equiv.);
- *
- * Assumes that K has:
- * - well-defined equality semantics (T::operator==(const T&)).
- *
- * Note that while these are automatically defined for builtin types
- * (such as "int", "double", or any pointer), you may need to provide
- * appropriate operators for your own classes, particularly those with
- * constructors and/or pointers to other objects.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/rw/tvsldict.h_v $
- *
- * Rev 1.1 04 Mar 1992 10:16:48 KEFFER
- *
- *
- * Rev 1.0 02 Mar 1992 16:10:54 KEFFER
- * Initial revision.
- */
-
- //$DECLARE_TEMPLATE
-
- #include "rw/tislist.h"
- #include "rw/tvasslnk.h"
-
-
- /****************************************************************
- * *
- * Declarations for RWTValSlistDictionary<K,V> *
- * *
- ****************************************************************/
-
-
- template <class K, class V>
- class RWExport RWTValSlistDictionary : private RWTIsvSlist< RWTValAssocLink<K,V> > {
-
- friend class RWExport RWTValSlistDictionaryIterator<K,V>;
- typedef RWTValSlistDictionary<K,V> SELF;
- typedef RWTValAssocLink<K,V> LINK;
- typedef RWTIsvSlist<LINK> BASE;
- typedef RWTValSlistDictionaryIterator<K,V> ITERATOR;
-
- protected:
-
- static RWBoolean testKey(const LINK*, void*); // Repackage tester function
- static void applyAssoc(LINK*, void*); // Repackage apply function
-
- public:
-
- // Constructors:
- RWTValSlistDictionary() { }
- RWTValSlistDictionary(const RWTValSlistDictionary<K,V>&);
- ~RWTValSlistDictionary() { clear(); }
-
- // Operators:
- RWTValSlistDictionary<K,V>& operator=(const RWTValSlistDictionary<K,V>&);
- // Look up key, add if not there:
- V& operator[](K key);
-
- // Member functions:
- void applyToKeyAndValue(void (*applyFun)(K,V&,void*), void*);
- void clear() {BASE::clearAndDestroy();}
- RWBoolean contains(K) const; // Contain key?
- BASE::entries;
- BASE::isEmpty;
- RWBoolean find(K key, K& retKey) const; // Returns key in "retKey" if found
- RWBoolean findValue(K key, V& retVal) const; // Returns value in "retVal" if found
- RWBoolean findKeyAndValue(K key, K& retKey, V& retVal) const;
- void insertKeyAndValue(K key, V value)
- {(*this)[key] = value;}
- RWBoolean remove(K);
- };
-
- // Helper class:
- template <class K, class V> struct RWTAssocApply {
- typedef void (*KVFun)(K, V&, void*);
- KVFun _testFun; // Pointer to K/V apply function
- void* _data; // Client data
- RWTAssocApply(KVFun f, void* p) : _testFun(f), _data(p) { }
- };
-
-
- /****************************************************************
- * *
- * Declarations for RWTValSlistDictionaryIterator<K,V> *
- * *
- ****************************************************************/
-
- template <class K, class V>
- class RWExport RWTValSlistDictionaryIterator : private RWTIsvSlistIterator< RWTValAssocLink<K,V> > {
-
- // For convenience in what follows:
- typedef RWTValSlistDictionary<K,V> DICT;
- typedef RWTValAssocLink<K,V> LINK;
- typedef RWTIsvSlistIterator<LINK> BASE;
-
- // Disallow postfix increment. Unless we hide it, some compilers will
- // substitute the prefix increment operator in its place.
- RWBoolean operator++(int);
-
- public:
-
- // Constructor: starts with iterator positioned at last link.
- RWTValSlistDictionaryIterator(DICT& s) : RWTIsvSlistIterator< RWTValAssocLink<K,V> >(s) { }
-
-
- // Operators:
- RWBoolean operator++() {return BASE::operator++();}
- BASE::operator+=; // Advance iterator n links and test
-
- DICT* container() const {return (DICT*)BASE::container();}
- RWBoolean findNext(K);
- K key() const {return BASE::key()->_key;}
- RWBoolean remove(); // Remove item at cursor
- RWBoolean removeNext(K);
- void reset() {BASE::reset();}
- void reset(DICT& s) {BASE::reset(s);}
- V value() const {return BASE::key()->_value;}
- };
-
-
-
- /****************************************************************
- * *
- * Definitions for RWTValSlistDictionary<K,V> *
- * *
- ****************************************************************/
-
- //$IMPLEMENT_TEMPLATE
-
- // Copy constructor (some of these names get pretty bloody long, eh?)
- template <class K, class V>
- RWTValSlistDictionary<K,V>::RWTValSlistDictionary(const RWTValSlistDictionary<K,V>& d)
- {
- // Cast away "constness", which we will honor anyway...
- ITERATOR next((SELF&)d);
- while (++next)
- insertKeyAndValue(next.key(), next.value());
- }
-
- template <class K, class V> RWTValSlistDictionary<K,V>&
- RWTValSlistDictionary<K,V>::operator=(const RWTValSlistDictionary<K,V>& d)
- {
- if (this!=&d){
- clear();
- // Cast away "constness", which we will honor anyway...
- ITERATOR next((SELF&)d);
- while (++next)
- insertKeyAndValue(next.key(), next.value());
- }
- return *this;
- }
-
- template <class K, class V> V&
- RWTValSlistDictionary<K,V>::operator[](K key)
- {
- LINK* assoc = BASE::find(testKey, &key);
- if (assoc==rwnil) BASE::insert(assoc = new LINK(key));
- return assoc->_value;
- }
-
- template <class K, class V> void
- RWTValSlistDictionary<K,V>::applyToKeyAndValue(void (*applyFun)(K,V&,void*), void* a)
- {
- // Package together the user-supplied apply function and data:
- RWTAssocApply<K,V> capsule(applyFun, a);
- // Now use our own apply function:
- BASE::apply(applyAssoc, &capsule);
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::contains(K key) const
- {
- return BASE::contains(testKey, &key);
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::find(K key, K& retKey) const
- {
- LINK* assoc = BASE::find(testKey, &key);
- if (assoc) {
- retKey = assoc->_key;
- return TRUE;
- }
- return FALSE;
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::findKeyAndValue(K key, K& retKey, V& retValue) const
- {
- LINK* assoc = BASE::find(testKey, &key);
- if (assoc) {
- retKey = assoc->_key;
- retValue = assoc->_value;
- return TRUE;
- }
- return FALSE;
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::findValue(K key, V& retValue) const
- {
- LINK* assoc = BASE::find(testKey, &key);
- if (assoc) {
- retValue = assoc->_value;
- return TRUE;
- }
- return FALSE;
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::remove(K key)
- {
- LINK* assoc = (LINK*)BASE::remove(testKey, &key);
- return assoc ? (delete assoc, TRUE) : FALSE;
- }
-
-
-
- /********************************************************
- * *
- * RWTValSlistDictionary<K,V> PROTECTED FUNCTIONS *
- * *
- ********************************************************/
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionary<K,V>::testKey(const RWTValAssocLink<K,V>* a, void* p)
- {
- return a->_key == *(K*)p;
- }
-
- template <class K, class V> void
- RWTValSlistDictionary<K,V>::applyAssoc(RWTValAssocLink<K,V>* a, void* p)
- {
- RWTAssocApply<K,V>* c = (RWTAssocApply<K,V>*)p;
- (*c->_testFun)(a->_key, a->_value, c->_data);
- }
-
-
-
- /****************************************************************
- * *
- * Definitions for RWTValSlistDictionaryIterator<K,V> *
- * *
- ****************************************************************/
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionaryIterator<K,V>::findNext(K ky)
- {
- while (++(*this)) {
- if (key()==ky) return TRUE;
- }
- return FALSE;
- }
-
- // Remove item at cursor
- template <class K, class V> RWBoolean
- RWTValSlistDictionaryIterator<K,V>::remove()
- {
- LINK* link = BASE::remove();
- return link ? (delete link, TRUE) : FALSE;
- }
-
- template <class K, class V> RWBoolean
- RWTValSlistDictionaryIterator<K,V>::removeNext(K key)
- {
- LINK* cur;
- LINK* prev = BASE::key();
-
- while (++(*this)) {
- cur = BASE::key();
- if (key == cur->_key){ // Is this the victim?
- LINK* rem = (LINK*)BASE::removeRight(prev); // If so, remove it and...
- #ifdef RWDEBUG
- assert (rem==cur);
- #endif
- delete rem; // ... delete it.
- return TRUE;
- }
- prev = cur;
- }
- return FALSE;
- }
-
- pragma pop_align_members();
- #endif /* __RWTVSLDICT_H__ */
-