added List::append() methods

This commit is contained in:
Mark Olesen
2009-04-22 16:11:52 +02:00
parent 35adfbf525
commit 6ce8e7d0c0
3 changed files with 82 additions and 25 deletions

View File

@ -43,31 +43,33 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
List<vector> list(IStringStream("1 ((0 1 2))")()); List<vector> list1(IStringStream("1 ((0 1 2))")());
Info<< list << endl; Info<< "list1: " << list1 << endl;
List<vector> list2(IStringStream("((0 1 2) (3 4 5) (6 7 8))")()); List<vector> 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; Info<< findIndex(list2, vector(3, 4, 5)) << endl;
list2.setSize(10, vector(1, 2, 3)); list2.setSize(10, vector(1, 2, 3));
Info<< list2 << endl; Info<< "list2: " << list2 << endl;
List<vector> list3(list2.xfer()); List<vector> list3(list2.xfer());
Info<< "Transferred via the xfer() method" << endl; Info<< "Transferred via the xfer() method" << endl;
Info<< list2 << nl Info<< "list2: " << list2 << nl
<< list3 << endl; << "list3: " << list3 << endl;
// Subset // Subset
const labelList map(IStringStream("2 (0 2)")()); const labelList map(IStringStream("2 (0 2)")());
List<vector> subList3(list3, map); List<vector> subList3(list3, map);
Info<< "Elements " << map << " out of " << list3 Info<< "Elements " << map << " out of " << list3
<< " : " << subList3 << endl; << " => " << subList3 << endl;
return 0; return 0;
} }
// ************************************************************************* // // ************************************************************************* //

View File

@ -404,6 +404,61 @@ void Foam::List<T>::clear()
} }
template<class T>
void Foam::List<T>::append(const UList<T>& lst)
{
if (this == &lst)
{
FatalErrorIn
(
"List<T>::append(const UList<T>&)"
) << "attempted appending to self" << abort(FatalError);
}
label nextFree = this->size_;
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T>
void Foam::List<T>::append(const UIndirectList<T>& lst)
{
label nextFree = this->size_;
setSize(nextFree + lst.size());
forAll(lst, elemI)
{
this->operator[](nextFree++) = lst[elemI];
}
}
template<class T>
void Foam::List<T>::append(const SLList<T>& lst)
{
if (lst.size())
{
label nextFree = this->size_;
setSize(nextFree + lst.size());
for
(
typename SLList<T>::const_iterator iter = lst.begin();
iter != lst.end();
++iter
)
{
this->operator[](nextFree++) = iter();
}
}
}
// Transfer the contents of the argument List into this List // Transfer the contents of the argument List into this List
// and anull the argument list // and anull the argument list
template<class T> template<class T>
@ -559,14 +614,11 @@ void Foam::List<T>::operator=(const IndirectList<T>& lst)
if (this->size_) this->v_ = new T[this->size_]; 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];
} }
} }
}
// Assignment operator. Takes linear time. // Assignment operator. Takes linear time.
@ -581,14 +633,11 @@ void Foam::List<T>::operator=(const UIndirectList<T>& lst)
if (this->size_) this->v_ = new T[this->size_]; 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];
} }
} }
}
// Assignment operator. Takes linear time. // Assignment operator. Takes linear time.
@ -603,14 +652,11 @@ void Foam::List<T>::operator=(const BiIndirectList<T>& lst)
if (this->size_) this->v_ = new T[this->size_]; 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];
} }
} }
}
// * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * // // * * * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * //

View File

@ -181,6 +181,15 @@ public:
//- Clear the list, i.e. set size to zero. //- Clear the list, i.e. set size to zero.
void clear(); void clear();
//- Append a List at the end of this list
void append(const UList<T>&);
//- Append a UIndirectList at the end of this list
void append(const UIndirectList<T>&);
//- Append a SLList at the end of this list
void append(const SLList<T>&);
//- Transfer the contents of the argument List into this List //- Transfer the contents of the argument List into this List
// and annull the argument list. // and annull the argument list.
void transfer(List<T>&); void transfer(List<T>&);