sbstmap.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_SBSTMAP_H
  21. #define _AAPL_SBSTMAP_H
  22. #include "compare.h"
  23. #include "svector.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 SBstMapEl
  33. {
  34. SBstMapEl() {}
  35. SBstMapEl(const Key &key) : key(key) {}
  36. SBstMapEl(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 SBstMap
  51. * \brief Copy-on-write binary search table for key and value pairs.
  52. *
  53. * This is a map style binary search table that employs the copy-on-write
  54. * mechanism for table data. BstMap stores key and value pairs in each
  55. * element. The key and value can be any type. A compare class for the key
  56. * must be supplied.
  57. */
  58. /*@}*/
  59. #define BST_TEMPL_DECLARE class Key, class Value, \
  60. class Compare = CmpOrd<Key>, class Resize = ResizeExpn
  61. #define BST_TEMPL_DEF class Key, class Value, class Compare, class Resize
  62. #define BST_TEMPL_USE Key, Value, Compare, Resize
  63. #define GET_KEY(el) ((el).key)
  64. #define BstTable SBstMap
  65. #define Vector SVector
  66. #define Table STable
  67. #define Element SBstMapEl<Key, Value>
  68. #define BSTMAP
  69. #define SHARED_BST
  70. #include "bstcommon.h"
  71. #undef BST_TEMPL_DECLARE
  72. #undef BST_TEMPL_DEF
  73. #undef BST_TEMPL_USE
  74. #undef GET_KEY
  75. #undef BstTable
  76. #undef Vector
  77. #undef Table
  78. #undef Element
  79. #undef BSTMAP
  80. #undef SHARED_BST
  81. /**
  82. * \fn SBstMap::insert(const Key &key, BstMapEl<Key, Value> **lastFound)
  83. * \brief Insert the given key.
  84. *
  85. * If the given key does not already exist in the table then a new element
  86. * having key is inserted. They key copy constructor and value default
  87. * constructor are used to place the pair in the table. If lastFound is given,
  88. * it is set to the new entry created. If the insert fails then lastFound is
  89. * set to the existing pair of the same key.
  90. *
  91. * \returns The new element created upon success, null upon failure.
  92. */
  93. /**
  94. * \fn SBstMap::insertMulti(const Key &key)
  95. * \brief Insert the given key even if it exists already.
  96. *
  97. * If the key exists already then the new element having key is placed next
  98. * to some other pair of the same key. InsertMulti cannot fail. The key copy
  99. * constructor and the value default constructor are used to place the pair in
  100. * the table.
  101. *
  102. * \returns The new element created.
  103. */
  104. #endif /* _AAPL_SBSTMAP_H */