bucket_iterator.h - Engine C API Reference

bucket_iterator.h
  1. #pragma once
  2. namespace stingray_plugin_foundation {
  3. template <class CONTAINER, class ITEM>
  4. class ConstBucketIterator;
  5. // A helper class that makes an iterator for a bucket based data structure.
  6. // Such a data structure has a number of random access buckets which may be
  7. // "valid" or not. The iterator iterates over the valid buckets only.
  8. template <class CONTAINER, class ITEM>
  9. class BucketIterator {
  10. public:
  11. typedef BucketIterator<CONTAINER, ITEM> iterator;
  12. friend class ConstBucketIterator<CONTAINER, ITEM>;
  13. typedef ITEM value_type;
  14. typedef ITEM* pointer;
  15. typedef ITEM& reference;
  16. typedef ptrdiff_t difference_type;
  17. typedef std::forward_iterator_tag iterator_category;
  18. inline BucketIterator() : _i(0) { }
  19. inline BucketIterator(CONTAINER &container, unsigned i);
  20. inline BucketIterator(const BucketIterator &other);
  21. inline bool operator==(const iterator &rhs) const { return cmp(rhs) == 0; }
  22. inline bool operator!=(const iterator &rhs) const { return cmp(rhs) != 0; }
  23. inline bool operator<(const iterator &rhs) const { return cmp(rhs) < 0; }
  24. inline bool operator<=(const iterator &rhs) const { return cmp(rhs) <= 0; }
  25. inline bool operator>(const iterator &rhs) const { return cmp(rhs) > 0; }
  26. inline bool operator>=(const iterator &rhs) const { return cmp(rhs) >= 0; }
  27. inline iterator operator++();
  28. inline iterator operator++(int);
  29. inline ITEM &operator*() { return _container->bucket_value(_i); }
  30. inline ITEM *operator->() { return &_container->bucket_value(_i); }
  31. private:
  32. inline int cmp(const iterator &rhs) const {
  33. if(_container < rhs._container) return -1;
  34. else if(_container > rhs._container) return 1;
  35. else if(_i < rhs._i) return -1;
  36. else if(_i > rhs._i) return 1;
  37. else return 0;
  38. }
  39. void advance_to_valid_bucket();
  40. CONTAINER *_container;
  41. unsigned _i;
  42. };
  43. // A helper class that makes an iterator for a bucket based data structure.
  44. // Such a data structure has a number of random access buckets which may be
  45. // "valid" or not. The iterator iterates over the valid buckets only.
  46. template <class CONTAINER, class ITEM>
  47. class ConstBucketIterator {
  48. public:
  49. typedef ConstBucketIterator<CONTAINER, ITEM> iterator;
  50. typedef ITEM value_type;
  51. typedef ITEM* pointer;
  52. typedef ITEM& reference;
  53. typedef ptrdiff_t difference_type;
  54. typedef std::forward_iterator_tag iterator_category;
  55. inline ConstBucketIterator() : _i(0) {}
  56. inline ConstBucketIterator(const CONTAINER &container, unsigned i);
  57. inline ConstBucketIterator(const ConstBucketIterator<CONTAINER, ITEM> &rhs);
  58. inline ConstBucketIterator(const BucketIterator<CONTAINER, ITEM> &rhs);
  59. inline bool operator==(const iterator &rhs) const { return cmp(rhs) == 0; }
  60. inline bool operator!=(const iterator &rhs) const { return cmp(rhs) != 0; }
  61. inline bool operator<(const iterator &rhs) const { return cmp(rhs) < 0; }
  62. inline bool operator<=(const iterator &rhs) const { return cmp(rhs) <= 0; }
  63. inline bool operator>(const iterator &rhs) const { return cmp(rhs) > 0; }
  64. inline bool operator>=(const iterator &rhs) const { return cmp(rhs) >= 0; }
  65. inline iterator operator++();
  66. inline iterator operator++(int);
  67. inline const ITEM &operator*() { return _container->bucket_value(_i); }
  68. inline const ITEM *operator->() { return &_container->bucket_value(_i); }
  69. private:
  70. inline int cmp(const iterator &rhs) const {if (_container != rhs._container) return (int)(_container - rhs._container); return (int)(_i - rhs._i);}
  71. void advance_to_valid_bucket();
  72. const CONTAINER *_container;
  73. unsigned _i;
  74. };
  75. } // namespace stingray_plugin_foundation
  76. #include "bucket_iterator.inl"