blob: 7a4d2418d408c128afbbdb0118de489bf8826a16 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#if ! defined _Section_h
#define _Section_h 1
/*
Section of an existing collection viewed as a collection
*/
#include <basic/Collection.h>
#include <assert.h>
namespace omega {
template<class T> class Section_Iterator;
template <class T> class Section : public Sequence<T> {
public:
Section(Sequence<T> *, int start, int length);
Iterator<T> *new_iterator();
const T &operator[](int) const;
T &operator[](int);
int index(const T &) const;
int size() const;
friend class Section_Iterator<T>;
private:
Sequence<T> *it;
int _start, _length;
};
template <class T> class Section_Iterator : public Iterator<T> {
public:
Section_Iterator(Section<T> &sec);
virtual ~Section_Iterator() { delete it; }
const T & operator*() const { return *(*it); }
T & operator*() { return *(*it); }
void operator++(int);
void operator++();
bool live() const;
Iterator<T> *new_copy() const;
private:
Section_Iterator(const Section_Iterator<T> &si);
Iterator<T> *it;
int remaining;
};
} // namespace
#define instantiate_Section(T) template class Section<T>; \
template class Section_Iterator<T>; \
instantiate_Sequence(T)
#define instantiate_Section_Iterator(T) instantiate_Section(T)
namespace omega {
template <class T> Section<T>::Section(Sequence<T> *s, int start, int length)
{
assert(s->size() >= start-1 + length);
it = s;
_start = start;
_length = length;
}
template <class T> Iterator<T> *Section<T>::new_iterator()
{
return new Section_Iterator<T>(*this);
}
template <class T> const T &Section<T>::operator[](int i) const
{
assert(1 <= i && i <= size());
return (*it)[i+(_start-1)];
}
template <class T> T &Section<T>::operator[](int i)
{
assert(1 <= i && i <= size());
return (*it)[i+(_start-1)];
}
template <class T> int Section<T>::index(const T &var) const
{
int i;
for (i=1; i<=size(); i++)
if ((*this)[i] == var)
return i;
return 0;
}
template <class T> int Section<T>::size() const
{
return _length;
}
template <class T> Section_Iterator<T>::Section_Iterator(Section<T> &sec)
{
it = sec.it->new_iterator();
for (int i = 1; i < sec._start; i++)
(*it)++;
remaining = sec.size();
}
template <class T> Section_Iterator<T>::Section_Iterator(const Section_Iterator<T> &si) : it(si.it), remaining(si.remaining) {}
template <class T> void Section_Iterator<T>::operator++()
{ this->operator++(0); }
template <class T> void Section_Iterator<T>::operator++(int)
{
if (remaining > 0)
{
(*it)++;
remaining--;
}
}
template <class T> bool Section_Iterator<T>::live() const
{
return (remaining > 0);
}
template <class T> Iterator<T> *Section_Iterator<T>::new_copy() const
{
return new Section_Iterator<T>(*this);
}
} // namespace
#endif
|