array.hpp 4.27 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
5

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

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq 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.
25

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

30 31
#ifndef __ZMQ_ARRAY_INCLUDED__
#define __ZMQ_ARRAY_INCLUDED__
32 33 34 35

#include <vector>
#include <algorithm>

36 37
#include "macros.hpp"

38 39
namespace zmq
{
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
//  Implementation of fast arrays with O(1) access, insertion and
//  removal. The array stores pointers rather than objects.
//  O(1) is achieved by making items inheriting from
//  array_item_t<ID> class which internally stores the position
//  in the array.
//  The ID template argument is used to differentiate among arrays
//  and thus let an object be stored in different arrays.

//  Base class for objects stored in the array. If you want to store
//  same object in multiple arrays, each of those arrays has to have
//  different ID. The item itself has to be derived from instantiations of
//  array_item_t template for all relevant IDs.

template <int ID = 0> class array_item_t
{
  public:
56
    array_item_t () : _array_index (-1) {}
57 58 59

    //  The destructor doesn't have to be virtual. It is made virtual
    //  just to keep ICC and code checking tools from complaining.
60
    virtual ~array_item_t () ZMQ_DEFAULT;
61

62
    void set_array_index (int index_) { _array_index = index_; }
63

64
    int get_array_index () const { return _array_index; }
65

66
  private:
67
    int _array_index;
68

69
    ZMQ_NON_COPYABLE_NOR_MOVABLE (array_item_t)
70
};
71 72


73 74 75 76 77 78 79 80
template <typename T, int ID = 0> class array_t
{
  private:
    typedef array_item_t<ID> item_t;

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

81
    array_t () ZMQ_DEFAULT;
82

83
    size_type size () { return _items.size (); }
84

85
    bool empty () { return _items.empty (); }
86

87
    T *&operator[] (size_type index_) { return _items[index_]; }
88

89
    void push_back (T *item_)
90 91
    {
        if (item_)
92 93
            static_cast<item_t *> (item_)->set_array_index (
              static_cast<int> (_items.size ()));
94
        _items.push_back (item_);
95
    }
96

97
    void erase (T *item_)
98
    {
99
        erase (static_cast<item_t *> (item_)->get_array_index ());
100
    }
101

102
    void erase (size_type index_)
103
    {
xqcool's avatar
xqcool committed
104 105
        if (_items.empty ())
            return;
106 107 108
        static_cast<item_t *> (_items.back ())
          ->set_array_index (static_cast<int> (index_));

109 110
        _items[index_] = _items.back ();
        _items.pop_back ();
111 112
    }

113
    void swap (size_type index1_, size_type index2_)
114
    {
115
        if (_items[index1_])
116 117
            static_cast<item_t *> (_items[index1_])
              ->set_array_index (static_cast<int> (index2_));
118
        if (_items[index2_])
119 120
            static_cast<item_t *> (_items[index2_])
              ->set_array_index (static_cast<int> (index1_));
121
        std::swap (_items[index1_], _items[index2_]);
122 123
    }

124
    void clear () { _items.clear (); }
125

126
    static size_type index (T *item_)
127
    {
128 129
        return static_cast<size_type> (
          static_cast<item_t *> (item_)->get_array_index ());
130 131 132 133
    }

  private:
    typedef std::vector<T *> items_t;
134
    items_t _items;
135

136
    ZMQ_NON_COPYABLE_NOR_MOVABLE (array_t)
137
};
138 139 140
}

#endif