![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2009-2011 250bpm s.r.o. 00003 Copyright (c) 2007-2009 iMatix Corporation 00004 Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 00005 00006 This file is part of 0MQ. 00007 00008 0MQ is free software; you can redistribute it and/or modify it under 00009 the terms of the GNU Lesser General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 0MQ is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #ifndef __ZMQ_ARRAY_INCLUDED__ 00023 #define __ZMQ_ARRAY_INCLUDED__ 00024 00025 #include <vector> 00026 #include <algorithm> 00027 00028 namespace zmq 00029 { 00030 00031 // Base class for objects stored in the array. If you want to store 00032 // same object in mutliple arrays, each of those arrays has to have 00033 // different ID. The item itself has to be derived from instantiations of 00034 // array_item_t template for all relevant IDs. 00035 00036 template <int ID = 0> class array_item_t 00037 { 00038 public: 00039 00040 inline array_item_t () : 00041 array_index (-1) 00042 { 00043 } 00044 00045 // The destructor doesn't have to be virtual. It is mad virtual 00046 // just to keep ICC and code checking tools from complaining. 00047 inline virtual ~array_item_t () 00048 { 00049 } 00050 00051 inline void set_array_index (int index_) 00052 { 00053 array_index = index_; 00054 } 00055 00056 inline int get_array_index () 00057 { 00058 return array_index; 00059 } 00060 00061 private: 00062 00063 int array_index; 00064 00065 array_item_t (const array_item_t&); 00066 const array_item_t &operator = (const array_item_t&); 00067 }; 00068 00069 // Fast array implementation with O(1) access to item, insertion and 00070 // removal. Array stores pointers rather than objects. The objects have 00071 // to be derived from array_item_t<ID> class. 00072 00073 template <typename T, int ID = 0> class array_t 00074 { 00075 private: 00076 00077 typedef array_item_t <ID> item_t; 00078 00079 public: 00080 00081 typedef typename std::vector <T*>::size_type size_type; 00082 00083 inline array_t () 00084 { 00085 } 00086 00087 inline ~array_t () 00088 { 00089 } 00090 00091 inline size_type size () 00092 { 00093 return items.size (); 00094 } 00095 00096 inline bool empty () 00097 { 00098 return items.empty (); 00099 } 00100 00101 inline T *&operator [] (size_type index_) 00102 { 00103 return items [index_]; 00104 } 00105 00106 inline void push_back (T *item_) 00107 { 00108 if (item_) 00109 ((item_t*) item_)->set_array_index ((int) items.size ()); 00110 items.push_back (item_); 00111 } 00112 00113 inline void erase (T *item_) { 00114 erase (((item_t*) item_)->get_array_index ()); 00115 } 00116 00117 inline void erase (size_type index_) { 00118 if (items.back ()) 00119 ((item_t*) items.back ())->set_array_index ((int) index_); 00120 items [index_] = items.back (); 00121 items.pop_back (); 00122 } 00123 00124 inline void swap (size_type index1_, size_type index2_) 00125 { 00126 if (items [index1_]) 00127 ((item_t*) items [index1_])->set_array_index ((int) index2_); 00128 if (items [index2_]) 00129 ((item_t*) items [index2_])->set_array_index ((int) index1_); 00130 std::swap (items [index1_], items [index2_]); 00131 } 00132 00133 inline void clear () 00134 { 00135 items.clear (); 00136 } 00137 00138 inline size_type index (T *item_) 00139 { 00140 return (size_type) ((item_t*) item_)->get_array_index (); 00141 } 00142 00143 private: 00144 00145 typedef std::vector <T*> items_t; 00146 items_t items; 00147 00148 array_t (const array_t&); 00149 const array_t &operator = (const array_t&); 00150 }; 00151 00152 } 00153 00154 #endif 00155