Stxxl  1.3.2
algo/adaptor.h
1 /***************************************************************************
2  * include/stxxl/bits/algo/adaptor.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_ALGO_ADAPTOR_HEADER
15 #define STXXL_ALGO_ADAPTOR_HEADER
16 
17 #include <stxxl/bits/mng/bid.h>
18 #include <stxxl/bits/mng/adaptor.h>
19 
20 
21 __STXXL_BEGIN_NAMESPACE
22 
23 template <unsigned _blk_sz, typename _run_type, class __pos_type = int_type>
24 struct RunsToBIDArrayAdaptor : public TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>
25 {
26  typedef RunsToBIDArrayAdaptor<_blk_sz, _run_type, __pos_type> _Self;
27  typedef BID<_blk_sz> data_type;
28 
29  enum { block_size = _blk_sz };
30 
31  unsigned_type dim_size;
32 
33  typedef TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type> _Parent;
34  using _Parent::array;
35  using _Parent::pos;
36 
37  RunsToBIDArrayAdaptor(_run_type ** a, __pos_type p, unsigned_type d)
38  : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a, p), dim_size(d)
39  { }
40  RunsToBIDArrayAdaptor(const _Self & a)
41  : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a), dim_size(a.dim_size)
42  { }
43 
44  const _Self & operator = (const _Self & a)
45  {
46  array = a.array;
47  pos = a.pos;
48  dim_size = a.dim_size;
49  return *this;
50  }
51 
52  data_type & operator * ()
53  {
54  CHECK_RUN_BOUNDS(pos);
55  return (BID<_blk_sz>&)((*(array[(pos) % dim_size]))[(pos) / dim_size].bid);
56  }
57 
58  const data_type * operator -> () const
59  {
60  CHECK_RUN_BOUNDS(pos);
61  return &((*(array[(pos) % dim_size])[(pos) / dim_size].bid));
62  }
63 
64 
65  data_type & operator [] (__pos_type n) const
66  {
67  n += pos;
68  CHECK_RUN_BOUNDS(n);
69  return (BID<_blk_sz>&)((*(array[(n) % dim_size]))[(n) / dim_size].bid);
70  }
71 };
72 
73 BLOCK_ADAPTOR_OPERATORS(RunsToBIDArrayAdaptor)
74 
75 template <unsigned _blk_sz, typename _run_type, class __pos_type = int_type>
76 struct RunsToBIDArrayAdaptor2
77  : public TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>
78 {
79  typedef RunsToBIDArrayAdaptor2<_blk_sz, _run_type, __pos_type> _Self;
80  typedef BID<_blk_sz> data_type;
81 
82  typedef TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type> ParentClass_;
83 
84  using ParentClass_::pos;
85  using ParentClass_::array;
86 
87  enum
88  { block_size = _blk_sz };
89 
90  __pos_type w, h, K;
91 
92  RunsToBIDArrayAdaptor2(_run_type ** a, __pos_type p, int_type _w, int_type _h)
93  : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a, p),
94  w(_w), h(_h), K(_w * _h)
95  { }
96 
97  RunsToBIDArrayAdaptor2(const _Self & a)
98  : TwoToOneDimArrayAdaptorBase<_run_type *, BID<_blk_sz>, __pos_type>(a),
99  w(a.w), h(a.h), K(a.K)
100  { }
101 
102  const _Self & operator = (const _Self & a)
103  {
104  array = a.array;
105  pos = a.pos;
106  w = a.w;
107  h = a.h;
108  K = a.K;
109  return *this;
110  }
111 
112  data_type & operator * ()
113  {
114  register __pos_type i = pos - K;
115  if (i < 0)
116  return (BID<_blk_sz>&)((*(array[(pos) % w]))[(pos) / w].bid);
117 
118  register __pos_type _w = w;
119  _w--;
120  return (BID<_blk_sz>&)((*(array[(i) % _w]))[h + (i / _w)].bid);
121  }
122 
123  const data_type * operator -> () const
124  {
125  register __pos_type i = pos - K;
126  if (i < 0)
127  return &((*(array[(pos) % w])[(pos) / w].bid));
128 
129 
130  register __pos_type _w = w;
131  _w--;
132  return &((*(array[(i) % _w])[h + (i / _w)].bid));
133  }
134 
135 
136  data_type & operator [] (__pos_type n) const
137  {
138  n += pos;
139  register __pos_type i = n - K;
140  if (i < 0)
141  return (BID<_blk_sz>&)((*(array[(n) % w]))[(n) / w].bid);
142 
143 
144  register __pos_type _w = w;
145  _w--;
146  return (BID<_blk_sz>&)((*(array[(i) % _w]))[h + (i / _w)].bid);
147  }
148 };
149 
150 BLOCK_ADAPTOR_OPERATORS(RunsToBIDArrayAdaptor2)
151 
152 
153 template <typename trigger_iterator_type>
154 struct trigger_entry_iterator
155 {
156  typedef trigger_entry_iterator<trigger_iterator_type> _Self;
157  typedef typename std::iterator_traits<trigger_iterator_type>::value_type::bid_type bid_type;
158 
159  // STL typedefs
160  typedef bid_type value_type;
161  typedef std::random_access_iterator_tag iterator_category;
162  typedef int_type difference_type;
163  typedef value_type * pointer;
164  typedef value_type & reference;
165 
166  trigger_iterator_type value;
167 
168  trigger_entry_iterator(const _Self & a) : value(a.value) { }
169  trigger_entry_iterator(trigger_iterator_type v) : value(v) { }
170 
171  bid_type & operator * ()
172  {
173  return value->bid;
174  }
175  bid_type * operator -> () const
176  {
177  return &(value->bid);
178  }
179  const bid_type & operator [] (int_type n) const
180  {
181  return (value + n)->bid;
182  }
183  bid_type & operator [] (int_type n)
184  {
185  return (value + n)->bid;
186  }
187 
188  _Self & operator ++ ()
189  {
190  value++;
191  return *this;
192  }
193  _Self operator ++ (int)
194  {
195  _Self __tmp = *this;
196  value++;
197  return __tmp;
198  }
199  _Self & operator -- ()
200  {
201  value--;
202  return *this;
203  }
204  _Self operator -- (int)
205  {
206  _Self __tmp = *this;
207  value--;
208  return __tmp;
209  }
210  bool operator == (const _Self & a) const
211  {
212  return value == a.value;
213  }
214  bool operator != (const _Self & a) const
215  {
216  return value != a.value;
217  }
218  _Self operator += (int_type n)
219  {
220  value += n;
221  return *this;
222  }
223  _Self operator -= (int_type n)
224  {
225  value -= n;
226  return *this;
227  }
228  int_type operator - (const _Self & a) const
229  {
230  return value - a.value;
231  }
232  int_type operator + (const _Self & a) const
233  {
234  return value + a.value;
235  }
236 };
237 
238 template <typename Iterator>
239 inline
240 trigger_entry_iterator<Iterator>
241 make_bid_iterator(Iterator iter)
242 {
243  return trigger_entry_iterator<Iterator>(iter);
244 }
245 
246 __STXXL_END_NAMESPACE
247 
248 #endif // !STXXL_ALGO_ADAPTOR_HEADER
Block identifier class.
Definition: bid.h:40