diff --git a/applications/test/DynamicField/DynamicFieldTest.C b/applications/test/DynamicField/DynamicFieldTest.C
index bfff5b2453..1a7ccb463b 100644
--- a/applications/test/DynamicField/DynamicFieldTest.C
+++ b/applications/test/DynamicField/DynamicFieldTest.C
@@ -2,16 +2,16 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
- \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
+ \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
- OpenFOAM is free software: you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
+ OpenFOAM is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
@@ -19,15 +19,16 @@ License
for more details.
You should have received a copy of the GNU General Public License
- along with OpenFOAM. If not, see .
+ along with OpenFOAM; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
\*---------------------------------------------------------------------------*/
+#include "point.H"
#include "DynamicField.H"
#include "IOstreams.H"
-#include "labelField.H"
using namespace Foam;
@@ -36,44 +37,62 @@ using namespace Foam;
int main(int argc, char *argv[])
{
- {
- DynamicField dl(10);
- Pout<< "null construct dl:" << dl << endl;
- dl.append(3);
- dl.append(2);
- dl.append(1);
- Pout<< "appending : dl:" << dl << endl;
+ DynamicField testField;
+ DynamicField testField2;
- dl[2] *= 10;
- Pout<< "assigning : dl:" << dl << endl;
- }
+ testField.setSize(5);
+ testField2.setSize(5);
- {
- DynamicField dl(IStringStream("(1 2 3)")());
- Pout<< "reading : dl:" << dl << endl;
- }
+ testField[0] = testField2[0] = vector(1.0, 4.5, 6.3);
+ testField[1] = testField2[1] = vector(5.2, 2.3, 3.5);
+ testField[2] = testField2[2] = vector(7.5, 4.7, 7.7);
+ testField[3] = testField2[3] = vector(2.8, 8.2, 2.3);
+ testField[4] = testField2[4] = vector(6.1, 1.7, 8.8);
- {
- labelField lf(3);
- lf[0] = 1;
- lf[1] = 2;
- lf[2] = 3;
- DynamicField dl;
- dl = lf;
- Pout<< "assigning from labelField : dl:" << dl << endl;
- }
+ Info << "testField:" << testField << endl;
- {
- labelField lf(3);
- lf[0] = 1;
- lf[1] = 2;
- lf[2] = 3;
- DynamicField dl(lf);
- Pout<< "constructing from labelField dl:" << dl << endl;
- }
+ testField.append(vector(0.5, 4.8, 6.2));
+ Info << "testField after appending:" << testField << endl;
- Info<< "\nEnd\n";
+ testField.append(vector(2.7, 2.3, 6.1));
+
+ Info << "testField after appending:" << testField << endl;
+
+ vector elem = testField.remove();
+
+ Info << "removed element:" << elem << endl;
+ Info << "testField:" << testField << endl;
+
+ testField.append(vector(3.0, 1.3, 9.2));
+
+ Info << "testField:" << testField << endl;
+
+ testField.setSize(10, vector(1.5, 0.6, -1.0));
+
+ Info << "testField after setSize:" << testField << endl;
+
+ testField.append(testField2);
+
+ Info << "testField after appending testField2:" << testField << endl;
+
+ testField = testField2;
+
+ Info << "testField after assignment:" << testField << endl;
+
+ testField += testField2;
+
+ Info << "testField after field algebra:" << testField << endl;
+
+ testField.clear();
+
+ testField.append(vector(3.0, 1.3, 9.2));
+
+ Info << "testField after clear and append:" << testField << endl;
+
+ testField.clearStorage();
+
+ Info << "testField after clearStorage:" << testField << endl;
return 0;
}
diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C
index f557f1f43c..d01ce9bb5b 100644
--- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C
+++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.C
@@ -25,87 +25,53 @@ License
#include "DynamicField.H"
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
-namespace Foam
-{
-
-// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
-
-template
-const char* const DynamicField::typeName("DynamicField");
-
-
-// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
-
-template
-DynamicField::DynamicField(Istream& is)
+template
+Foam::DynamicField::DynamicField(Istream& is)
:
- Field(is),
- capacity_(Field::size())
+ Field(is),
+ capacity_(Field::size())
{}
-template
-tmp > DynamicField::clone() const
+template
+Foam::tmp >
+Foam::DynamicField::clone() const
{
- return tmp >(new DynamicField(*this));
+ return tmp >
+ (
+ new DynamicField(*this)
+ );
}
-// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
-
-template
-void DynamicField::setSize(const label nElem)
-{
- // allocate more capacity?
- if (nElem > capacity_)
- {
- capacity_ = max(nElem, label(1 + capacity_*2));
-
- Field::setSize(capacity_);
- }
-
- // adjust addressed size
- Field::size(nElem);
-}
-
-
-// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
-
-
// * * * * * * * * * * * * * * * IOstream Operator * * * * * * * * * * * * * //
-template
-Ostream& operator<<(Ostream& os, const DynamicField& f)
+template
+Foam::Ostream& Foam::operator<<
+(
+ Ostream& os,
+ const DynamicField& lst
+)
{
- os << static_cast&>(f);
+ os << static_cast&>(lst);
return os;
}
-template
-Ostream& operator<<(Ostream& os, const tmp >& tf)
+template
+Foam::Istream& Foam::operator>>
+(
+ Istream& is,
+ DynamicField& lst
+)
{
- os << tf();
- tf.clear();
- return os;
-}
-
-
-template
-Istream& operator>>(Istream& is, DynamicField& lst)
-{
- is >> static_cast&>(lst);
- lst.capacity_ = lst.Field::size();
+ is >> static_cast&>(lst);
+ lst.capacity_ = lst.Field::size();
return is;
}
-// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
-
-} // End namespace Foam
-
-
// ************************************************************************* //
diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H
index a464ac9d22..d05d2f5d84 100644
--- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H
+++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicField.H
@@ -25,9 +25,10 @@ Class
Foam::DynamicField
Description
- Dynamically sized Field. WIP.
+ Dynamically sized Field.
SourceFiles
+ DynamicFieldI.H
DynamicField.C
\*---------------------------------------------------------------------------*/
@@ -44,89 +45,78 @@ namespace Foam
// Forward declaration of friend functions and operators
-template
+template
class DynamicField;
-template
-Ostream& operator<<(Ostream&, const DynamicField&);
-
-template
-Ostream& operator<<(Ostream&, const tmp >&);
-
-template
-Istream& operator>>(Istream&, DynamicField&);
+template
+Ostream& operator<<
+(
+ Ostream&,
+ const DynamicField&
+);
+template
+Istream& operator>>
+(
+ Istream&,
+ DynamicField&
+);
/*---------------------------------------------------------------------------*\
Class DynamicField Declaration
\*---------------------------------------------------------------------------*/
-template
+template
class DynamicField
:
- public Field
+ public Field
{
-
// Private data
//- The capacity (allocated size) of the underlying field.
label capacity_;
- //- Construct given size and initial value
- DynamicField(const label, const Type&);
-
- //- Construct as copy of tmp
-# ifdef ConstructFromTmp
- DynamicField(const tmp >&);
-# endif
-
- //- Construct from a dictionary entry
- DynamicField(const word&, const dictionary&, const label);
-
public:
- // Static data members
-
- static const char* const typeName;
-
-
// Static Member Functions
//- Return a null field
- inline static const DynamicField& null()
+ inline static const DynamicField& null()
{
- return *reinterpret_cast< DynamicField* >(0);
+ return *reinterpret_cast
+ <
+ DynamicField*
+ >(0);
}
// Constructors
//- Construct null
- // Used for temporary fields which are initialised after construction
- DynamicField();
+ inline DynamicField();
- //- Construct given size
- // Used for temporary fields which are initialised after construction
+ //- Construct given size.
explicit inline DynamicField(const label);
- //- Construct as copy of a UList\
- explicit inline DynamicField(const UList&);
+ //- Construct from UList. Size set to UList size.
+ // Also constructs from DynamicField with different sizing parameters.
+ explicit inline DynamicField(const UList&);
- //- Construct by transferring the List contents
- explicit inline DynamicField(const Xfer >&);
+ //- Construct by transferring the parameter contents
+ explicit inline DynamicField(const Xfer >&);
//- Construct by 1 to 1 mapping from the given field
inline DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const labelList& mapAddressing
);
//- Construct by interpolative mapping from the given field
inline DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const labelListList& mapAddressing,
const scalarListList& weights
);
@@ -134,59 +124,129 @@ public:
//- Construct by mapping from the given field
inline DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const FieldMapper& map
);
- //- Construct as copy
- inline DynamicField(const DynamicField&);
-
- //- Construct as copy or re-use as specified.
- inline DynamicField(DynamicField&, bool reUse);
+ //- Construct copy
+ inline DynamicField(const DynamicField&);
//- Construct by transferring the Field contents
- inline DynamicField(const Xfer >&);
+ inline DynamicField
+ (
+ const Xfer >&
+ );
- //- Construct from Istream
- inline DynamicField(Istream&);
+ //- Construct from Istream. Size set to size of list read.
+ explicit DynamicField(Istream&);
//- Clone
- tmp > clone() const;
+ tmp > clone() const;
// Member Functions
- //- Size of the underlying storage.
- inline label capacity() const;
+ // Access
- //- Append an element at the end of the list
- inline void append(const Type&);
+ //- Size of the underlying storage.
+ inline label capacity() const;
- //- Alter the addressed list size.
- // New space will be allocated if required.
- // Use this to resize the list prior to using the operator[] for
- // setting values (as per List usage).
- void setSize(const label nElem);
+ // Edit
- // Member operators
+ //- Alter the size of the underlying storage.
+ // The addressed size will be truncated if needed to fit, but will
+ // remain otherwise untouched.
+ // Use this or reserve() in combination with append().
+ inline void setCapacity(const label);
- inline void operator=(const DynamicField&);
- inline void operator=(const UList&);
- inline void operator=(const tmp >&);
+ //- Alter the addressed list size.
+ // New space will be allocated if required.
+ // Use this to resize the list prior to using the operator[] for
+ // setting values (as per List usage).
+ inline void setSize(const label);
- //- Return element of Field.
- using Field::operator[];
+ //- Alter the addressed list size and fill new space with a
+ // constant.
+ inline void setSize(const label, const T&);
- // IOstream operators
+ //- Alter the addressed list size.
+ // New space will be allocated if required.
+ // Use this to resize the list prior to using the operator[] for
+ // setting values (as per List usage).
+ inline void resize(const label);
- friend Ostream& operator<<
- (Ostream&, const DynamicField&);
+ //- Alter the addressed list size and fill new space with a
+ // constant.
+ inline void resize(const label, const T&);
- friend Ostream& operator<<
- (Ostream&, const tmp >&);
+ //- Reserve allocation space for at least this size.
+ // Never shrinks the allocated size, use setCapacity() for that.
+ inline void reserve(const label);
- friend Istream& operator>>
- (Istream&, DynamicField&);
+ //- Clear the addressed list, i.e. set the size to zero.
+ // Allocated size does not change
+ inline void clear();
+
+ //- Clear the list and delete storage.
+ inline void clearStorage();
+
+ //- Shrink the allocated space to the number of elements used.
+ // Returns a reference to the DynamicField.
+ inline DynamicField& shrink();
+
+ //- Transfer contents to the Xfer container as a plain List
+ inline Xfer > xfer();
+
+
+ // Member Operators
+
+ //- Append an element at the end of the list
+ inline DynamicField& append
+ (
+ const T&
+ );
+
+ //- Append a List at the end of this list
+ inline DynamicField& append
+ (
+ const UList&
+ );
+
+ //- Remove and return the top element
+ inline T remove();
+
+ //- Return non-const access to an element, resizing list if
+ // necessary
+ inline T& operator()(const label);
+
+ //- Assignment of all addressed entries to the given value
+ inline void operator=(const T&);
+
+ //- Assignment from DynamicField
+ inline void operator=
+ (
+ const DynamicField&
+ );
+
+ //- Assignment from UList
+ inline void operator=(const UList&);
+
+
+ // IOstream operators
+
+ // Write DynamicField to Ostream.
+ friend Ostream& operator<<
+ (
+ Ostream&,
+ const DynamicField&
+ );
+
+ //- Read from Istream, discarding contents of existing DynamicField.
+ friend Istream& operator>>
+ (
+ Istream&,
+ DynamicField&
+ );
};
diff --git a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H
index bc54c71fbd..588ca07440 100644
--- a/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H
+++ b/src/OpenFOAM/fields/Fields/DynamicField/DynamicFieldI.H
@@ -23,175 +23,441 @@ License
\*---------------------------------------------------------------------------*/
-#include "DynamicField.H"
-
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
-template
-Foam::DynamicField::DynamicField()
+template
+inline Foam::DynamicField::DynamicField()
:
- Field(),
- capacity_(0)
+ Field(0),
+ capacity_(Field::size())
{}
-template
-Foam::DynamicField::DynamicField(const label size)
+template
+inline Foam::DynamicField::DynamicField
+(
+ const label nElem
+)
:
- Field(size),
- capacity_(Field::size())
+ Field(nElem),
+ capacity_(Field::size())
{
- Field::size(0);
+ // we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
+ Field::size(0);
}
-template
-inline Foam::DynamicField::DynamicField
+template
+inline Foam::DynamicField::DynamicField
(
- const UList& lst
+ const UList& lst
)
:
- Field(lst),
- capacity_(Field::size())
+ Field(lst),
+ capacity_(Field::size())
{}
-template
-inline Foam::DynamicField::DynamicField
+template
+inline Foam::DynamicField::DynamicField
(
- const Xfer >& lst
+ const Xfer >& lst
)
:
- Field(lst),
- capacity_(Field::size())
+ Field(lst),
+ capacity_(Field::size())
{}
-template
-Foam::DynamicField::DynamicField
+template
+inline Foam::DynamicField::DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const labelList& mapAddressing
)
:
- Field(mapF, mapAddressing),
- capacity_(Field::size())
+ Field(mapF, mapAddressing),
+ capacity_(Field::size())
{}
-template
-Foam::DynamicField::DynamicField
+template
+inline Foam::DynamicField::DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const labelListList& mapAddressing,
const scalarListList& weights
)
:
- Field(mapF, mapAddressing, weights),
- capacity_(Field::size())
+ Field(mapF, mapAddressing, weights),
+ capacity_(Field::size())
{}
//- Construct by mapping from the given field
-template
-Foam::DynamicField::DynamicField
+template
+inline Foam::DynamicField::DynamicField
(
- const UList& mapF,
+ const UList& mapF,
const FieldMapper& map
)
:
- DynamicField(mapF, map),
- capacity_(Field::size())
+ Field(mapF, map),
+ capacity_(Field::size())
{}
-template
-Foam::DynamicField::DynamicField(const DynamicField& f)
+template
+inline Foam::DynamicField::DynamicField
+(
+ const DynamicField& lst
+)
:
- Field(f),
- capacity_(Field::size())
+ Field(lst),
+ capacity_(lst.capacity())
{}
-template
-Foam::DynamicField::DynamicField(DynamicField& f, bool reUse)
+template
+inline Foam::DynamicField::DynamicField
+(
+ const Xfer >& lst
+)
:
- Field(f, reUse),
- capacity_(Field::size())
-{}
-
-
-template
-Foam::DynamicField::DynamicField(const Xfer >& f)
-:
- Field(f),
- capacity_(Field::size())
+ Field(lst),
+ capacity_(Field::size())
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
-template
-Foam::label Foam::DynamicField::capacity() const
+template
+inline Foam::label Foam::DynamicField::capacity()
+const
{
return capacity_;
}
-template
-void Foam::DynamicField::append(const Type& t)
+template
+inline void Foam::DynamicField::setCapacity
+(
+ const label nElem
+)
{
- label elemI = Field::size();
+ label nextFree = Field::size();
+ capacity_ = nElem;
+
+ if (nextFree > capacity_)
+ {
+ // truncate addressed sizes too
+ nextFree = capacity_;
+ }
+ // we could also enforce SizeInc granularity when (!SizeMult || !SizeDiv)
+
+ Field::setSize(capacity_);
+ Field::size(nextFree);
+}
+
+
+template
+inline void Foam::DynamicField::reserve
+(
+ const label nElem
+)
+{
+ // allocate more capacity?
+ if (nElem > capacity_)
+ {
+// TODO: convince the compiler that division by zero does not occur
+// if (SizeInc && (!SizeMult || !SizeDiv))
+// {
+// // resize with SizeInc as the granularity
+// capacity_ = nElem;
+// unsigned pad = SizeInc - (capacity_ % SizeInc);
+// if (pad != SizeInc)
+// {
+// capacity_ += pad;
+// }
+// }
+// else
+ {
+ capacity_ = max
+ (
+ nElem,
+ label(SizeInc + capacity_ * SizeMult / SizeDiv)
+ );
+ }
+
+ // adjust allocated size, leave addressed size untouched
+ label nextFree = Field::size();
+ Field::setSize(capacity_);
+ Field::size(nextFree);
+ }
+}
+
+
+template
+inline void Foam::DynamicField::setSize
+(
+ const label nElem
+)
+{
+ // allocate more capacity?
+ if (nElem > capacity_)
+ {
+// TODO: convince the compiler that division by zero does not occur
+// if (SizeInc && (!SizeMult || !SizeDiv))
+// {
+// // resize with SizeInc as the granularity
+// capacity_ = nElem;
+// unsigned pad = SizeInc - (capacity_ % SizeInc);
+// if (pad != SizeInc)
+// {
+// capacity_ += pad;
+// }
+// }
+// else
+ {
+ capacity_ = max
+ (
+ nElem,
+ label(SizeInc + capacity_ * SizeMult / SizeDiv)
+ );
+ }
+
+ Field::setSize(capacity_);
+ }
+
+ // adjust addressed size
+ Field::size(nElem);
+}
+
+
+template
+inline void Foam::DynamicField::setSize
+(
+ const label nElem,
+ const T& t
+)
+{
+ label nextFree = Field::size();
+ setSize(nElem);
+
+ // set new elements to constant value
+ while (nextFree < nElem)
+ {
+ this->operator[](nextFree++) = t;
+ }
+}
+
+
+template
+inline void Foam::DynamicField::resize
+(
+ const label nElem
+)
+{
+ this->setSize(nElem);
+}
+
+
+template
+inline void Foam::DynamicField::resize
+(
+ const label nElem,
+ const T& t
+)
+{
+ this->setSize(nElem, t);
+}
+
+
+template
+inline void Foam::DynamicField::clear()
+{
+ Field::size(0);
+}
+
+
+template
+inline void Foam::DynamicField::clearStorage()
+{
+ Field::clear();
+ capacity_ = 0;
+}
+
+
+template
+inline Foam::DynamicField&
+Foam::DynamicField::shrink()
+{
+ label nextFree = Field::size();
+ if (capacity_ > nextFree)
+ {
+ // use the full list when resizing
+ Field::size(capacity_);
+
+ // the new size
+ capacity_ = nextFree;
+ Field::setSize(capacity_);
+ Field::size(nextFree);
+ }
+ return *this;
+}
+
+
+template
+inline Foam::Xfer >
+Foam::DynamicField::xfer()
+{
+ return xferMoveTo< List >(*this);
+}
+
+
+template
+inline Foam::DynamicField&
+Foam::DynamicField::append
+(
+ const T& t
+)
+{
+ const label elemI = List::size();
setSize(elemI + 1);
this->operator[](elemI) = t;
+ return *this;
+}
+
+
+template
+inline Foam::DynamicField&
+Foam::DynamicField::append
+(
+ const UList& lst
+)
+{
+ if (this == &lst)
+ {
+ FatalErrorIn
+ (
+ "DynamicField::append"
+ "(const UList&)"
+ ) << "attempted appending to self" << abort(FatalError);
+ }
+
+ label nextFree = List::size();
+ setSize(nextFree + lst.size());
+
+ forAll(lst, elemI)
+ {
+ this->operator[](nextFree++) = lst[elemI];
+ }
+ return *this;
+}
+
+
+template