summaryrefslogtreecommitdiff
path: root/src/array.hpp
blob: e7b526676bc353b339ad3f798b44bec3c7387ff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ZMQ_ARRAY_INCLUDED__
#define __ZMQ_ARRAY_INCLUDED__

#include <vector>
#include <algorithm>

namespace zmq
{

    //  Base class for objects stored in the array. Note that each object can
    //  be stored in at most two arrays. This is needed specifically in the
    //  case where single pipe object is stored both in array of inbound pipes
    //  and in the array of outbound pipes.

    class array_item_t
    {
    public:

        inline array_item_t () :
            array_index1 (-1),
            array_index2 (-1)
        {
        }

        //  The destructor doesn't have to be virtual. It is mad virtual
        //  just to keep ICC and code checking tools from complaining.
        inline virtual ~array_item_t ()
        {
        }

        inline void set_array_index1 (int index_)
        {
            array_index1 = index_;
        }

        inline int get_array_index1 ()
        {
            return array_index1;
        }

        inline void set_array_index2 (int index_)
        {
            array_index2 = index_;
        }

        inline int get_array_index2 ()
        {
            return array_index2;
        }

    private:

        int array_index1;
        int array_index2;

        array_item_t (const array_item_t&);
        const array_item_t &operator = (const array_item_t&);
    };

    //  Fast array implementation with O(1) access to item, insertion and
    //  removal. Array stores pointers rather than objects. The objects have
    //  to be derived from array_item_t class, thus they can be stored in
    //  two arrays. Template parameter N specifies which index in array_item_t
    //  to use.

    template <typename T, int N = 1> class array_t
    {
    public:

        typedef typename std::vector <T*>::size_type size_type;

        inline array_t ()
        {
        }

        inline ~array_t ()
        {
        }

        inline size_type size ()
        {
            return items.size ();
        }

        inline bool empty ()
        {
            return items.empty ();
        }

        inline T *&operator [] (size_type index_)
        {
            return items [index_];
        }

        inline void push_back (T *item_)
        {
            if (item_) {
                if (N == 1)
                    item_->set_array_index1 ((int) items.size ());
                else
                    item_->set_array_index2 ((int) items.size ());
            }
            items.push_back (item_);
        }

        inline void erase (T *item_)
        {
            if (N == 1)
                erase (item_->get_array_index1 ());
            else
                erase (item_->get_array_index2 ());
        }

        inline void erase (size_type index_) {
            if (items.back ()) {
                if (N == 1)
                    items.back ()->set_array_index1 ((int) index_);
                else
                    items.back ()->set_array_index2 ((int) index_);
            }
            items [index_] = items.back ();
            items.pop_back ();
        }

        inline void swap (size_type index1_, size_type index2_)
        {
            if (N == 1) {
		        if (items [index1_])
		            items [index1_]->set_array_index1 ((int) index2_);
		        if (items [index2_])
		            items [index2_]->set_array_index1 ((int) index1_);
            }
            else {
		        if (items [index1_])
		            items [index1_]->set_array_index2 ((int) index2_);
		        if (items [index2_])
		            items [index2_]->set_array_index2 ((int) index1_);
            }
            std::swap (items [index1_], items [index2_]);
        }

        inline void clear ()
        {
            items.clear ();
        }

        inline size_type index (T *item_)
        {
            if (N == 1)
                return (size_type) item_->get_array_index1 ();
            else
                return (size_type) item_->get_array_index2 ();
        }

    private:

        typedef std::vector <T*> items_t;
        items_t items;

        array_t (const array_t&);
        const array_t &operator = (const array_t&);
    };

}

#endif