LongList.H 6.9 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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
    LongList

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 2-D graph with a fixed size of the chunks used to store the data.
    This way the data does not get copied every time array is resized, but
    only the pointers to the chunks of data.

SourceFiles
    LongListI.H
    LongList.C

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

#ifndef LongList_H
#define LongList_H

#include "label.H"
#include "bool.H"
Franjo's avatar
Franjo committed
45
#include "IOstreams.H"
Franjo's avatar
Franjo committed
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 78 79
#include "error.H"

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

namespace Foam
{

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

template<class T, label Offset>
class LongList;

template<class T, label Offset>
Ostream& operator<<
(
    Ostream&,
    const LongList<T, Offset>&
);
template<class T, label Offset>
Istream& operator>>
(
    Istream&,
    LongList<T, Offset>&
);

/*---------------------------------------------------------------------------*\
                           Class LongList Declaration
\*---------------------------------------------------------------------------*/

template<class T, label Offset = 19>
class LongList
{
    // Private data
        //- number of allocated elements
Franjo's avatar
Franjo committed
80
        label N_;
81

Franjo's avatar
Franjo committed
82
        //- number of elements in the list
Franjo's avatar
Franjo committed
83
        label nextFree_;
84

Franjo's avatar
Franjo committed
85 86
        //- number of used blocks of data
        label numBlocks_;
87

Franjo's avatar
Franjo committed
88 89 90 91
        //- maximum number of blocks that can be allocated
        //- without reallocating the list containing pointers
        //- to the chunks of data
        label numAllocatedBlocks_;
92

Franjo's avatar
Franjo committed
93 94 95 96
        //- size of blocks is calculated by powers of 2
        //- and therefore the access can be done using shift and mask
        label shift_;
        label mask_;
97

Franjo's avatar
Franjo committed
98 99 100 101 102
        //- array of pointers to the blocks of data, each of the size WIDTH
        T** dataPtr_;

    // Private member functions
        //- check index
Franjo's avatar
Franjo committed
103
        void checkIndex(const label i) const;
104

Franjo's avatar
Franjo committed
105 106
        //- initialize width and mask
        void initializeParameters();
107

Franjo's avatar
Franjo committed
108
        //- Allocate memory for the list
Franjo's avatar
Franjo committed
109
        void allocateSize(const label);
110

Franjo's avatar
Franjo committed
111 112
        //- delete all elements
        void clearOut();
113

Franjo's avatar
Franjo committed
114 115 116 117 118 119 120 121
public:

    // Constructors

        //- Construct null
        inline LongList();

        //- Construct given size
Franjo's avatar
Franjo committed
122
        explicit inline LongList(const label size);
Franjo's avatar
Franjo committed
123 124

        //- Construct to given size and initialize
Franjo's avatar
Franjo committed
125
        explicit inline LongList(const label size, const T& t);
Franjo's avatar
Franjo committed
126 127 128 129 130 131 132 133 134 135 136 137 138

        //- Copy contructor
        inline LongList(const LongList<T, Offset>&);

    // Destructor

        inline ~LongList();

    // Member Functions

        // Access

            //- Size of the active part of the list.
Franjo's avatar
Franjo committed
139
            inline label size() const;
140

Franjo's avatar
Franjo committed
141
            //- Return the binary size in number of characters of the UList
142
            //  if the element is a primitive type
Franjo's avatar
Franjo committed
143
            //  i.e. contiguous<T>() == true
Franjo's avatar
Franjo committed
144
            inline label byteSize() const;
Franjo's avatar
Franjo committed
145 146 147 148

        // Edit

            //- Reset size of List.
Franjo's avatar
Franjo committed
149
            void setSize(const label);
Franjo's avatar
Franjo committed
150 151 152 153 154 155 156

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

            //- Shrink the list to the number of elements used
            inline LongList<T, Offset>& shrink();
157

Franjo's avatar
Franjo committed
158 159 160 161 162 163 164 165
            //- transfer the list from another one without allocating it
            inline void transfer(LongList<T, Offset>&);


    // Member Operators

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

Franjo's avatar
Franjo committed
167 168 169
        //- 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);
170

Franjo's avatar
Franjo committed
171 172
        //- check if the element is in the list (takes linear time)
        inline bool contains(const T& e) const;
Franjo's avatar
Franjo committed
173
        inline label containsAtPosition(const T& e) const;
Franjo's avatar
Franjo committed
174 175

        //- Return and remove the element
Franjo's avatar
Franjo committed
176
        inline T remove(const label i);
Franjo's avatar
Franjo committed
177
        inline T removeLastElement();
178

Franjo's avatar
Franjo committed
179
        //- get and set operators
Franjo's avatar
Franjo committed
180 181
        inline const T& operator[](const label i) const;
        inline T& operator[](const label i);
Franjo's avatar
Franjo committed
182 183 184

        //- Return non-const access to an element,
        //  resizing the list if necessary
Franjo's avatar
Franjo committed
185
        inline T& operator()(const label);
186

Franjo's avatar
Franjo committed
187 188
        //- return a non-const access to an element,
        // resize the list if necessary
Franjo's avatar
Franjo committed
189
        inline T& newElmt(const label);
Franjo's avatar
Franjo committed
190 191 192

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

Franjo's avatar
Franjo committed
194 195 196 197 198 199 200
        //- Assignment operator
        inline void operator=(const LongList<T, Offset>&);


    // IOstream operators
        //- Read from stream and append to the current content
        void appendFromStream(Istream&);
201

Franjo's avatar
Franjo committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        //- Write as a dictionary entry.
        void writeEntry(Ostream& os) const;

        //- Write as a dictionary entry with keyword.
        void writeEntry(const word& keyword, Ostream& os) const;

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

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


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

} // End namespace Foam

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

#include "LongListI.H"

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

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

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

#endif

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