• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

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/bid.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>
00153 struct trigger_entry_iterator
00154 {
00155     typedef trigger_entry_iterator<trigger_iterator_type> _Self;
00156     typedef typename std::iterator_traits<trigger_iterator_type>::value_type::bid_type bid_type;
00157 
00158     // STL typedefs
00159     typedef bid_type value_type;
00160     typedef std::random_access_iterator_tag iterator_category;
00161     typedef int_type difference_type;
00162     typedef value_type * pointer;
00163     typedef value_type & reference;
00164 
00165     trigger_iterator_type value;
00166 
00167     trigger_entry_iterator(const _Self & a) : value(a.value) { }
00168     trigger_entry_iterator(trigger_iterator_type v) : value(v) { }
00169 
00170     bid_type & operator * ()
00171     {
00172         return value->bid;
00173     }
00174     bid_type * operator -> () const
00175     {
00176         return &(value->bid);
00177     }
00178     const bid_type & operator [] (int_type n) const
00179     {
00180         return (value + n)->bid;
00181     }
00182     bid_type & operator [] (int_type n)
00183     {
00184         return (value + n)->bid;
00185     }
00186 
00187     _Self & operator ++ ()
00188     {
00189         value++;
00190         return *this;
00191     }
00192     _Self operator ++ (int)
00193     {
00194         _Self __tmp = *this;
00195         value++;
00196         return __tmp;
00197     }
00198     _Self & operator -- ()
00199     {
00200         value--;
00201         return *this;
00202     }
00203     _Self operator -- (int)
00204     {
00205         _Self __tmp = *this;
00206         value--;
00207         return __tmp;
00208     }
00209     bool operator == (const _Self & a) const
00210     {
00211         return value == a.value;
00212     }
00213     bool operator != (const _Self & a) const
00214     {
00215         return value != a.value;
00216     }
00217     _Self operator += (int_type n)
00218     {
00219         value += n;
00220         return *this;
00221     }
00222     _Self operator -= (int_type n)
00223     {
00224         value -= n;
00225         return *this;
00226     }
00227     int_type operator - (const _Self & a) const
00228     {
00229         return value - a.value;
00230     }
00231     int_type operator + (const _Self & a) const
00232     {
00233         return value + a.value;
00234     }
00235 };
00236 
00237 template <typename Iterator>
00238 inline
00239 trigger_entry_iterator<Iterator>
00240 make_bid_iterator(Iterator iter)
00241 {
00242     return trigger_entry_iterator<Iterator>(iter);
00243 }
00244 
00245 __STXXL_END_NAMESPACE
00246 
00247 #endif // !STXXL_ALGO_ADAPTOR_HEADER

Generated on Sun Oct 17 2010 06:13:42 for Stxxl by  doxygen 1.7.1