Source: dictionarybag.h


Annotated List
Files
Globals
Hierarchy
Index
// ##########################################################################
// ####                                                                  ####
// ####                      RTP Audio Server Project                    ####
// ####                    ============================                  ####
// ####                                                                  ####
// #### Dictionary Bag Template                                          ####
// ####                                                                  ####
// #### Version 1.00  --  February 23, 2001                              ####
// ####                                                                  ####
// #### Copyright (C) 1999  Thomas Dreibholz                             ####
// ####               2000  Universität Bonn, Abt. IV                    ####
// ####               2001  EMail: Dreibholz@bigfoot.com                 ####
// ####                     WWW:   http://www.bigfoot.com/~dreibholz     ####
// ####                                                                  ####
// ##########################################################################


#ifndef DICTIONARYBAG_H
#define DICTIONARYBAG_H


#include "system.h"


/**
  * Internal structure for DictionaryBag template.
  */
template<class K, class T> struct DictionaryBagNode
{
   DictionaryBagNode<K,T>* Prev;
   DictionaryBagNode<K,T>* Next;
   K                       Key;
   T                       Element;
};


/**
  * This class implements the DictionaryBag datatype template.
  *
  * @short   Dictionary Bag
  * @author  Thomas Dreibholz (Dreibholz@bigfoot.com)
  * @version 1.0
  */
template<class K, class T> class DictionaryBag
{
   // ====== Constructor/Destructor =========================================
   public:
   /**
     * Constructor.
     */
   DictionaryBag();
   
   /**
     * Destructor.
     */
   ~DictionaryBag();


   // ====== Element add/remove/test functions ==============================
   /**
     * Add element to head of bag.
     *
     * @param key Key of element to be added.
     * @param element Element to be added.
     * @return true, if element has been added; false, if not.
     */
   bool addHead(K key, T element);

   /**
     * Add element to tail of bag.
     *
     * @param key Key of element to be added.
     * @param element Element to be added.
     * @return true, if element has been added; false, if not.
     */
   bool addTail(K key, T element);

   /**
     * Remove element from bag.
     *
     * @param key Key for element to be removed.
     */
   void remove(const K& key);

   /**
     * Remove element from bag.
     *
     * @param element Element to be removed.
     */
   void remove(T element);

   /**
     * Remove all elements with given key from bag.
     *
     * @param key Key.
     */
   void removeAll(const K& key);

   /**
     * Remove first element from bag.
     */
   inline void removeHead();

   /**
     * Remove last element from bag.
     */
   inline void removeTail();

   /**
     * Check, if bag contains an element.
     *
     * @param element Element to be checked to be in the bag.
     */
   inline bool contains(const T& element) const;

   /**
     * Check, if bag contains an element.
     *
     * @param key Key of element to be checked to be in the bag.
     */
   inline bool contains(const K& key) const;

   /**
     * Find element.
     *
     * @param key Key for element to find.
     * @return Element.
     */
   inline T find(const K& key) const;
   
   /**
     * Get number of elements in the bag.
     *
     * @return Number of elements.
     */
   inline cardinal getCount() const;


   // ====== Bag iteration functions ========================================
   /**
     * Get first element. If true is returned, the given reference will be a
     * reference to the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool first(T& element);

   /**
     * Get last element. If true is returned, the given reference will be a
     * reference to the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool last(T& element);

   /**
     * Get next element. If true is returned, the given reference will be a
     * reference to the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool next(T& element);

   /**
     * Get previous element. If true is returned, the given reference will be a
     * reference to the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool prev(T& element);

   /**
     * Get first element. If true is returned, the given references will be
     * references to the key and the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool first(K& key, T& element);

   /**
     * Get last element. If true is returned, the given references will be
     * references to the key and the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool last(K& key, T& element);

   /**
     * Get next element. If true is returned, the given references will be
     * references to the key and the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool next(K& key, T& element);

   /**
     * Get previous element. If true is returned, the given references will be
     * references to the key and the element.
     *
     * @param element Reference to hold the element's reference.
     * @return true, if the reference is valid.
     */
   inline bool prev(K& key, T& element);


   // ====== Protected data =================================================
   protected:
   /**
     * Get DictionaryBagNode for an element.
     *
     * @param element Element.
     * @return Pointer to DictionaryBagNode or NULL, if element is not in the bag.
     */
   DictionaryBagNode<K,T>* findNode(const T& element) const;

   /**
     * Get DictionaryBagNode for an element.
     *
     * @param key Element's key.
     * @return Pointer to DictionaryBagNode or NULL, if element is not in the bag.
     */
   DictionaryBagNode<K,T>* findNode(const K& key) const;
   
   /**
     * Remove a DictionaryBagNode.
     *
     * @param node DictionaryBagNode to be removed.
     */
   void removeNode(DictionaryBagNode<K,T>* node);

   
   // ====== Private data ===================================================
   private:
   cardinal                Count;
   DictionaryBagNode<K,T>  FirstNode;
   DictionaryBagNode<K,T>* Pointer;
};


#include "dictionarybag.icc"
#include "dictionarybag.cc"


#endif

Generated by: viper@odin on Fri Feb 23 12:41:26 2001, using kdoc 2.0a36.