/*-------------------------------------------------------------------------- @COPYRIGHT : Copyright 1996, Alex P. Zijdenbos, McConnell Brain Imaging Centre, Montreal Neurological Institute, McGill University. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies. The author and McGill University make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. ---------------------------------------------------------------------------- $RCSfile: Dictionary.h,v $ $Revision: 1.3 $ $Author: stever $ $Date: 2003/11/17 04:07:52 $ $State: Exp $ --------------------------------------------------------------------------*/ #ifndef DICTIONARY_H #define DICTIONARY_H #include /* (bert) changed from iostream.h */ #include "MTypes.h" #include "trivials.h" /************************************************************* * Dictionary class, essentially an implementation of the Map * class in the 2nd edition of "The C++ Programming Language" * by Bjarne Stroustrup, pp. 284-... * * Alex Zijdenbos, 1994/03/15 *************************************************************/ template class Dictionary; template class DictIterator; /***************** * DictEntry class *****************/ template class DictEntry { public: const Key key; Value value; private: DictEntry *_next; DictEntry *_previous; DictEntry(const Key& k, const Value& v) : key(k), value(v) {} ~DictEntry() { delete _next; } friend class Dictionary; friend class DictIterator; }; /****************** * Dictionary class ******************/ template class Dictionary { friend class DictIterator; DictEntry *_head; DictEntry *_current; DictEntry *_tail; Key _defaultKey; Value _defaultValue; unsigned _size; public: // Constructors/destructor Dictionary() { _initialize(); } Dictionary(const Key& key, const Value& value) : _defaultKey(key), _defaultValue(value) { _initialize(); } Dictionary(const Dictionary&); ~Dictionary() { delete _head; } Dictionary& operator = (const Dictionary&); // Get functions unsigned size() const { return _size; } Value *at(const Key& key); Value& operator [] (const Key& key); Value operator [] (const Key& key) const; const Key& keyOf(const Value& value); Boolean containsKey(const Key& key) const; Boolean containsValue(const Value& value) const; const DictEntry *first() const { return _head; } const DictEntry *last() const { return _tail; } // Entry removing functions void clear() { delete _head; _initialize(); } void removeAll() { clear(); } void remove(const Key& key); // Operators //Dictionary& operator += (const Dictionary& dict); // Add two dictionaries together; this implies that the += operator must be // defined on Value. Boolean operator == (const Dictionary& dict) const; private: void _initialize() { _size = 0; _head = 0; _current = 0; _tail = 0; } void _notImplementedError(); }; template std::ostream& operator << (std::ostream& os, const Dictionary& dict); /*************************** * Dictionary iterator class ***************************/ template class DictIterator { const Dictionary *_dictionary; DictEntry *_dictEntry; public: DictIterator() { _dictionary = 0; _dictEntry = 0; } DictIterator(const Dictionary& dictionary) { _dictionary = &dictionary; _dictEntry = _dictionary->_head; } void attach(const Dictionary& dictionary) { _dictionary = &dictionary; _dictEntry = _dictionary->_head; } DictEntry* first() { return _dictEntry = _dictionary->_head; } DictEntry* last() { return _dictEntry = _dictionary->_tail; } operator DictEntry* () { return _dictEntry; } DictEntry* operator ++(); // Prefix DictEntry* operator ++(int); // Postfix DictEntry* operator --(); // Prefix DictEntry* operator --(int); // Postfix }; #endif