array.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_ARRAY_INCLUDED__
4 #define __ZMQ_ARRAY_INCLUDED__
5 
6 #include <vector>
7 #include <algorithm>
8 
9 #include "macros.hpp"
10 
11 namespace zmq
12 {
13 // Implementation of fast arrays with O(1) access, insertion and
14 // removal. The array stores pointers rather than objects.
15 // O(1) is achieved by making items inheriting from
16 // array_item_t<ID> class which internally stores the position
17 // in the array.
18 // The ID template argument is used to differentiate among arrays
19 // and thus let an object be stored in different arrays.
20 
21 // Base class for objects stored in the array. If you want to store
22 // same object in multiple arrays, each of those arrays has to have
23 // different ID. The item itself has to be derived from instantiations of
24 // array_item_t template for all relevant IDs.
25 
26 template <int ID = 0> class array_item_t
27 {
28  public:
30 
31  // The destructor doesn't have to be virtual. It is made virtual
32  // just to keep ICC and code checking tools from complaining.
33  virtual ~array_item_t () ZMQ_DEFAULT;
34 
36 
37  int get_array_index () const { return _array_index; }
38 
39  private:
41 
43 };
44 
45 
46 template <typename T, int ID = 0> class array_t
47 {
48  private:
50 
51  public:
52  typedef typename std::vector<T *>::size_type size_type;
53 
55 
56  size_type size () { return _items.size (); }
57 
58  bool empty () { return _items.empty (); }
59 
61 
62  void push_back (T *item_)
63  {
64  if (item_)
65  static_cast<item_t *> (item_)->set_array_index (
66  static_cast<int> (_items.size ()));
67  _items.push_back (item_);
68  }
69 
70  void erase (T *item_)
71  {
72  erase (static_cast<item_t *> (item_)->get_array_index ());
73  }
74 
76  {
77  if (_items.empty ())
78  return;
79  static_cast<item_t *> (_items.back ())
80  ->set_array_index (static_cast<int> (index_));
81 
82  _items[index_] = _items.back ();
83  _items.pop_back ();
84  }
85 
86  void swap (size_type index1_, size_type index2_)
87  {
88  if (_items[index1_])
89  static_cast<item_t *> (_items[index1_])
90  ->set_array_index (static_cast<int> (index2_));
91  if (_items[index2_])
92  static_cast<item_t *> (_items[index2_])
93  ->set_array_index (static_cast<int> (index1_));
94  std::swap (_items[index1_], _items[index2_]);
95  }
96 
97  void clear () { _items.clear (); }
98 
99  static size_type index (T *item_)
100  {
101  return static_cast<size_type> (
102  static_cast<item_t *> (item_)->get_array_index ());
103  }
104 
105  private:
106  typedef std::vector<T *> items_t;
108 
110 };
111 }
112 
113 #endif
zmq::array_t::_items
items_t _items
Definition: array.hpp:107
zmq::array_item_t::~array_item_t
virtual ~array_item_t() ZMQ_DEFAULT
ZMQ_DEFAULT
#define ZMQ_DEFAULT
Definition: macros.hpp:43
zmq::array_item_t::array_item_t
array_item_t()
Definition: array.hpp:29
zmq::array_item_t
Definition: array.hpp:26
zmq::array_t::items_t
std::vector< T * > items_t
Definition: array.hpp:106
T
#define T(upbtypeconst, upbtype, ctype, default_value)
zmq::array_t::clear
void clear()
Definition: array.hpp:97
zmq::array_t::erase
void erase(T *item_)
Definition: array.hpp:70
zmq
Definition: zmq.hpp:229
zmq::array_t::item_t
array_item_t< ID > item_t
Definition: array.hpp:49
macros.hpp
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::array_t::index
static size_type index(T *item_)
Definition: array.hpp:99
std::swap
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: json.h:1226
zmq::array_t::array_t
array_t() ZMQ_DEFAULT
zmq::array_t::push_back
void push_back(T *item_)
Definition: array.hpp:62
zmq::array_item_t::get_array_index
int get_array_index() const
Definition: array.hpp:37
index_
int index_
Definition: gmock-matchers_test.cc:6752
zmq::array_item_t::_array_index
int _array_index
Definition: array.hpp:40
zmq::array_t::erase
void erase(size_type index_)
Definition: array.hpp:75
zmq::array_t::size_type
std::vector< T * >::size_type size_type
Definition: array.hpp:52
zmq::array_t::operator[]
T *& operator[](size_type index_)
Definition: array.hpp:60
zmq::array_item_t::set_array_index
void set_array_index(int index_)
Definition: array.hpp:35
size
GLsizeiptr size
Definition: glcorearb.h:2943
zmq::array_t::empty
bool empty()
Definition: array.hpp:58
zmq::array_t::swap
void swap(size_type index1_, size_type index2_)
Definition: array.hpp:86
zmq::array_t
Definition: array.hpp:46


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48