diff --git a/applications/test/List/ListTest.C b/applications/test/List/ListTest.C index bc8901b03b..d4834cb1db 100644 --- a/applications/test/List/ListTest.C +++ b/applications/test/List/ListTest.C @@ -43,31 +43,33 @@ using namespace Foam; int main(int argc, char *argv[]) { - List list(IStringStream("1 ((0 1 2))")()); - Info<< list << endl; + List list1(IStringStream("1 ((0 1 2))")()); + Info<< "list1: " << list1 << endl; List list2(IStringStream("((0 1 2) (3 4 5) (6 7 8))")()); - Info<< list2 << endl; + Info<< "list2: " << list2 << endl; + + list1.append(list2); + Info<< "list1.append(list2): " << list1 << endl; Info<< findIndex(list2, vector(3, 4, 5)) << endl; list2.setSize(10, vector(1, 2, 3)); - Info<< list2 << endl; + Info<< "list2: " << list2 << endl; List list3(list2.xfer()); Info<< "Transferred via the xfer() method" << endl; - Info<< list2 << nl - << list3 << endl; + Info<< "list2: " << list2 << nl + << "list3: " << list3 << endl; // Subset const labelList map(IStringStream("2 (0 2)")()); List subList3(list3, map); Info<< "Elements " << map << " out of " << list3 - << " : " << subList3 << endl; + << " => " << subList3 << endl; return 0; } - // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/List/List.C b/src/OpenFOAM/containers/Lists/List/List.C index 6d20e2b3b7..429c91f820 100644 --- a/src/OpenFOAM/containers/Lists/List/List.C +++ b/src/OpenFOAM/containers/Lists/List/List.C @@ -404,6 +404,61 @@ void Foam::List::clear() } +template +void Foam::List::append(const UList& lst) +{ + if (this == &lst) + { + FatalErrorIn + ( + "List::append(const UList&)" + ) << "attempted appending to self" << abort(FatalError); + } + + label nextFree = this->size_; + setSize(nextFree + lst.size()); + + forAll(lst, elemI) + { + this->operator[](nextFree++) = lst[elemI]; + } +} + + +template +void Foam::List::append(const UIndirectList& lst) +{ + label nextFree = this->size_; + setSize(nextFree + lst.size()); + + forAll(lst, elemI) + { + this->operator[](nextFree++) = lst[elemI]; + } +} + + +template +void Foam::List::append(const SLList& lst) +{ + if (lst.size()) + { + label nextFree = this->size_; + setSize(nextFree + lst.size()); + + for + ( + typename SLList::const_iterator iter = lst.begin(); + iter != lst.end(); + ++iter + ) + { + this->operator[](nextFree++) = iter(); + } + } +} + + // Transfer the contents of the argument List into this List // and anull the argument list template @@ -559,12 +614,9 @@ void Foam::List::operator=(const IndirectList& lst) if (this->size_) this->v_ = new T[this->size_]; } - if (this->size_) + forAll(*this, i) { - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + this->operator[](i) = lst[i]; } } @@ -581,12 +633,9 @@ void Foam::List::operator=(const UIndirectList& lst) if (this->size_) this->v_ = new T[this->size_]; } - if (this->size_) + forAll(*this, i) { - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + this->operator[](i) = lst[i]; } } @@ -603,12 +652,9 @@ void Foam::List::operator=(const BiIndirectList& lst) if (this->size_) this->v_ = new T[this->size_]; } - if (this->size_) + forAll(*this, i) { - forAll(*this, i) - { - this->operator[](i) = lst[i]; - } + this->operator[](i) = lst[i]; } } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index 86876cff71..5961ff1747 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -109,7 +109,7 @@ public: List(const List&); //- Construct by transferring the parameter contents - List(const Xfer >&); + List(const Xfer< List >&); //- Construct as copy or re-use as specified. List(List&, bool reUse); @@ -181,6 +181,15 @@ public: //- Clear the list, i.e. set size to zero. void clear(); + //- Append a List at the end of this list + void append(const UList&); + + //- Append a UIndirectList at the end of this list + void append(const UIndirectList&); + + //- Append a SLList at the end of this list + void append(const SLList&); + //- Transfer the contents of the argument List into this List // and annull the argument list. void transfer(List&); @@ -195,7 +204,7 @@ public: void transfer(SortableList&); //- Transfer contents to the Xfer container - inline Xfer > xfer(); + inline Xfer< List > xfer(); //- Return subscript-checked element of UList. inline T& newElmt(const label);