From 17f44d57164b123be802b3474f674d2e0df4d216 Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Mon, 19 Sep 2016 11:30:09 -0600 Subject: Template definition back in header files --- .gitignore | 3 + chill/src/ir_rose.cc | 1 + omegalib/omega/include/basic/Bag.h | 328 +++++++++++++++++++++++++++- omegalib/omega/include/basic/DynamicArray.h | 223 ++++++++++++++++++- omegalib/omega/include/basic/List.h | 151 ++++++++++++- omegalib/omega/include/basic/Map.h | 67 +++++- omegalib/omega/include/basic/Section.h | 83 ++++++- omegalib/omega/include/basic/SimpleList.h | 109 ++++++++- omegalib/omega/include/basic/Tuple.c | 2 +- omegalib/omega/include/basic/Tuple.h | 256 +++++++++++++++++++++- 10 files changed, 1193 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index ba18327..f0d8fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ # Cmake build directory /build +# CLion config directory +/.idea + # Vim swap file *.swap diff --git a/chill/src/ir_rose.cc b/chill/src/ir_rose.cc index 36e2db9..27930e6 100644 --- a/chill/src/ir_rose.cc +++ b/chill/src/ir_rose.cc @@ -844,6 +844,7 @@ IR_roseCode::IR_roseCode(const char *filename, const char* proc_name) : symtab2_ = func->get_definition()->get_symbol_table(); symtab3_ = func->get_definition()->get_body()->get_symbol_table(); // Manu:: added is_fortran_ parameter + // TODO Substitute it with a better builder ocg_ = new omega::CG_roseBuilder(is_fortran_, root, firstScope, func->get_definition()->get_symbol_table(), func->get_definition()->get_body()->get_symbol_table(), diff --git a/omegalib/omega/include/basic/Bag.h b/omegalib/omega/include/basic/Bag.h index 3bd7143..a3d07a0 100644 --- a/omegalib/omega/include/basic/Bag.h +++ b/omegalib/omega/include/basic/Bag.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace omega { @@ -71,10 +72,6 @@ public: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_Bag(T) template class Bag; \ instantiate_List_Element(T); #define instantiate_Ordered_Bag(T) template class Ordered_Bag; \ @@ -82,4 +79,327 @@ public: #define instantiate_Set(T) template class Set; \ instantiate_Ordered_Bag(T) + +namespace omega { + +template Bag::Bag() { + contents = new List_Element ; + contents->tail = 0; + } +template Bag::~Bag() { + delete contents; + } + +template Ordered_Bag::Ordered_Bag() {} + +template Set::Set() {} + +template Bag::Bag(const Bag &L) { + contents = new List_Element(*L.contents); + } + +template Bag & Bag::operator=(const Bag &L) { + if (this != &L) { + delete contents; + contents = new List_Element(*L.contents); + } + return *this; + } + + + +template Set::Set(T e) { + assert(this->contents); + this->contents->tail = new List_Element(e, 0); + } + + +/**************************************************************** + * * + * Misc. simple Collection operations * + * * + ****************************************************************/ + +template bool Bag::empty() const { + return contents->tail == 0; + } + +template Iterator *Bag::new_iterator() + { + return new List_Element_Iterator(contents->tail); + } + + +template void Bag::clear() { + if (contents->tail) delete contents->tail; + contents->tail = 0; + } + +template int Bag::size() const { + int i = 0; + List_Element * p = contents->tail; + while (p) { + p = p->tail; + i++; + }; + return i; + } + + +/**************************************************************** + * * + * Collection/Element operations (e.g. insert, contains) * + * * + ****************************************************************/ + +template void Bag::remove(T e) { + List_Element * p = contents; + while (p->tail && p->tail->head != e) p = p->tail; + if (p->tail && p->tail->head == e) { + List_Element * q = p->tail; + p->tail = q->tail; + q->tail = 0; + delete q; + } + } + +template T Bag::extract() { + List_Element * p = contents->tail; + T e = p->head; + contents->tail = p->tail; + p->tail = 0; + delete p; + return e; + } + + +template void Bag::insert(T e) { + List_Element * q = new List_Element(e,contents->tail); + contents->tail = q; + } + +template void Ordered_Bag::insert(T e) { + List_Element * p = this->contents; + while (p->tail && p->tail->head < e) p = p->tail; + if (!p->tail || p->tail->head != e) { + List_Element * q = new List_Element(e,p->tail); + p->tail = q; + } + } + + +template bool Bag::contains(T e) const { + List_Element * p = contents; + while (p->tail && p->tail->head != e) p = p->tail; + return (p->tail && p->tail->head == e); + } + +template bool Ordered_Bag::contains(T e) const { + List_Element * p = this->contents; + while (p->tail && p->tail->head < e) p = p->tail; + return (p->tail && p->tail->head == e); + } + + +template bool Set::contains (const Set& b) const { + List_Element * p = this->contents; + List_Element * q = b.contents; + do { + /* consume matched elements in p and q */ + p = p->tail; + q = q->tail; + if (!q) return 1; /* no more elements to match */ + if (!p) return 0; /* nothing left in p to match with */ + if (q->head < p->head) { + /* nothing smaller than + p->head left in p, so q->head + can't be matched */ + return 0; + }; + while (p && p->head < q->head) { + /* toss away some elements from p */ + p = p->tail; + } + if (!p || q->head < p->head) return 0; + } while (q); + + return 1; + } + + + +/**************************************************************** + * * + * Collection/Collection operations (e.g. |=) * + * * + ****************************************************************/ + +template void Bag::operator |= (const Bag & b) { + assert(this != &b); + List_Element * q = b.contents->tail; + + while (q) { + List_Element * r = new List_Element(q->head,contents->tail); + contents->tail = r; + q = q->tail; + } + } + +template void Ordered_Bag::operator |= (const Ordered_Bag & b) { + if (this == &b) return; + List_Element * p = this->contents; + List_Element * q = b.contents->tail; + + while (q) { + while (p->tail && p->tail->head < q->head) p = p->tail; + List_Element * r = new List_Element(q->head,p->tail); + p->tail = r; + q = q->tail; + } + } + +template void Ordered_Bag::operator |= (const Bag & b) { + Ordered_Bag tmp; + for (List_Element *p = b.contents; p; p=p->tail) { + tmp.insert(p->head); + } + *this |= tmp; +} + +template void Set::operator |= (const Set & b) { + if (this == &b) return; + List_Element * p = this->contents; + List_Element * q = b.contents->tail; + + while (q) { + while (p->tail && p->tail->head < q->head) p = p->tail; + if (!p->tail || p->tail->head != q->head) { + List_Element * r = new List_Element(q->head,p->tail); + p->tail = r; + } + q = q->tail; + } + } + +template void Set::operator |= (const Ordered_Bag & b) { + Set tmp; + for (List_Element *p = b.contents; p; p=p->tail) { + tmp.insert(p->head); + } + *this |= tmp; +} + +template void Set::operator |= (const Bag & b) { + Set tmp; + for (List_Element *p = b.contents; p; p=p->tail) { + tmp.insert(p->head); + } + *this |= tmp; +} + + + +// delete items also in b +template void Set::operator -= (const Set & b) { + if (this == &b) { + this->clear(); + return; + } + List_Element * p = this->contents; + List_Element * q = b.contents->tail; + + while (q) { + while (p->tail && p->tail->head < q->head) p = p->tail; + if (p->tail && p->tail->head == q->head) { + List_Element * r = p->tail; + p->tail = r->tail; + r->tail = 0; + delete r; + } + q = q->tail; + } + } + + +// delete items not in b +template void Set::operator &= (const Set & b) + { + if (this == &b) return; + List_Element * p = this->contents; + List_Element * q = b.contents->tail; + + while (q) { + while (p->tail && p->tail->head < q->head) { + List_Element * r = p->tail; + p->tail = r->tail; + r->tail = 0; + delete r; + }; + if (p->tail && p->tail->head == q->head) { + /* allow p->tail->head into the result */ + p = p->tail; + } + /* q->head has matched anything it is going to match */ + q = q->tail; + } + if (p->tail) { + delete p->tail; + p->tail = 0; + }; + + } + + +template bool Set::operator & (const Set& b) const { + List_Element * p = this->contents; + List_Element * q = b.contents; + do { + p = p->tail; + q = q->tail; + while (p && q && p->head != q->head) { + while (p && p->head < q->head) p = p->tail; + while (p && q && q->head < p->head) q = q->tail; + }; + if (p && q && p->head == q->head) return 1; + } while (p && q); + + return 0; + } + + +template bool Ordered_Bag::operator == (const Ordered_Bag& b) const { + List_Element * p = this->contents; + List_Element * q = b.contents; + while (1) { + p = p->tail; + q = q->tail; + if (!p && !q) return 1; + if (!p || !q) return 0; + if (p->head != q->head) return 0; + }; + + } + +template bool Ordered_Bag::operator != (const Ordered_Bag& b) const { + return !(*this == b); + } + +template bool Ordered_Bag::operator < (const Ordered_Bag& b) const { + List_Element * p = this->contents; + List_Element * q = b.contents; + while (1) { + p = p->tail; + q = q->tail; + if (!p && !q) return 0; + if (!p) return 1; + if (!q) return 0; + if (p->head < q->head) return 1; + if (q->head < p->head) return 0; + }; + + return 1; + } + +} // namespace + #endif diff --git a/omegalib/omega/include/basic/DynamicArray.h b/omegalib/omega/include/basic/DynamicArray.h index ac9bae4..08f8b91 100644 --- a/omegalib/omega/include/basic/DynamicArray.h +++ b/omegalib/omega/include/basic/DynamicArray.h @@ -1,6 +1,8 @@ #ifndef Already_Included_DynamicArray #define Already_Included_DynamicArray +#include + namespace omega { template class DynamicArray2; @@ -82,10 +84,6 @@ template class DynamicArray4 : public DynamicArray } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_DynamicArray1(T) template class DynamicArray1; \ template class DynamicArray; @@ -100,4 +98,221 @@ template class DynamicArray4 : public DynamicArray #define instantiate_DynamicArray4(T) template class DynamicArray4; \ template class DynamicArray; \ instantiate_DynamicArray3(T); + +namespace omega { + +template void DynamicArray::do_constr() + { +// #if ! defined SHUT_UP_ABOUT_STATEMENT_WITH_NO_EFFECT_IN_DYNAMIC_ARRAY_CREATION +// assert(d > 0); +// #endif + bounds = 0; + elements = 0; + partial = false; + } + + +template void DynamicArray1::do_construct(int d0) + { + this->bounds = new int[1]; + this->bounds[0] = d0; + this->elements = new T [d0]; + this->partial = false; + } + +template void DynamicArray2::do_construct(int d0, int d1) + { + this->bounds = new int[2]; + this->bounds[0] = d0; + this->bounds[1] = d1; + this->elements = new T [d0 * d1]; + this->partial = false; + } + +template void DynamicArray3::do_construct(int d0,int d1,int d2) + { + this->bounds = new int[3]; + this->bounds[0] = d0; + this->bounds[1] = d1; + this->bounds[2] = d2; + this->elements = new T [d0 * d1 * d2]; + this->partial = false; + } + +template void DynamicArray4::do_construct(int d0,int d1,int d2,int d3) + { + this->bounds = new int[4]; + this->bounds[0] = d0; + this->bounds[1] = d1; + this->bounds[2] = d2; + this->bounds[3] = d3; + this->elements = new T [d0 * d1 * d2 * d3]; + this->partial = false; + } + +template DynamicArray::DynamicArray() + { + do_constr(); + } + +template DynamicArray1::DynamicArray1(const char *) + { + this->do_constr(); + } + +template DynamicArray2::DynamicArray2(const char *,const char *) + { + this->do_constr(); + } + +template DynamicArray3::DynamicArray3(const char *,const char *,const char *) + { + this->do_constr(); + } + +template DynamicArray4::DynamicArray4(const char *,const char *,const char *,const char *) + { + this->do_constr(); + } + +template DynamicArray1::DynamicArray1(int d0) + { + do_construct(d0); + } + +template DynamicArray2::DynamicArray2(int d0, int d1) + { + do_construct(d0, d1); + } + +template DynamicArray3::DynamicArray3(int d0,int d1,int d2) + { + do_construct(d0, d1, d2); + } + +template DynamicArray4::DynamicArray4(int d0,int d1,int d2,int d3) + { + do_construct(d0, d1, d2, d3); + } + + +template void DynamicArray::do_destruct() + { + if (! partial) + { + delete [] bounds; + delete [] elements; + } + } + + +template DynamicArray::~DynamicArray() + { + do_destruct(); + } + + +template void DynamicArray1::resize(int d0) + { + assert(!this->partial); + this->do_destruct(); + if (d0 == 0) + this->do_constr(); + else + do_construct(d0); + } + +template void DynamicArray2::resize(int d0, int d1) + { + assert(!this->partial); + this->do_destruct(); + if (d0 == 0 && d1 == 0) + this->do_constr(); + else + do_construct(d0, d1); + } + +template void DynamicArray3::resize(int d0, int d1, int d2) + { + assert(!this->partial); + this->do_destruct(); + if (d0 == 0 && d1 == 0 && d2 == 0) + this->do_constr(); + else + do_construct(d0, d1, d2); + } + +template void DynamicArray4::resize(int d0, int d1, int d2, int d3) + { + assert(!this->partial); + this->do_destruct(); + if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0) + this->do_constr(); + else + do_construct(d0, d1, d2, d3); + } + + +template T& DynamicArray1::operator[](int d0) + { +#if !defined (NDEBUG) + assert(this->elements != 0 && "Trying to dereference undefined array"); + assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds"); +#endif + + return this->elements[d0]; + } + +template DynamicArray1 DynamicArray2::operator[](int d0) + { +#if !defined (NDEBUG) + assert(this->elements != 0 && "Trying to dereference undefined array"); + assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds"); +#endif + + DynamicArray1 result; + result.bounds = this->bounds+1; + result.elements = this->elements + this->bounds[1] * d0; + result.partial = true; + return result; + } + +template DynamicArray2 DynamicArray3::operator[](int d0) + { +#if !defined (NDEBUG) + assert(this->elements != 0 && "Trying to dereference undefined array"); + assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds"); +#endif + DynamicArray2 result; + result.bounds = this->bounds+1; + result.elements = this->elements + this->bounds[1] * this->bounds[2] * d0; + result.partial = true; + return result; + } + +template DynamicArray3 DynamicArray4::operator[](int d0) + { +#if !defined (NDEBUG) + assert(this->elements != 0 && "Trying to dereference undefined array"); + assert(0 <= d0 && d0 < this->bounds[0] && "Array subscript out of bounds"); +#endif + + DynamicArray3 result; + result.bounds = this->bounds+1; + result.elements = this->elements + this->bounds[1] * this->bounds[2] * this->bounds[3] * d0; + result.partial = true; + return result; + } + + +template + DynamicArray::DynamicArray(DynamicArray &D) + { + assert(D.elements != 0 && "Trying to copy an undefined array"); + partial = true; + bounds = D.bounds; + elements = D.elements; + } + +} // namespace #endif diff --git a/omegalib/omega/include/basic/List.h b/omegalib/omega/include/basic/List.h index ee394d9..28ab1d5 100644 --- a/omegalib/omega/include/basic/List.h +++ b/omegalib/omega/include/basic/List.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace omega { @@ -77,14 +78,156 @@ private: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_List(T) template class List; \ template class List_Iterator; \ instantiate_Only_List_Element(T) \ instantiate_Sequence(T) +namespace omega { + +template List_Iterator::List_Iterator(List &l) +: List_Element_Iterator(l.contents) {} + +template List_Iterator::List_Iterator(const List &l) +: List_Element_Iterator(l.contents) {} + +template List_Iterator::List_Iterator() +: List_Element_Iterator(0) {} + +template Iterator *List::new_iterator() +{ + return new List_Iterator(*this); +} + +template const T &List::operator[](int i) const +{ + assert(i > 0 && "Subscript out of bounds"); + List_Iterator p(*this); + + while(--i > 0 && p) + p++; + + if (p) + return *p; + else + return *((T *)0); +} + +template T &List::operator[](int i) +{ + assert(i > 0 && "Subscript out of bounds"); + List_Iterator p(*this); + + while(--i > 0 && p) + p++; + + if (p) + return *p; + else + return *((T *)0); +} + +template int List::index(const T &item) const +{ + List_Iterator p(*this); + int i = 1; + + while(p && *p != item) + { + p++; + i++; + } + if (p) + return i; + else + return 0; +} + +template int List::size() const + { + int i = 0; + List_Element * p = contents; + while (p) + { + p = p->tail; + i++; + } + return i; + } + +template T &List::front() const + { + return contents->head; + } + +template T List::remove_front() + { + List_Element *frunt = contents; + contents = contents->tail; + T fruntT = frunt->head; + frunt->tail = 0; + delete frunt; + return fruntT; + } + +template void List::prepend(const T &item) + { + contents = new List_Element(item, contents); + } + + +template void List::append(const T &item) + { + *(end()) = new List_Element(item, 0); + } + +template void List::ins_after(List_Iterator i, + const T &item) + { +#if ! defined NDEBUG + for (List_Element *e = contents; e != &(i.element()); e=e->tail) + { + assert(e); + } +#endif + i.element().tail = new List_Element(item, i.element().tail); + } + +template void List::del_front() + { + List_Element *e = contents; + contents = contents->tail; + e->tail = 0; + delete e; + } + +template void List::del_after(List_Iterator i) + { +#if ! defined NDEBUG + for (List_Element *e0 = contents; e0 != &(i.element()); e0=e0->tail) + { + assert(e0); + } +#endif + List_Element *e = i.element().tail; + i.element().tail = e->tail; + e->tail = 0; + delete e; + } + +template void List::clear() + { + delete contents; + contents = 0; + } + +template void List::join(List &consumed) + { + List_Element *e = consumed.contents; + consumed.contents = 0; + *(end()) = e; + } + +} // namespace #endif diff --git a/omegalib/omega/include/basic/Map.h b/omegalib/omega/include/basic/Map.h index f94a10c..25a116d 100644 --- a/omegalib/omega/include/basic/Map.h +++ b/omegalib/omega/include/basic/Map.h @@ -55,14 +55,73 @@ private: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_Map(T1,T2) template class Map; \ template class MapElement; \ template class MapElementIterator; #define instantiate_MapElement(T1,T2) instantiate_Map(T1,T2) #define instantiate_MapElementIterator(T1,T2) instantiate_Map(T1,T2) +namespace omega { + +template MapElement:: MapElement(const MapElement& M) { + if (M.tail) tail = new MapElement(*M.tail); + else tail = 0; + k = M.k; + v = M.v; + } + +template MapElement & + MapElement:: operator=(const MapElement& M) { + if (this != &M) { + if (tail) delete tail; + if (M.tail) tail = new MapElement(*M.tail); + else tail = 0; + k = M.k; + v = M.v; + } + return *this; + } + + + + +#if ! defined linux +template Map ::Map(const V &default_value) +#else +template Map ::Map(V default_value) +#endif + : _default_value(default_value) + { + contents = 0; + } + +template Map ::~Map() + { + delete contents; + } + +template V Map::operator()(K k) const { + MapElement * P = contents; + while (P) { + if (P->k == k) return P->v; + P = P->tail; + }; + return _default_value; + } + +template V & Map::operator[](K k) { + MapElement * P = contents; + while (P) { + if (P->k == k) return P->v; + P = P->tail; + }; + P = new MapElement ; + P->k = k; + P->v = _default_value; + P->tail = contents; + contents = P; + return P->v; + } + +} // namespace #endif diff --git a/omegalib/omega/include/basic/Section.h b/omegalib/omega/include/basic/Section.h index 60821d1..7a4d241 100644 --- a/omegalib/omega/include/basic/Section.h +++ b/omegalib/omega/include/basic/Section.h @@ -5,6 +5,7 @@ */ #include +#include namespace omega { @@ -51,13 +52,87 @@ private: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #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 diff --git a/omegalib/omega/include/basic/SimpleList.h b/omegalib/omega/include/basic/SimpleList.h index a08b307..104390d 100644 --- a/omegalib/omega/include/basic/SimpleList.h +++ b/omegalib/omega/include/basic/SimpleList.h @@ -81,13 +81,114 @@ private: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_Simple_List(T) template class Simple_List; \ template class Simple_List_Iterator; \ instantiate_Only_List_Element(T) \ instantiate_Sequence(T) +namespace omega { + +template Simple_List_Iterator::Simple_List_Iterator(Simple_List &l) +: List_Element_Iterator(l.contents) {} + +template Simple_List_Iterator::Simple_List_Iterator(const Simple_List &l) +: List_Element_Iterator(l.contents) {} + +template Simple_List_Iterator::Simple_List_Iterator() +: List_Element_Iterator(0) {} + +template Iterator *Simple_List::new_iterator() +{ + return new Simple_List_Iterator(*this); +} + +template const T &Simple_List::operator[](int i) const +{ + Simple_List_Iterator p(*this); + + while(--i > 0 && p) + p++; + + if (p) + return *p; + else + return *((T *)0); +} + +template T &Simple_List::operator[](int i) +{ + Simple_List_Iterator p(*this); + + while(--i > 0 && p) + p++; + + if (p) + return *p; + else + return *((T *)0); +} + + +template int Simple_List::size() const + { + int i = 0; + List_Element * p = contents; + while (p) + { + p = p->tail; + i++; + } + return i; + } + +template T &Simple_List::front() const + { + return contents->head; + } + +template T Simple_List::remove_front() + { + List_Element *frunt = contents; + contents = contents->tail; + T fruntT = frunt->head; + frunt->tail = 0; + delete frunt; + return fruntT; + } + +template void Simple_List::prepend(const T &item) + { + contents = new List_Element(item, contents); + } + + +template void Simple_List::append(const T &item) + { + *(end()) = new List_Element(item, 0); + } + + +template void Simple_List::del_front() + { + List_Element *e = contents; + contents = contents->tail; + e->tail = 0; + delete e; + } + + +template void Simple_List::clear() + { + delete contents; + contents = 0; + } + +template void Simple_List::join(Simple_List &consumed) + { + List_Element *e = consumed.contents; + consumed.contents = 0; + *(end()) = e; + } + +} // namespace #endif diff --git a/omegalib/omega/include/basic/Tuple.c b/omegalib/omega/include/basic/Tuple.c index ce99e82..ee6648f 100644 --- a/omegalib/omega/include/basic/Tuple.c +++ b/omegalib/omega/include/basic/Tuple.c @@ -149,7 +149,7 @@ template void Tuple::join(Tuple& t) { assert(alloc_sz >= sz); for(int i=0; i void Tuple::clear() { if (sz) delete [] data; data = 0; alloc_sz = 0; sz = 0; } diff --git a/omegalib/omega/include/basic/Tuple.h b/omegalib/omega/include/basic/Tuple.h index 28e83bd..7bb3c24 100644 --- a/omegalib/omega/include/basic/Tuple.h +++ b/omegalib/omega/include/basic/Tuple.h @@ -79,12 +79,258 @@ private: } // namespace -#if ! defined DONT_INCLUDE_TEMPLATE_CODE -#include -#endif - #define instantiate_Tuple(T) template class Tuple; \ template class Tuple_Iterator; \ instantiate_Sequence(T) - + +namespace omega { + +template T &Tuple::operator[](int index) + { + assert(1 <= index && index <= sz); return data[index-1]; + } + +template const T &Tuple::operator[](int index) const + { + assert(1 <= index && index <= sz); return data[index-1]; + } + + +template Tuple::~Tuple() + { + if (data) + delete [] data; + } + +template Tuple::Tuple() : sz(0), alloc_sz(0), + prealloc_min(20),prealloc_pad(5), data(0) +{ + // nothing needs be done + } + +template Tuple::Tuple(int size) : sz(size), + prealloc_min(20),prealloc_pad(5) +{ + if (sz > 0) + { + alloc_sz = prealloc_size(sz); + data = new T[alloc_sz]; + assert(alloc_sz >= sz); + //Need some handling for out of memory. + assert (data!=0); + } + else { + alloc_sz = 0; + data = 0; + } +} + + +template Tuple::Tuple(const Tuple& t) + : sz(t.sz), alloc_sz(t.alloc_sz), prealloc_min(20),prealloc_pad(5) +{ + if (sz > 0) { + data = new T[alloc_sz]; + assert (data!=0); + assert (alloc_sz >= sz); + for (int i=0; i Tuple& Tuple::operator=(const Tuple& t) +{ + if (this != &t) { // Delete this + if (data) + delete [] data; + sz = t.sz; + alloc_sz = t.alloc_sz; + assert(alloc_sz >= sz); + if (sz > 0) { // Copy old + data = new T[alloc_sz]; + assert (data!=0); + for (int i=0; i void Tuple::reallocate(const int req_size) +{ + if (alloc_sz >= req_size) { // if (sz >= req_size), does this. + sz = req_size; + return; + } + alloc_sz = prealloc_size(req_size); + T* tmp_data = new T[alloc_sz]; + for(int i=0;i= req_size); +} + +template void Tuple::delete_last() +{ +assert(sz > 0); +sz --; +} + +template void Tuple::append(const T &v) +{ + // Check if reallocation is necessary. + if (sz == 0) { // Empty Tuple + assert(alloc_sz >= 0); // May be nonzero for cleared tuple + + if(alloc_sz == 0) { // If it's > 1 no allocation is necessary + alloc_sz = prealloc_size(1); + data = new T[alloc_sz]; + } + assert (alloc_sz > 0 && data != 0); + } else { + if(sz == alloc_sz) { // Requires new allocation + alloc_sz = realloc_size(alloc_sz); + T * data_tmp = new T[alloc_sz]; + assert (data_tmp!=0); + assert (alloc_sz > sz); + for (int i=0; i= sz); + data[sz++] = v; +} + +template void Tuple::append(const Tuple& t) { + int old_sz = sz; + reallocate(t.size()+size()); + assert(alloc_sz >= sz); + for(int i=0; i void Tuple::join(Tuple& t) { + int old_sz = sz; + reallocate(t.size()+size()); + assert(alloc_sz >= sz); + for(int i=0; i void Tuple::clear() { if (sz) delete [] data; data = 0; alloc_sz = 0; sz = 0; } + +template int Tuple::empty() const { return (sz == 0); } + +template Iterator *Tuple::new_iterator() +{ + return new Tuple_Iterator(*this); +} + +template int Tuple::index(const T & var) const +/* returns index or 0 if var isn't in the tuple */ +{ + int i; + for (i=0; i bool Tuple::operator == (const Tuple& b) const +{ + int i; + if (sz != b.size()) return false; + for (i=0; i Tuple_Iterator::Tuple_Iterator(const Tuple &tpl) : +current(tpl.data), lastptr(tpl.data+tpl.sz-1), firstptr(tpl.data), sz(tpl.sz) +{ +} + +template Tuple_Iterator::Tuple_Iterator(T * cr, T *frst, T * lst, + int insz) + : current(cr), lastptr(lst), firstptr(frst), sz(insz) +{ +} + +template const T & Tuple_Iterator::operator*() const +{ + assert (current<=lastptr && current>=firstptr); + return *current; +} + +template T & Tuple_Iterator::operator*() +{ + assert (current<=lastptr && current >=firstptr); + return *current; +} + +template void Tuple_Iterator::operator++(int) +{ + current++; +} + +template void Tuple_Iterator::operator++() +{ + current++; +} + +template void Tuple_Iterator::operator--(int) +{ + current--; +} + +template void Tuple_Iterator::operator--() +{ + current--; +} + +template void Tuple_Iterator::set_to_last() +{ + current = lastptr; +} + +template void Tuple_Iterator::set_to_first() +{ + current = firstptr; +} + +template void Tuple_Iterator::set_position(const int req_pos) +{ + assert(req_pos <= sz && 1 <= req_pos); + current = firstptr + (req_pos - 1); +} + + +template bool Tuple_Iterator::live() const +{ + return (current !=0 && current<=lastptr && current >= firstptr); +} + +template Iterator *Tuple_Iterator::new_copy() const { + return new Tuple_Iterator(current, firstptr, lastptr, sz); +} + +} // namespace #endif -- cgit v1.2.3-70-g09d2