bstmap.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2002 Adrian Thurston <thurston@cs.queensu.ca>
  3. */
  4. /* This file is part of Aapl.
  5. *
  6. * Aapl is free software; you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation; either version 2.1 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
  12. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Aapl; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _AAPL_BSTMAP_H
  21. #define _AAPL_BSTMAP_H
  22. #include "compare.h"
  23. #include "vector.h"
  24. #ifdef AAPL_NAMESPACE
  25. namespace Aapl {
  26. #endif
  27. /**
  28. * \brief Element for BstMap.
  29. *
  30. * Stores the key and value pair.
  31. */
  32. template <class Key, class Value> struct BstMapEl
  33. {
  34. BstMapEl() {}
  35. BstMapEl(const Key &key) : key(key) {}
  36. BstMapEl(const Key &key, const Value &val) : key(key), value(val) {}
  37. /** \brief The key */
  38. Key key;
  39. /** \brief The value. */
  40. Value value;
  41. };
  42. #ifdef AAPL_NAMESPACE
  43. }
  44. #endif
  45. /**
  46. * \addtogroup bst
  47. * @{
  48. */
  49. /**
  50. * \class BstMap
  51. * \brief Binary search table for key and value pairs.
  52. *
  53. * BstMap stores key and value pairs in each element. The key and value can be
  54. * any type. A compare class for the key must be supplied.
  55. */
  56. /*@}*/
  57. #define BST_TEMPL_DECLARE class Key, class Value, \
  58. class Compare = CmpOrd<Key>, class Resize = ResizeExpn
  59. #define BST_TEMPL_DEF class Key, class Value, class Compare, class Resize
  60. #define BST_TEMPL_USE Key, Value, Compare, Resize
  61. #define GET_KEY(el) ((el).key)
  62. #define BstTable BstMap
  63. #define Element BstMapEl<Key, Value>
  64. #define BSTMAP
  65. #include "bstcommon.h"
  66. #undef BST_TEMPL_DECLARE
  67. #undef BST_TEMPL_DEF
  68. #undef BST_TEMPL_USE
  69. #undef GET_KEY
  70. #undef BstTable
  71. #undef Element
  72. #undef BSTMAP
  73. /**
  74. * \fn BstMap::insert(const Key &key, BstMapEl<Key, Value> **lastFound)
  75. * \brief Insert the given key.
  76. *
  77. * If the given key does not already exist in the table then a new element
  78. * having key is inserted. They key copy constructor and value default
  79. * constructor are used to place the pair in the table. If lastFound is given,
  80. * it is set to the new entry created. If the insert fails then lastFound is
  81. * set to the existing pair of the same key.
  82. *
  83. * \returns The new element created upon success, null upon failure.
  84. */
  85. /**
  86. * \fn BstMap::insertMulti(const Key &key)
  87. * \brief Insert the given key even if it exists already.
  88. *
  89. * If the key exists already then the new element having key is placed next
  90. * to some other pair of the same key. InsertMulti cannot fail. The key copy
  91. * constructor and the value default constructor are used to place the pair in
  92. * the table.
  93. *
  94. * \returns The new element created.
  95. */
  96. #endif /* _AAPL_BSTMAP_H */