PointerTable.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: PointerTable.h /main/1 1996/07/29 17:01:44 cde-hp $ */
  24. // Copyright (c) 1994 James Clark
  25. // See the file COPYING for copying permission.
  26. #ifndef PointerTable_INCLUDED
  27. #define PointerTable_INCLUDED 1
  28. #include "Vector.h"
  29. #include "Boolean.h"
  30. #include <stddef.h>
  31. #ifdef SP_NAMESPACE
  32. namespace SP_NAMESPACE {
  33. #endif
  34. template<class P, class K, class HF, class KF> class PointerTableIter;
  35. template<class P, class K, class HF, class KF>
  36. class PointerTable {
  37. void constraints() {
  38. P p(0);
  39. K key = KF::key(*p);
  40. unsigned long n = HF::hash(key);
  41. n = 0; // prevent warning
  42. }
  43. public:
  44. PointerTable();
  45. P insert(P, Boolean replace = 0);
  46. // Return a reference so that it is possible to do
  47. // lookups into a table of smart-pointers from multiple threads.
  48. const P &lookup(const K &) const;
  49. P remove(const K &);
  50. size_t count() const { return used_; }
  51. void clear();
  52. void swap(PointerTable<P, K, HF, KF> &);
  53. protected:
  54. size_t used_;
  55. size_t usedLimit_;
  56. Vector<P> vec_;
  57. P null_;
  58. size_t startIndex(const K &k) const {
  59. return size_t(HF::hash(k) & (vec_.size() - 1));
  60. }
  61. size_t nextIndex(size_t i) const {
  62. return i == 0 ? vec_.size() - 1 : i - 1;
  63. }
  64. friend class PointerTableIter<P, K, HF, KF>;
  65. };
  66. template<class P, class K, class HF, class KF>
  67. class PointerTableIter {
  68. public:
  69. PointerTableIter(const PointerTable<P, K, HF, KF> &);
  70. const P &next();
  71. private:
  72. const PointerTable<P, K, HF, KF> *tablePtr_;
  73. size_t i_;
  74. };
  75. #ifdef SP_NAMESPACE
  76. }
  77. #endif
  78. #endif /* not PointerTable_INCLUDED */
  79. #ifdef SP_DEFINE_TEMPLATES
  80. #include "PointerTable.C"
  81. #endif