From 210f77d2c32f14d2e99577fd3c9842bb19d47e50 Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Mon, 19 Sep 2016 21:14:58 +0000 Subject: Moved most modules into lib --- lib/omega/include/basic/Section.h | 138 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 lib/omega/include/basic/Section.h (limited to 'lib/omega/include/basic/Section.h') diff --git a/lib/omega/include/basic/Section.h b/lib/omega/include/basic/Section.h new file mode 100644 index 0000000..7a4d241 --- /dev/null +++ b/lib/omega/include/basic/Section.h @@ -0,0 +1,138 @@ +#if ! defined _Section_h +#define _Section_h 1 +/* + Section of an existing collection viewed as a collection + */ + +#include +#include + +namespace omega { + +template class Section_Iterator; + +template class Section : public Sequence { +public: + Section(Sequence *, int start, int length); + + Iterator *new_iterator(); + + const T &operator[](int) const; + T &operator[](int); + + int index(const T &) const; + int size() const; + + friend class Section_Iterator; + +private: + Sequence *it; + int _start, _length; +}; + +template class Section_Iterator : public Iterator { +public: + Section_Iterator(Section &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 *new_copy() const; + +private: + Section_Iterator(const Section_Iterator &si); + Iterator *it; + int remaining; +}; + +} // namespace + +#define instantiate_Section(T) template class Section; \ + template class Section_Iterator; \ + instantiate_Sequence(T) +#define instantiate_Section_Iterator(T) instantiate_Section(T) + + +namespace omega { + +template Section::Section(Sequence *s, int start, int length) + { + assert(s->size() >= start-1 + length); + it = s; + _start = start; + _length = length; + } + +template Iterator *Section::new_iterator() + { + return new Section_Iterator(*this); + } + +template const T &Section::operator[](int i) const + { + assert(1 <= i && i <= size()); + return (*it)[i+(_start-1)]; + } + +template T &Section::operator[](int i) + { + assert(1 <= i && i <= size()); + return (*it)[i+(_start-1)]; + } + +template int Section::index(const T &var) const + { + int i; + for (i=1; i<=size(); i++) + if ((*this)[i] == var) + return i; + return 0; + } + +template int Section::size() const + { + return _length; + } + + +template Section_Iterator::Section_Iterator(Section &sec) + { + it = sec.it->new_iterator(); + for (int i = 1; i < sec._start; i++) + (*it)++; + remaining = sec.size(); + } + + +template Section_Iterator::Section_Iterator(const Section_Iterator &si) : it(si.it), remaining(si.remaining) {} + + +template void Section_Iterator::operator++() + { this->operator++(0); } + +template void Section_Iterator::operator++(int) + { + if (remaining > 0) + { + (*it)++; + remaining--; + } + } + +template bool Section_Iterator::live() const + { + return (remaining > 0); + } + +template Iterator *Section_Iterator::new_copy() const + { + return new Section_Iterator(*this); + } + +} // namespace +#endif -- cgit v1.2.3-70-g09d2