DynList.H 7.58 KB
Newer Older
Franjo's avatar
Franjo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | cfMesh: A library for mesh generation
   \\    /   O peration     |
    \\  /    A nd           | Author: Franjo Juretic (franjo.juretic@c-fields.com)
     \\/     M anipulation  | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
    This file is part of cfMesh.

    cfMesh 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.

    cfMesh is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with cfMesh.  If not, see <http://www.gnu.org/licenses/>.

Class
    DynList

Description
    A dynamic list is a 1-D vector of objects of type T which resizes
    itself as necessary to accept the new objects.  Internal storage
    is a compact array and the list can be shrunk to compact storage.
    The increase of list size is controlled by three template parameters,
    which allows the list storage to either increase by the given increment
    or the given multiplier and divider (allowing non-integer multiples).

SourceFiles
    DynListI.H
    DynList.C

\*---------------------------------------------------------------------------*/

#ifndef DynList_H
#define DynList_H

#include "UList.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

// * * * * * * Forward declaration of template friend fuctions * * * * * * * //

template<class T, label staticSize>
class DynList;

template<class T, label staticSize>
Ostream& operator<<
(
    Ostream&,
    const DynList<T, staticSize>&
);
template<class T, label staticSize>
Istream& operator>>
(
    Istream&,
    DynList<T, staticSize>&
);


/*---------------------------------------------------------------------------*\
                           Class DynList Declaration
\*---------------------------------------------------------------------------*/

template<class T, label staticSize = 16>
class DynList
{
    // Private data
78 79 80 81 82 83
        //- pointer to the data
        T* dataPtr_;

        //- size of the allocated data
        label nAllocated_;

Franjo's avatar
Franjo committed
84 85 86 87 88 89 90
        //- statically allocated data (used for short lists)
        T staticData_[staticSize];

        //- Number of next free element
        label nextFree_;

    // Private member functions
91 92 93 94 95 96
        //- access to the data pointer
        inline T* data();

        //- const access to the data pointer
        inline const T* data() const;

Franjo's avatar
Franjo committed
97 98 99 100 101 102
        //- allocate list size
        inline void allocateSize(const label);

        //- check if index is inside the scope (used for debugging only)
        inline void checkIndex(const label) const;

103 104 105
        //- check if nAllocated_ is greater or equal to nextFree_
        inline void checkAllocation() const;

Franjo's avatar
Franjo committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
public:

    // Constructors

        //- Construct null
        inline DynList();

        //- Construct given size
        explicit inline DynList(const label);

        //- Construct from given size and defualt value
        explicit inline DynList(const label, const T&);

        //- Construct from UList. nextFree_ set to size().
        explicit inline DynList(const UList<T>&);

        //- Construct from other ListType
        template<class ListType>
        inline DynList(const ListType&);

        //- Copy constructor
        inline DynList(const DynList<T, staticSize>&);

        //- Construct from Istream. nextFree_ set to size().
        explicit DynList(Istream&);

    // Destructor

        inline ~DynList();


    // Member Functions

        // Access

            //- Size of the active part of the list.
            //- Direct over-ride of list size member function
            inline label size() const;

            //- Number of bytes used by the active part of the list
            //- Direct over-ride of list byteSize member function
            inline label byteSize() const;


        // Edit

            //- Reset size of List.
            void setSize(const label);

            //- Clear the list, i.e. set next free to zero.
            //  Allocated size does not change
            void clear();

            //- Shrink the List<T> to the number of elements used
            void shrink();


    // Member Operators

        //- Append an element at the end of the list
        inline void append(const T& e);

        //- Append an element at the end of the list if it is not yet
        //- present in the list (takes linear time)
        inline void appendIfNotIn(const T& e);

        //- check if the element is in the list (takes linear time)
        inline bool contains(const T& e) const;
        inline label containsAtPosition(const T& e) const;

        //- return a const reference to the last element
        inline const T& lastElement() const;

        //- Return and remove the last element
        inline T removeLastElement();
        inline T removeElement(const label i);

        //- return a refence to the element. Resize the list if necessary
        inline T& newElmt(const label);

        //- Return non-const access to an element,
        //-  resizing the list if necessary
        inline T& operator()(const label);

        //- return access to an element
        inline const T& operator[](const label) const;
        inline T& operator[](const label);

        //- return forward and reverse circular indices
        inline label fcIndex(const label index, const label offset = 1) const;
        inline label rcIndex(const label index, const label offset = 1) const;

        //- return forward and reverse circular elements
        inline const T& fcElement
        (
            const label index,
            const label offset = 1
        ) const;

        inline const T& rcElement
        (
            const label index,
            const label offset = 1
        ) const;

        //- Assignment of all entries to the given value
        inline void operator=(const T&);

        //- Copy of another list
        inline void operator=(const DynList<T, staticSize>&);

        //- Copy of another list type
        template<class ListType>
        inline void operator=(const ListType&);

221 222 223 224
        //- Compare the list with the another one
        inline bool operator==(const DynList<T, staticSize>&) const;
        inline bool operator!=(const DynList<T, staticSize>&) const;

Franjo's avatar
Franjo committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    // IOstream operators

        // Write DynList to Ostream.
        friend Ostream& operator<< <T, staticSize>
        (
            Ostream&,
            const DynList<T, staticSize>&
        );

        //- Read from Istream, discarding contents of existing DynList.
        friend Istream& operator>> <T, staticSize>
        (
            Istream&,
            DynList<T, staticSize>&
        );
};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#include "DynListI.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#ifdef NoRepository
#   include "DynList.C"
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //