array.hpp 4.49 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 36 37 38

#include <vector>
#include <algorithm>

namespace zmq
{

39 40 41 42
    //  Base class for objects stored in the array. If you want to store
    //  same object in mutliple 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.
43

44
    template <int ID = 0> class array_item_t
45 46 47 48
    {
    public:

        inline array_item_t () :
49
            array_index (-1)
50 51 52 53 54 55 56 57 58
        {
        }

        //  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 ()
        {
        }

59
        inline void set_array_index (int index_)
60
        {
61
            array_index = index_;
62 63
        }

64
        inline int get_array_index ()
65
        {
66
            return array_index;
67 68
        }

69 70
    private:

71
        int array_index;
72 73

        array_item_t (const array_item_t&);
74
        const array_item_t &operator = (const array_item_t&);
75 76
    };

77
    //  Fast array implementation with O(1) access to item, insertion and
78
    //  removal. Array stores pointers rather than objects. The objects have
79
    //  to be derived from array_item_t<ID> class.
80

81
    template <typename T, int ID = 0> class array_t
82
    {
83 84 85 86
    private:

        typedef array_item_t <ID> item_t;

87 88 89 90
    public:

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

91
        inline array_t ()
92 93 94
        {
        }

95
        inline ~array_t ()
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        {
        }

        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_)
        {
116 117
            if (item_)
                ((item_t*) item_)->set_array_index ((int) items.size ());
118 119 120
            items.push_back (item_);
        }

121 122
        inline void erase (T *item_) {
            erase (((item_t*) item_)->get_array_index ());
123 124 125
        }

        inline void erase (size_type index_) {
126 127
            if (items.back ())
                ((item_t*) items.back ())->set_array_index ((int) index_);
128 129 130 131 132 133
            items [index_] = items.back ();
            items.pop_back ();
        }

        inline void swap (size_type index1_, size_type index2_)
        {
134 135 136 137
            if (items [index1_])
                ((item_t*) items [index1_])->set_array_index ((int) index2_);
            if (items [index2_])
                ((item_t*) items [index2_])->set_array_index ((int) index1_);
138 139 140 141 142 143 144 145 146 147
            std::swap (items [index1_], items [index2_]);
        }

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

        inline size_type index (T *item_)
        {
148
            return (size_type) ((item_t*) item_)->get_array_index ();
149 150 151 152 153 154 155
        }

    private:

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

156
        array_t (const array_t&);
157
        const array_t &operator = (const array_t&);
158 159 160 161 162
    };

}

#endif
163