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