javahash.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JAVAHASH_HPP
  14. #define JAVAHASH_HPP
  15. #include "jiface.hpp"
  16. #include "jlib.hpp"
  17. #include "jobserve.hpp"
  18. #include "jiter.hpp"
  19. #include "jsuperhash.hpp"
  20. interface jlib_decl IJavaHashable : extends IObservable
  21. {
  22. public:
  23. virtual unsigned getHash() const = 0;
  24. virtual bool equals(const IJavaHashable & other) const = 0;
  25. };
  26. interface ICompare;
  27. template <class ELEMENT>
  28. class JavaHashTableOf
  29. : public SuperHashTableOf<ELEMENT, ELEMENT>, implements IObserver
  30. {
  31. typedef JavaHashTableOf<ELEMENT> _SELF;
  32. private:
  33. virtual void onAdd(void *next) override;
  34. virtual void onRemove(void *) override;
  35. virtual unsigned getHashFromElement(const void * et) const override
  36. {
  37. return static_cast<const ELEMENT *>(et)->getHash();
  38. }
  39. virtual unsigned getHashFromFindParam(const void * et) const override
  40. {
  41. return static_cast<const ELEMENT *>(et)->getHash();
  42. }
  43. virtual const void * getFindParam(const void * et) const override { return et; }
  44. virtual bool matchesFindParam(const void * et, const void * key, unsigned fphash __attribute__((unused))) const override
  45. {
  46. return static_cast<const ELEMENT *>(et)->equals(*static_cast<const ELEMENT *>(key));
  47. }
  48. public:
  49. JavaHashTableOf(bool _keep)
  50. : SuperHashTableOf<ELEMENT, ELEMENT>(), keep(_keep) {}
  51. JavaHashTableOf(unsigned initsize, bool _keep)
  52. : SuperHashTableOf<ELEMENT, ELEMENT>(initsize), keep(_keep) {}
  53. ~JavaHashTableOf() { _SELF::_releaseAll(); }
  54. IMPLEMENT_IINTERFACE
  55. bool addOwn(ELEMENT &);
  56. bool replaceOwn(ELEMENT &);
  57. ELEMENT * findLink(const ELEMENT &) const;
  58. virtual bool onNotify(INotification & notify);
  59. ELEMENT * findCompare(ICompare *icmp,void * (ELEMENT::*getPtr)() const, unsigned hash, const void *val) const;
  60. IMPLEMENT_SUPERHASHTABLEOF_REF_FIND(ELEMENT, ELEMENT)
  61. private:
  62. bool keep;
  63. };
  64. template <class ELEMENT>
  65. class JavaHashIteratorOf : public SuperHashIteratorOf<ELEMENT>
  66. {
  67. typedef JavaHashIteratorOf<ELEMENT> _SELF;
  68. public:
  69. JavaHashIteratorOf(JavaHashTableOf<ELEMENT> & _table, bool linkTable=true)
  70. : SuperHashIteratorOf<ELEMENT>(_table, linkTable) {}
  71. inline ELEMENT & get() { ELEMENT & et = _SELF::query(); et.Link(); return et; }
  72. };
  73. #endif