TRF Language Model
wb-iter.h
Go to the documentation of this file.
1 // You may obtain a copy of the License at
2 //
3 // http://www.apache.org/licenses/LICENSE-2.0
4 //
5 // Unless required by applicable law or agreed to in writing, software
6 // distributed under the License is distributed on an "AS IS" BASIS,
7 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8 // See the License for the specific language governing permissions and
9 // limitations under the License.
10 //
11 // Copyright 2014-2015 Tsinghua University
12 // Author: wb.th08@gmail.com (Bin Wang), ozj@tsinghua.edu.cn (Zhijian Ou)
13 //
14 // All h, cpp, cc, and script files (e.g. bat, sh, pl, py) should include the above
15 // license declaration. Different coding language may use different comment styles.
16 
17 
18 #ifndef _WB_ITER_H_
19 #define _WB_ITER_H_
20 #include "wb-vector.h"
21 
22 namespace wb
23 {
28  namespace iter
29  {
30  template <typename T>
31  class Obj
32  {
33  public:
34  virtual void Reset() = 0;
35  virtual bool Next(T &t) = 0;
36  };
37 
38  template <typename T>
39  class Line : public Obj<T>
40  {
41  public:
42  T beg;
43  T end;
44  T step;
45  T cur;
46  public:
47  Line(T b, T e, T s) :beg(b), end(e), step(s), cur(b) {}
48  virtual void Reset() { cur = beg; }
49  virtual bool Next(T &t)
50  {
51  if ((cur-end>0) == (step>0))
52  return false;
53  t = cur;
54  cur += step;
55  return true;
56  }
57  };
58  template <typename T>
59  class Ary : public Obj<T>
60  {
61  public:
62  T *p;
63  int len;
64  int cur;
65  public:
66  Ary(T *p_p, int p_len) :p(p_p), len(p_len), cur(0) {}
67  virtual void Reset() { cur = 0; }
68  virtual bool Next(T &t)
69  {
70  if (cur >= len)
71  return false;
72  t = p[cur];
73  cur++;
74  return true;
75  }
76  };
77  }
78 
79 
80  template <typename T>
81  class vIter
82  {
83  public:
84  T *m_pBuf;
85  int m_nDim;
87  int m_nCur;
88  public:
89  vIter(T *pbuf, int dim) : m_pBuf(pbuf), m_nDim(dim), m_nCur(0) {}
91  void Reset()
92  {
93  for (int i = 0; i < m_nDim; i++) {
94  m_aObj[i]->Reset();
95  }
96  m_nCur = 0;
97  }
98  bool Next()
99  {
100  m_nCur++;
101  if (m_nCur == 1) {
102  for (int i = 0; i < m_nDim; i++) {
103  m_aObj[i]->Next(m_pBuf[i]);
104  }
105  }
106  else {
107  int i = 0;
108  for (i = 0; i < m_nDim; i++) {
109  if (m_aObj[i]->Next(m_pBuf[i]))
110  break;
111  m_aObj[i]->Reset();
112  m_aObj[i]->Next(m_pBuf[i]);
113  }
114  if (i >= m_nDim) {
115  return false;
116  }
117  }
118 
119  return true;
120  }
121  void AddLine(T beg, T end, T step = 1) {
122  m_aObj.Add() = new iter::Line<T>(beg, end, step);
123  }
124  void AddAllLine(T beg, T end, T step = 1) {
125  for (int i = 0; i < m_nDim; i++)
126  m_aObj[i] = new iter::Line<T>(beg, end, step);
127  Reset();
128  }
129  void AddAry(T *p, int n) {
130  m_aObj.Add() = new iter::Ary<T>(p, n);
131  }
132  void AddAllAry(T *p, int n) {
133  for (int i = 0; i < m_nDim; i++)
134  m_aObj[i] = new iter::Ary<T>(p, n);
135  Reset();
136  }
137  };
139 }
140 
141 #endif
void AddLine(T beg, T end, T step=1)
Definition: wb-iter.h:121
T step
step
Definition: wb-iter.h:44
Array< iter::Obj< T > * > m_aObj
the iter of each dimension
Definition: wb-iter.h:86
virtual bool Next(T &t)
Definition: wb-iter.h:68
void AddAry(T *p, int n)
Definition: wb-iter.h:129
int m_nDim
dimension
Definition: wb-iter.h:85
virtual void Reset()=0
virtual bool Next(T &t)
Definition: wb-iter.h:49
bool Next()
Definition: wb-iter.h:98
T cur
current value
Definition: wb-iter.h:45
Line(T b, T e, T s)
Definition: wb-iter.h:47
T * m_pBuf
the buffer
Definition: wb-iter.h:84
virtual void Reset()
Definition: wb-iter.h:67
void AddAllLine(T beg, T end, T step=1)
Definition: wb-iter.h:124
vIter(T *pbuf, int dim)
Definition: wb-iter.h:89
~vIter()
Definition: wb-iter.h:90
int m_nCur
current number
Definition: wb-iter.h:87
void Add(T t)
Add a value to the tail of array.
Definition: wb-vector.h:242
T beg
begin value
Definition: wb-iter.h:42
virtual bool Next(T &t)=0
T * p
the array
Definition: wb-iter.h:62
T end
end value
Definition: wb-iter.h:43
void AddAllAry(T *p, int n)
Definition: wb-iter.h:132
void Reset()
Definition: wb-iter.h:91
virtual void Reset()
Definition: wb-iter.h:48
Ary(T *p_p, int p_len)
Definition: wb-iter.h:66
int len
the length of the array
Definition: wb-iter.h:63
int cur
cur position
Definition: wb-iter.h:64
#define SAFE_DEL_POINTER_ARRAY(a)
Definition: wb-vector.h:52
define all the code written by Bin Wang.
Definition: wb-file.cpp:21
Dynamic array.
Definition: wb-vector.h:205
Defination of simple dynamic array/stack/queue and so on.