/src/libzmq/src/array.hpp
Line | Count | Source (jump to first uncovered line) |
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 | 8.07k | array_item_t () : _array_index (-1) {} zmq::array_item_t<1>::array_item_t() Line | Count | Source | 29 | 1.28k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<2>::array_item_t() Line | Count | Source | 29 | 1.28k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<3>::array_item_t() Line | Count | Source | 29 | 1.28k | array_item_t () : _array_index (-1) {} |
zmq::array_item_t<0>::array_item_t() Line | Count | Source | 29 | 4.21k | 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 | 14.9k | void set_array_index (int index_) { _array_index = index_; } zmq::array_item_t<0>::set_array_index(int) Line | Count | Source | 35 | 8.43k | void set_array_index (int index_) { _array_index = index_; } |
zmq::array_item_t<3>::set_array_index(int) Line | Count | Source | 35 | 1.40k | void set_array_index (int index_) { _array_index = index_; } |
zmq::array_item_t<2>::set_array_index(int) Line | Count | Source | 35 | 5.14k | void set_array_index (int index_) { _array_index = index_; } |
Unexecuted instantiation: zmq::array_item_t<1>::set_array_index(int) |
36 | | |
37 | 8.77k | int get_array_index () const { return _array_index; } zmq::array_item_t<0>::get_array_index() const Line | Count | Source | 37 | 4.21k | int get_array_index () const { return _array_index; } |
zmq::array_item_t<3>::get_array_index() const Line | Count | Source | 37 | 701 | int get_array_index () const { return _array_index; } |
zmq::array_item_t<2>::get_array_index() const Line | Count | Source | 37 | 3.85k | 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 | 15.0k | size_type size () { return _items.size (); } zmq::array_t<zmq::socket_base_t, 0>::size() Line | Count | Source | 56 | 4.15k | size_type size () { return _items.size (); } |
zmq::array_t<zmq::pipe_t, 3>::size() Line | Count | Source | 56 | 10.2k | size_type size () { return _items.size (); } |
zmq::array_t<zmq::pipe_t, 2>::size() Line | Count | Source | 56 | 643 | size_type size () { return _items.size (); } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::size() |
57 | | |
58 | 20.8k | bool empty () { return _items.empty (); } zmq::array_t<zmq::socket_base_t, 0>::empty() Line | Count | Source | 58 | 16.6k | bool empty () { return _items.empty (); } |
zmq::array_t<zmq::pipe_t, 2>::empty() Line | Count | Source | 58 | 4.15k | bool empty () { return _items.empty (); } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::empty() |
59 | | |
60 | 5.61k | T *&operator[] (size_type index_) { return _items[index_]; } zmq::array_t<zmq::socket_base_t, 0>::operator[](unsigned long) Line | Count | Source | 60 | 4.21k | T *&operator[] (size_type index_) { return _items[index_]; } |
zmq::array_t<zmq::pipe_t, 3>::operator[](unsigned long) Line | Count | Source | 60 | 1.40k | 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 | 5.56k | { |
64 | 5.56k | if (item_) |
65 | 5.56k | static_cast<item_t *> (item_)->set_array_index ( |
66 | 5.56k | static_cast<int> (_items.size ())); |
67 | 5.56k | _items.push_back (item_); |
68 | 5.56k | } zmq::array_t<zmq::socket_base_t, 0>::push_back(zmq::socket_base_t*) Line | Count | Source | 63 | 4.21k | { | 64 | 4.21k | if (item_) | 65 | 4.21k | static_cast<item_t *> (item_)->set_array_index ( | 66 | 4.21k | static_cast<int> (_items.size ())); | 67 | 4.21k | _items.push_back (item_); | 68 | 4.21k | } |
zmq::array_t<zmq::pipe_t, 3>::push_back(zmq::pipe_t*) Line | Count | Source | 63 | 701 | { | 64 | 701 | if (item_) | 65 | 701 | static_cast<item_t *> (item_)->set_array_index ( | 66 | 701 | static_cast<int> (_items.size ())); | 67 | 701 | _items.push_back (item_); | 68 | 701 | } |
zmq::array_t<zmq::pipe_t, 2>::push_back(zmq::pipe_t*) Line | Count | Source | 63 | 643 | { | 64 | 643 | if (item_) | 65 | 643 | static_cast<item_t *> (item_)->set_array_index ( | 66 | 643 | static_cast<int> (_items.size ())); | 67 | 643 | _items.push_back (item_); | 68 | 643 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::push_back(zmq::pipe_t*) |
69 | | |
70 | | void erase (T *item_) |
71 | 5.56k | { |
72 | 5.56k | erase (static_cast<item_t *> (item_)->get_array_index ()); |
73 | 5.56k | } zmq::array_t<zmq::socket_base_t, 0>::erase(zmq::socket_base_t*) Line | Count | Source | 71 | 4.21k | { | 72 | 4.21k | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 4.21k | } |
zmq::array_t<zmq::pipe_t, 3>::erase(zmq::pipe_t*) Line | Count | Source | 71 | 701 | { | 72 | 701 | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 701 | } |
zmq::array_t<zmq::pipe_t, 2>::erase(zmq::pipe_t*) Line | Count | Source | 71 | 643 | { | 72 | 643 | erase (static_cast<item_t *> (item_)->get_array_index ()); | 73 | 643 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::erase(zmq::pipe_t*) |
74 | | |
75 | | void erase (size_type index_) |
76 | 5.56k | { |
77 | 5.56k | if (_items.empty ()) |
78 | 0 | return; |
79 | 5.56k | static_cast<item_t *> (_items.back ()) |
80 | 5.56k | ->set_array_index (static_cast<int> (index_)); |
81 | | |
82 | 5.56k | _items[index_] = _items.back (); |
83 | 5.56k | _items.pop_back (); |
84 | 5.56k | } zmq::array_t<zmq::socket_base_t, 0>::erase(unsigned long) Line | Count | Source | 76 | 4.21k | { | 77 | 4.21k | if (_items.empty ()) | 78 | 0 | return; | 79 | 4.21k | static_cast<item_t *> (_items.back ()) | 80 | 4.21k | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 4.21k | _items[index_] = _items.back (); | 83 | 4.21k | _items.pop_back (); | 84 | 4.21k | } |
zmq::array_t<zmq::pipe_t, 3>::erase(unsigned long) Line | Count | Source | 76 | 701 | { | 77 | 701 | if (_items.empty ()) | 78 | 0 | return; | 79 | 701 | static_cast<item_t *> (_items.back ()) | 80 | 701 | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 701 | _items[index_] = _items.back (); | 83 | 701 | _items.pop_back (); | 84 | 701 | } |
zmq::array_t<zmq::pipe_t, 2>::erase(unsigned long) Line | Count | Source | 76 | 643 | { | 77 | 643 | if (_items.empty ()) | 78 | 0 | return; | 79 | 643 | static_cast<item_t *> (_items.back ()) | 80 | 643 | ->set_array_index (static_cast<int> (index_)); | 81 | | | 82 | 643 | _items[index_] = _items.back (); | 83 | 643 | _items.pop_back (); | 84 | 643 | } |
Unexecuted instantiation: zmq::array_t<zmq::pipe_t, 1>::erase(unsigned long) |
85 | | |
86 | | void swap (size_type index1_, size_type index2_) |
87 | 1.92k | { |
88 | 1.92k | if (_items[index1_]) |
89 | 1.92k | static_cast<item_t *> (_items[index1_]) |
90 | 1.92k | ->set_array_index (static_cast<int> (index2_)); |
91 | 1.92k | if (_items[index2_]) |
92 | 1.92k | static_cast<item_t *> (_items[index2_]) |
93 | 1.92k | ->set_array_index (static_cast<int> (index1_)); |
94 | 1.92k | std::swap (_items[index1_], _items[index2_]); |
95 | 1.92k | } zmq::array_t<zmq::pipe_t, 2>::swap(unsigned long, unsigned long) Line | Count | Source | 87 | 1.92k | { | 88 | 1.92k | if (_items[index1_]) | 89 | 1.92k | static_cast<item_t *> (_items[index1_]) | 90 | 1.92k | ->set_array_index (static_cast<int> (index2_)); | 91 | 1.92k | if (_items[index2_]) | 92 | 1.92k | static_cast<item_t *> (_items[index2_]) | 93 | 1.92k | ->set_array_index (static_cast<int> (index1_)); | 94 | 1.92k | std::swap (_items[index1_], _items[index2_]); | 95 | 1.92k | } |
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.21k | { |
101 | 3.21k | return static_cast<size_type> ( |
102 | 3.21k | static_cast<item_t *> (item_)->get_array_index ()); |
103 | 3.21k | } zmq::array_t<zmq::pipe_t, 2>::index(zmq::pipe_t*) Line | Count | Source | 100 | 3.21k | { | 101 | 3.21k | return static_cast<size_type> ( | 102 | 3.21k | static_cast<item_t *> (item_)->get_array_index ()); | 103 | 3.21k | } |
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 |