00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef STXXL_ALGO_ADAPTOR_HEADER
00015 #define STXXL_ALGO_ADAPTOR_HEADER
00016
00017 #include <stxxl/bits/mng/bid.h>
00018 #include <stxxl/bits/mng/adaptor.h>
00019
00020
00021 __STXXL_BEGIN_NAMESPACE
00022
00023 template <unsigned _blk_sz, typename _run_type, class __pos_type = int_type>
00024 struct RunsToBIDArrayAdaptor : public TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>
00025 {
00026 typedef RunsToBIDArrayAdaptor<_blk_sz, _run_type, __pos_type> _Self;
00027 typedef BID<_blk_sz> data_type;
00028
00029 enum { block_size = _blk_sz };
00030
00031 unsigned_type dim_size;
00032
00033 typedef TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type> _Parent;
00034 using _Parent::array;
00035 using _Parent::pos;
00036
00037 RunsToBIDArrayAdaptor(_run_type ** a, __pos_type p, unsigned_type d)
00038 : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a, p), dim_size(d)
00039 { }
00040 RunsToBIDArrayAdaptor(const _Self & a)
00041 : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a), dim_size(a.dim_size)
00042 { }
00043
00044 const _Self & operator = (const _Self & a)
00045 {
00046 array = a.array;
00047 pos = a.pos;
00048 dim_size = a.dim_size;
00049 return *this;
00050 }
00051
00052 data_type & operator * ()
00053 {
00054 CHECK_RUN_BOUNDS(pos);
00055 return (BID<_blk_sz>&)((*(array[(pos) % dim_size]))[(pos) / dim_size].bid);
00056 }
00057
00058 const data_type * operator -> () const
00059 {
00060 CHECK_RUN_BOUNDS(pos);
00061 return &((*(array[(pos) % dim_size])[(pos) / dim_size].bid));
00062 }
00063
00064
00065 data_type & operator [] (__pos_type n) const
00066 {
00067 n += pos;
00068 CHECK_RUN_BOUNDS(n);
00069 return (BID<_blk_sz>&)((*(array[(n) % dim_size]))[(n) / dim_size].bid);
00070 }
00071 };
00072
00073 BLOCK_ADAPTOR_OPERATORS(RunsToBIDArrayAdaptor)
00074
00075 template <unsigned _blk_sz, typename _run_type, class __pos_type = int_type>
00076 struct RunsToBIDArrayAdaptor2
00077 : public TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>
00078 {
00079 typedef RunsToBIDArrayAdaptor2<_blk_sz, _run_type, __pos_type> _Self;
00080 typedef BID<_blk_sz> data_type;
00081
00082 typedef TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type> ParentClass_;
00083
00084 using ParentClass_::pos;
00085 using ParentClass_::array;
00086
00087 enum
00088 { block_size = _blk_sz };
00089
00090 __pos_type w, h, K;
00091
00092 RunsToBIDArrayAdaptor2(_run_type ** a, __pos_type p, int_type _w, int_type _h)
00093 : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a, p),
00094 w(_w), h(_h), K(_w * _h)
00095 { }
00096
00097 RunsToBIDArrayAdaptor2(const _Self & a)
00098 : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a),
00099 w(a.w), h(a.h), K(a.K)
00100 { }
00101
00102 const _Self & operator = (const _Self & a)
00103 {
00104 array = a.array;
00105 pos = a.pos;
00106 w = a.w;
00107 h = a.h;
00108 K = a.K;
00109 return *this;
00110 }
00111
00112 data_type & operator * ()
00113 {
00114 register __pos_type i = pos - K;
00115 if (i < 0)
00116 return (BID<_blk_sz>&)((*(array[(pos) % w]))[(pos) / w].bid);
00117
00118 register __pos_type _w = w;
00119 _w--;
00120 return (BID<_blk_sz>&)((*(array[(i) % _w]))[h + (i / _w)].bid);
00121 }
00122
00123 const data_type * operator -> () const
00124 {
00125 register __pos_type i = pos - K;
00126 if (i < 0)
00127 return &((*(array[(pos) % w])[(pos) / w].bid));
00128
00129
00130 register __pos_type _w = w;
00131 _w--;
00132 return &((*(array[(i) % _w])[h + (i / _w)].bid));
00133 }
00134
00135
00136 data_type & operator [] (__pos_type n) const
00137 {
00138 n += pos;
00139 register __pos_type i = n - K;
00140 if (i < 0)
00141 return (BID<_blk_sz>&)((*(array[(n) % w]))[(n) / w].bid);
00142
00143
00144 register __pos_type _w = w;
00145 _w--;
00146 return (BID<_blk_sz>&)((*(array[(i) % _w]))[h + (i / _w)].bid);
00147 }
00148 };
00149
00150 BLOCK_ADAPTOR_OPERATORS(RunsToBIDArrayAdaptor2)
00151
00152
00153 template <typename trigger_iterator_type>
00154 struct trigger_entry_iterator
00155 {
00156 typedef trigger_entry_iterator<trigger_iterator_type> _Self;
00157 typedef typename std::iterator_traits<trigger_iterator_type>::value_type::bid_type bid_type;
00158
00159
00160 typedef bid_type value_type;
00161 typedef std::random_access_iterator_tag iterator_category;
00162 typedef int_type difference_type;
00163 typedef value_type * pointer;
00164 typedef value_type & reference;
00165
00166 trigger_iterator_type value;
00167
00168 trigger_entry_iterator(const _Self & a) : value(a.value) { }
00169 trigger_entry_iterator(trigger_iterator_type v) : value(v) { }
00170
00171 bid_type & operator * ()
00172 {
00173 return value->bid;
00174 }
00175 bid_type * operator -> () const
00176 {
00177 return &(value->bid);
00178 }
00179 const bid_type & operator [] (int_type n) const
00180 {
00181 return (value + n)->bid;
00182 }
00183 bid_type & operator [] (int_type n)
00184 {
00185 return (value + n)->bid;
00186 }
00187
00188 _Self & operator ++ ()
00189 {
00190 value++;
00191 return *this;
00192 }
00193 _Self operator ++ (int)
00194 {
00195 _Self __tmp = *this;
00196 value++;
00197 return __tmp;
00198 }
00199 _Self & operator -- ()
00200 {
00201 value--;
00202 return *this;
00203 }
00204 _Self operator -- (int)
00205 {
00206 _Self __tmp = *this;
00207 value--;
00208 return __tmp;
00209 }
00210 bool operator == (const _Self & a) const
00211 {
00212 return value == a.value;
00213 }
00214 bool operator != (const _Self & a) const
00215 {
00216 return value != a.value;
00217 }
00218 _Self operator += (int_type n)
00219 {
00220 value += n;
00221 return *this;
00222 }
00223 _Self operator -= (int_type n)
00224 {
00225 value -= n;
00226 return *this;
00227 }
00228 int_type operator - (const _Self & a) const
00229 {
00230 return value - a.value;
00231 }
00232 int_type operator + (const _Self & a) const
00233 {
00234 return value + a.value;
00235 }
00236 };
00237
00238 template <typename Iterator>
00239 inline
00240 trigger_entry_iterator<Iterator>
00241 make_bid_iterator(Iterator iter)
00242 {
00243 return trigger_entry_iterator<Iterator>(iter);
00244 }
00245
00246 __STXXL_END_NAMESPACE
00247
00248 #endif // !STXXL_ALGO_ADAPTOR_HEADER