Stxxl 1.2.1

adaptor.h

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