/src/libzmq/src/array.hpp
Line | Count | Source |
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: |
29 | 6.09k | array_item_t () : _array_index (-1) {}zmq::array_item_t<1>::array_item_t() Line | Count | Source | 29 | 1.40k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<2>::array_item_t() Line | Count | Source | 29 | 1.40k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<3>::array_item_t() Line | Count | Source | 29 | 1.40k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<0>::array_item_t() Line | Count | Source | 29 | 1.87k | array_item_t () : _array_index (-1) {} |
|
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 | | |
35 | 10.9k | void set_array_index (int index_) { _array_index = index_; }zmq::array_item_t<0>::set_array_index(int) Line | Count | Source | 35 | 3.74k | void set_array_index (int index_) { _array_index = index_; } |
zmq::array_item_t<3>::set_array_index(int) Line | Count | Source | 35 | 1.55k | void set_array_index (int index_) { _array_index = index_; } |
zmq::array_item_t<2>::set_array_index(int) Line | Count | Source | 35 | 5.63k | void set_array_index (int index_) { _array_index = index_; } |
Unexecuted instantiation: zmq::array_item_t<1>::set_array_index(int) |
36 | | |
37 | 6.87k | int get_array_index () const { return _array_index; }zmq::array_item_t<0>::get_array_index() const Line | Count | Source | 37 | 1.87k | int get_array_index () const { return _array_index; } |
zmq::array_item_t<3>::get_array_index() const Line | Count | Source | 37 | 778 | int get_array_index () const { return _array_index; } |
zmq::array_item_t<2>::get_array_index() const Line | Count | Source | 37 | 4.22k | int get_array_index () const { return _array_index; } |
Unexecuted instantiation: zmq::array_item_t<1>::get_array_index() const |
38 | | |
39 | | private: |
40 | | int _array_index; |
41 | | |
42 | | ZMQ_NON_COPYABLE_NOR_MOVABLE (array_item_t) |
43 | | }; |
44 | | |
45 | | |
46 | | template <typename T, int ID = 0> class array_t |
47 | | { |
48 | | private: |
49 | | typedef array_item_t<ID> item_t; |
50 | | |
51 | | public: |
52 | | typedef typename std::vector<T *>::size_type size_type; |
53 | | |
54 | | array_t () ZMQ_DEFAULT; |
55 | | |
56 | 6.25k | size_type size () { return _items.size (); }zmq::array_t<zmq::socket_base_t, 0>::size() Line | Count | Source | 56 | 1.80k | size_type size () { return _items.size (); } |
zmq::array_t<zmq::pipe_t, 3>::size() Line | Count | Source | 56 | 3.74k | size_type size () { return _items.size (); } |
zmq::array_t<zmq::pipe_t, 2>::size() Line | Count | Source | 56 | 704 | size_type size () { return _items.size (); } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::size() |
57 | | |
58 | 9.07k | bool empty () { return _items.empty (); }zmq::array_t<zmq::socket_base_t, 0>::empty() Line | Count | Source | 58 | 7.27k | bool empty () { return _items.empty (); } |
zmq::array_t<zmq::pipe_t, 2>::empty() Line | Count | Source | 58 | 1.80k | bool empty () { return _items.empty (); } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::empty() |
59 | | |
60 | 3.43k | T *&operator[] (size_type index_) { return _items[index_]; }zmq::array_t<zmq::socket_base_t, 0>::operator[](unsigned long) Line | Count | Source | 60 | 1.87k | T *&operator[] (size_type index_) { return _items[index_]; } |
zmq::array_t<zmq::pipe_t, 3>::operator[](unsigned long) Line | Count | Source | 60 | 1.55k | T *&operator[] (size_type index_) { return _items[index_]; } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 2>::operator[](unsigned long) Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::operator[](unsigned long) |
61 | | |
62 | | void push_back (T *item_) |
63 | 3.35k | { |
64 | 3.35k | if (item_) |
65 | 3.35k | static_cast<item_t *> (item_)->set_array_index ( |
66 | 3.35k | static_cast<int> (_items.size ())); |
67 | 3.35k | _items.push_back (item_); |
68 | 3.35k | } zmq::array_t<zmq::socket_base_t, 0>::push_back(zmq::socket_base_t*) Line | Count | Source | 63 | 1.87k | { | 64 | 1.87k | if (item_) | 65 | 1.87k | static_cast<item_t *> (item_)->set_array_index ( | 66 | 1.87k | static_cast<int> (_items.size ())); | 67 | 1.87k | _items.push_back (item_); | 68 | 1.87k | } |
zmq::array_t<zmq::pipe_t, 3>::push_back(zmq::pipe_t*) Line | Count | Source | 63 | 778 | { | 64 | 778 | if (item_) | 65 | 778 | static_cast<item_t *> (item_)->set_array_index ( | 66 | 778 | static_cast<int> (_items.size ())); | 67 | 778 | _items.push_back (item_); | 68 | 778 | } |
zmq::array_t<zmq::pipe_t, 2>::push_back(zmq::pipe_t*) Line | Count | Source | 63 | 704 | { | 64 | 704 | if (item_) | 65 | 704 | static_cast<item_t *> (item_)->set_array_index ( | 66 | 704 | static_cast<int> (_items.size ())); | 67 | 704 | _items.push_back (item_); | 68 | 704 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::push_back(zmq::pipe_t*) |
69 | | |
70 | | void erase (T *item_) |
71 | 3.35k | { |
72 | 3.35k | erase (static_cast<item_t *> (item_)->get_array_index ()); |
73 | 3.35k | } zmq::array_t<zmq::socket_base_t, 0>::erase(zmq::socket_base_t*) Line | Count | Source | 71 | 1.87k | { | 72 | 1.87k | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 1.87k | } |
zmq::array_t<zmq::pipe_t, 3>::erase(zmq::pipe_t*) Line | Count | Source | 71 | 778 | { | 72 | 778 | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 778 | } |
zmq::array_t<zmq::pipe_t, 2>::erase(zmq::pipe_t*) Line | Count | Source | 71 | 704 | { | 72 | 704 | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 704 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::erase(zmq::pipe_t*) |
74 | | |
75 | | void erase (size_type index_) |
76 | 3.35k | { |
77 | 3.35k | if (_items.empty ()) |
78 | 0 | return; |
79 | 3.35k | static_cast<item_t *> (_items.back ()) |
80 | 3.35k | ->set_array_index (static_cast<int> (index_)); |
81 | | |
82 | 3.35k | _items[index_] = _items.back (); |
83 | 3.35k | _items.pop_back (); |
84 | 3.35k | } zmq::array_t<zmq::socket_base_t, 0>::erase(unsigned long) Line | Count | Source | 76 | 1.87k | { | 77 | 1.87k | if (_items.empty ()) | 78 | 0 | return; | 79 | 1.87k | static_cast<item_t *> (_items.back ()) | 80 | 1.87k | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 1.87k | _items[index_] = _items.back (); | 83 | 1.87k | _items.pop_back (); | 84 | 1.87k | } |
zmq::array_t<zmq::pipe_t, 3>::erase(unsigned long) Line | Count | Source | 76 | 778 | { | 77 | 778 | if (_items.empty ()) | 78 | 0 | return; | 79 | 778 | static_cast<item_t *> (_items.back ()) | 80 | 778 | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 778 | _items[index_] = _items.back (); | 83 | 778 | _items.pop_back (); | 84 | 778 | } |
zmq::array_t<zmq::pipe_t, 2>::erase(unsigned long) Line | Count | Source | 76 | 704 | { | 77 | 704 | if (_items.empty ()) | 78 | 0 | return; | 79 | 704 | static_cast<item_t *> (_items.back ()) | 80 | 704 | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 704 | _items[index_] = _items.back (); | 83 | 704 | _items.pop_back (); | 84 | 704 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::erase(unsigned long) |
85 | | |
86 | | void swap (size_type index1_, size_type index2_) |
87 | 2.11k | { |
88 | 2.11k | if (_items[index1_]) |
89 | 2.11k | static_cast<item_t *> (_items[index1_]) |
90 | 2.11k | ->set_array_index (static_cast<int> (index2_)); |
91 | 2.11k | if (_items[index2_]) |
92 | 2.11k | static_cast<item_t *> (_items[index2_]) |
93 | 2.11k | ->set_array_index (static_cast<int> (index1_)); |
94 | 2.11k | std::swap (_items[index1_], _items[index2_]); |
95 | 2.11k | } zmq::array_t<zmq::pipe_t, 2>::swap(unsigned long, unsigned long) Line | Count | Source | 87 | 2.11k | { | 88 | 2.11k | if (_items[index1_]) | 89 | 2.11k | static_cast<item_t *> (_items[index1_]) | 90 | 2.11k | ->set_array_index (static_cast<int> (index2_)); | 91 | 2.11k | if (_items[index2_]) | 92 | 2.11k | static_cast<item_t *> (_items[index2_]) | 93 | 2.11k | ->set_array_index (static_cast<int> (index1_)); | 94 | 2.11k | std::swap (_items[index1_], _items[index2_]); | 95 | 2.11k | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::swap(unsigned long, unsigned long) |
96 | | |
97 | | void clear () { _items.clear (); } |
98 | | |
99 | | static size_type index (T *item_) |
100 | 3.52k | { |
101 | 3.52k | return static_cast<size_type> ( |
102 | 3.52k | static_cast<item_t *> (item_)->get_array_index ()); |
103 | 3.52k | } zmq::array_t<zmq::pipe_t, 2>::index(zmq::pipe_t*) Line | Count | Source | 100 | 3.52k | { | 101 | 3.52k | return static_cast<size_type> ( | 102 | 3.52k | static_cast<item_t *> (item_)->get_array_index ()); | 103 | 3.52k | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::index(zmq::pipe_t*) |
104 | | |
105 | | private: |
106 | | typedef std::vector<T *> items_t; |
107 | | items_t _items; |
108 | | |
109 | | ZMQ_NON_COPYABLE_NOR_MOVABLE (array_t) |
110 | | }; |
111 | | } |
112 | | |
113 | | #endif |