VRWGraph.H 7.94 KB
Newer Older
Franjo's avatar
Franjo committed
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  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
Andrew Heather's avatar
Andrew Heather committed
9
    This file is part of OpenFOAM.
Franjo's avatar
Franjo committed
10

Andrew Heather's avatar
Andrew Heather committed
11 12 13 14
    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.
Franjo's avatar
Franjo committed
15

Andrew Heather's avatar
Andrew Heather committed
16
    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
Franjo's avatar
Franjo committed
17 18 19 20 21
    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
Andrew Heather's avatar
Andrew Heather committed
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
Franjo's avatar
Franjo committed
23 24

Class
Mark Olesen's avatar
Mark Olesen committed
25
    Foam::Module::VRWGraph
Franjo's avatar
Franjo committed
26 27 28

Description
    This class is an implementation of a graph with variable column width.
29
    The implementation is memory efficient.
Franjo's avatar
Franjo committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43

SourceFiles
    VRWGraphI.H
    VRWGraph.C

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

#ifndef VRWGraph_H
#define VRWGraph_H

#include "labelLongList.H"
#include "DynList.H"
#include "bool.H"
#include "error.H"
44 45
#include "graphRow.H"
#include "graphConstRow.H"
Franjo's avatar
Franjo committed
46 47 48 49 50

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

namespace Foam
{
51 52
namespace Module
{
Franjo's avatar
Franjo committed
53

54
// Forward declarations
55
class VRWGraph;
56
Ostream& operator<<(Ostream&, const VRWGraph&);
Franjo's avatar
Franjo committed
57

Franjo's avatar
Franjo committed
58 59 60
class rowElement
{
    // Private data
Andrew Heather's avatar
Andrew Heather committed
61

Franjo's avatar
Franjo committed
62
        //- starting element of the row
Franjo's avatar
Franjo committed
63
        label start_;
Franjo's avatar
Franjo committed
64 65

        //- number of elements in the row
Franjo's avatar
Franjo committed
66
        label size_;
Franjo's avatar
Franjo committed
67

Andrew Heather's avatar
Andrew Heather committed
68 69 70 71

public:

    // Constructors
Franjo's avatar
Franjo committed
72

73
        rowElement() = default;
Andrew Heather's avatar
Andrew Heather committed
74

Franjo's avatar
Franjo committed
75
        inline rowElement(const label i, const label j)
Franjo's avatar
Franjo committed
76 77 78 79
        :
            start_(i),
            size_(j)
        {}
Franjo's avatar
Franjo committed
80

Andrew Heather's avatar
Andrew Heather committed
81 82

    //- Destructor
83
    ~rowElement() = default;
Andrew Heather's avatar
Andrew Heather committed
84 85 86


    // Member functions
Franjo's avatar
Franjo committed
87

Franjo's avatar
Franjo committed
88
        inline label start() const
Franjo's avatar
Franjo committed
89 90 91
        {
            return start_;
        }
Andrew Heather's avatar
Andrew Heather committed
92

Franjo's avatar
Franjo committed
93
        inline label& start()
Franjo's avatar
Franjo committed
94 95 96
        {
            return start_;
        }
Franjo's avatar
Franjo committed
97

Franjo's avatar
Franjo committed
98 99 100 101
        inline label size() const
        {
            return size_;
        }
Andrew Heather's avatar
Andrew Heather committed
102

Franjo's avatar
Franjo committed
103 104 105 106 107 108
        inline label& size()
        {
            return size_;
        }
};

Andrew Heather's avatar
Andrew Heather committed
109

110
// Useful typedefs
111
typedef const graphConstRow<VRWGraph> constRow;
112 113 114
typedef graphRow<VRWGraph> row;


Franjo's avatar
Franjo committed
115
/*---------------------------------------------------------------------------*\
Andrew Heather's avatar
Andrew Heather committed
116
                          Class VRWGraph Declaration
Franjo's avatar
Franjo committed
117 118 119 120 121
\*---------------------------------------------------------------------------*/

class VRWGraph
{
    // Private data
Andrew Heather's avatar
Andrew Heather committed
122

Franjo's avatar
Franjo committed
123
        //- list containing data
Franjo's avatar
Franjo committed
124
        labelLongList data_;
Franjo's avatar
Franjo committed
125

Franjo's avatar
Franjo committed
126 127 128
        //- number of rows
        LongList<rowElement> rows_;

Andrew Heather's avatar
Andrew Heather committed
129

Franjo's avatar
Franjo committed
130
    // Private member functions
Andrew Heather's avatar
Andrew Heather committed
131

Franjo's avatar
Franjo committed
132 133 134
        //- check index
        inline void checkIndex(const label i, const label j) const;

Andrew Heather's avatar
Andrew Heather committed
135

Franjo's avatar
Franjo committed
136
    // Enumerators
Andrew Heather's avatar
Andrew Heather committed
137

Franjo's avatar
Franjo committed
138 139 140 141 142 143 144
        enum typeOfEntries
        {
            NONE = 0,
            INVALIDROW=-10,
            FREEENTRY=-11,
            FREESTART=-12
        };
Franjo's avatar
Franjo committed
145

Andrew Heather's avatar
Andrew Heather committed
146

Franjo's avatar
Franjo committed
147
public:
Franjo's avatar
Franjo committed
148

Franjo's avatar
Franjo committed
149 150 151 152
    // Friend classes

        friend class VRWGraphSMPModifier;

Andrew Heather's avatar
Andrew Heather committed
153

Franjo's avatar
Franjo committed
154 155 156 157 158 159 160
    // Constructors

        //- Construct null
        inline VRWGraph();

        //- Construct given number of rows
        explicit inline VRWGraph(const label size);
Franjo's avatar
Franjo committed
161

Franjo's avatar
Franjo committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        //- Construct given number of rows and row size
        explicit inline VRWGraph
        (
            const label nRows,
            const label nColumnsInRow
        );

        //- Construct to given number of rows, row size and initialize
        explicit inline VRWGraph
        (
            const label nRows,
            const label nColumnsInRow,
            const label t
        );

Andrew Heather's avatar
Andrew Heather committed
177
        //- Copy constructor
Franjo's avatar
Franjo committed
178 179 180
        inline VRWGraph(const VRWGraph&);


Andrew Heather's avatar
Andrew Heather committed
181
    //- Destructor
182
    ~VRWGraph() = default;
Andrew Heather's avatar
Andrew Heather committed
183

Franjo's avatar
Franjo committed
184 185 186 187 188 189 190

    // Member Functions

        // Access

            //- Returns the number of rows
            inline label size() const;
Franjo's avatar
Franjo committed
191

Franjo's avatar
Franjo committed
192 193 194
            //- Returns the number of elements in the given row
            inline label sizeOfRow(const label rowI) const;

Andrew Heather's avatar
Andrew Heather committed
195

Franjo's avatar
Franjo committed
196 197 198 199
        // Edit

            //- Reset the number of rows
            inline void setSize(const label);
Franjo's avatar
Franjo committed
200

Franjo's avatar
Franjo committed
201 202 203 204 205 206 207
            //- Reset the number of rows. The second argument specifies
            //- the reserved column width
            inline void setSizeAndColumnWidth
            (
                const label newNumRows,
                const label rcWidth
            );
Franjo's avatar
Franjo committed
208

Franjo's avatar
Franjo committed
209 210 211
            //- Set the number of rows and the size of each row
            template<class ListType>
            inline void setSizeAndRowSize(const ListType&);
Franjo's avatar
Franjo committed
212

Franjo's avatar
Franjo committed
213 214 215 216 217 218
            //- Reset the size of the given row
            inline void setRowSize(const label rowI, const label newSize);

            //- Clear the graph
            inline void clear();

Andrew Heather's avatar
Andrew Heather committed
219

Franjo's avatar
Franjo committed
220 221 222 223 224
    // Member Operators

        //- Append a list as a row at the end of the graph
        template<class ListType>
        inline void appendList(const ListType& l);
Franjo's avatar
Franjo committed
225

Franjo's avatar
Franjo committed
226 227
        //- Append an element to the given row
        inline void append(const label rowI, const label);
Franjo's avatar
Franjo committed
228

Franjo's avatar
Franjo committed
229 230
        //- Append an element to the given row if it does not exist there
        inline void appendIfNotIn(const label rowI, const label);
Franjo's avatar
Franjo committed
231

Franjo's avatar
Franjo committed
232 233 234
        //- Set row with the list
        template<class ListType>
        inline void setRow(const label rowI, const ListType& l);
Franjo's avatar
Franjo committed
235

Franjo's avatar
Franjo committed
236 237 238
        //- merge graphs with the identical number of rows
        //- into a single one. Use for SMP parallelisation
        inline void mergeGraphs(const List<VRWGraph>& graphParts);
Franjo's avatar
Franjo committed
239

Franjo's avatar
Franjo committed
240 241 242 243 244 245 246 247 248
        //- set the graph to the reverse of the original graph.
        //- the rows of such graph store the rows which contain the elements
        //- of the original graph
        template<class GraphType>
        inline void reverseAddressing
        (
            const label nRows,
            const GraphType& origGraph
        );
Franjo's avatar
Franjo committed
249

Franjo's avatar
Franjo committed
250 251
        template<class GraphType>
        inline void reverseAddressing(const GraphType& origGraph);
Franjo's avatar
Franjo committed
252

Franjo's avatar
Franjo committed
253 254 255 256 257
        inline void reverseAddressing
        (
            const label nRows,
            const VRWGraph& origGraph
        );
Franjo's avatar
Franjo committed
258

Franjo's avatar
Franjo committed
259
        inline void reverseAddressing(const VRWGraph& origGraph);
Franjo's avatar
Franjo committed
260

Franjo's avatar
Franjo committed
261 262 263
        //- optimize memory usage
        // this should be used once the graph will not be resized any more
        void optimizeMemoryUsage();
Franjo's avatar
Franjo committed
264

Franjo's avatar
Franjo committed
265
        //- check if the element is in the given row (takes linear time)
266 267
        inline bool found(const label rowI, const label e) const;
        inline label find(const label rowI, const label e) const;
Franjo's avatar
Franjo committed
268

Franjo's avatar
Franjo committed
269 270 271
        //- get and set operators
        inline label operator()(const label i, const label j) const;
        inline label& operator()(const label i, const label j);
Franjo's avatar
Franjo committed
272

Franjo's avatar
Franjo committed
273 274
        inline constRow operator[](const label i) const;
        inline row operator[](const label i);
Franjo's avatar
Franjo committed
275

Franjo's avatar
Franjo committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        //- Assignment operator
        inline void operator=(const VRWGraph&);


    // IOstream operators

        // Write VRWGraph to Ostream.
        friend Ostream& operator<<(Ostream&, const VRWGraph&);

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


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

297
} // End namespace Module
Franjo's avatar
Franjo committed
298 299 300 301 302 303 304 305
} // End namespace Foam

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

#include "VRWGraphI.H"

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

Andrew Heather's avatar
Andrew Heather committed
306 307
#define forAllRow(graph, rowI, index)                                         \
    for (Foam::label index = 0; index < (graph).sizeOfRow(rowI); ++index)
Franjo's avatar
Franjo committed
308 309 310 311 312 313

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

#endif

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