Pair.H 5.99 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

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.
15
16
17
18
19
20
21

    OpenFOAM 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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29
30

Class
    Foam::Pair

Description
    An ordered pair of two objects of type \<T\> with first() and second()
    elements.

31
32
33
SourceFiles
    PairI.H

34
See also
Mark Olesen's avatar
Mark Olesen committed
35
36
    Foam::Tuple2 for storing two objects of dissimilar types.

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
\*---------------------------------------------------------------------------*/

#ifndef Pair_H
#define Pair_H

#include "FixedList.H"
#include "Istream.H"

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

namespace Foam
{

/*---------------------------------------------------------------------------*\
                            Class Pair Declaration
\*---------------------------------------------------------------------------*/

54
template<class T>
55
56
class Pair
:
57
    public FixedList<T, 2>
58
59
60
61
62
63
{

public:

    // Constructors

64
        //- Null constructor
65
        inline Pair();
66
67

        //- Construct from components
68
        inline Pair(const T& f, const T& s);
69

laurence's avatar
laurence committed
70
        //- Construct from FixedList
71
72
73
74
75
76
77
        inline Pair(const FixedList<T, 2>& lst);

        //- Construct, optionally sorted with first less-than second
        inline Pair(const T& f, const T& s, const bool doSort);

        //- Construct, optionally sorted with first less-than second
        inline Pair(const FixedList<T, 2>& lst, const bool doSort);
laurence's avatar
laurence committed
78

79
        //- Construct from Istream
80
        inline Pair(Istream& is);
81
82
83
84


    // Member Functions

85
    // Access
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

        //- Return first element
        using FixedList<T, 2>::first;

        //- Return last element
        using FixedList<T, 2>::last;

        //- Return second element, which is also the last element
        inline const T& second() const;

        //- Return second element, which is also the last element
        inline T& second();

        //- Return other element
        inline const T& other(const T& a) const;


103
    // Queries
104
105
106
107
108

        //- True if first() is less-than second()
        inline bool sorted() const;


109
    // Editing
110
111
112
113
114
115

        //- Flip the Pair in-place.
        inline void flip();

        //- Sort so that first() is less-than second()
        inline void sort();
116
117


118
    // Comparison
119

120
        //- Compare Pairs
121
        //  \return
122
        //  -  0: different
123
124
        //  - +1: identical values and order used
        //  - -1: identical values, but in reversed order
125
        static inline int compare(const Pair<T>& a, const Pair<T>& b);
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


    // Hashing

        //- Symmetrical hashing for Pair data.
        //  The lower value is hashed first.
        template<class HashT=Foam::Hash<T>>
        struct SymmHash
        {
            inline unsigned operator()
            (
                const Pair<T>& obj,
                unsigned seed=0
            ) const
            {
                if (obj.first() < obj.second())
                {
                    seed = HashT()(obj.first(), seed);
                    seed = HashT()(obj.second(), seed);
                }
                else
                {
                    seed = HashT()(obj.second(), seed);
                    seed = HashT()(obj.first(), seed);
                }
                return seed;
            }
        };
};


// * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * * //

//- Hashing for Pair data, which uses Hasher for contiguous data and
//- element-wise incrementally hashing otherwise.
template<class T>
struct Hash<Pair<T>>
{
    inline unsigned operator()(const Pair<T>& obj, unsigned seed=0) const
    {
        if (contiguous<T>())
        {
            return Hasher(obj.cdata(), sizeof(obj), seed);
        }

        seed = Hash<T>()(obj.first(), seed);
        seed = Hash<T>()(obj.second(), seed);

        return seed;
    }
176
};
177
178


179
180
// * * * * * * * * * * * * * * Global Operators  * * * * * * * * * * * * * * //

181
182
183
//- Return reverse of a Pair
template<class T>
Pair<T> reverse(const Pair<T>& p)
184
{
185
    return Pair<T>(p.second(), p.first());
186
}
187
188


189
190
template<class T>
bool operator==(const Pair<T>& a, const Pair<T>& b)
191
{
192
    return (a.first() == b.first() && a.second() == b.second());
193
}
laurence's avatar
laurence committed
194

195

196
197
template<class T>
bool operator!=(const Pair<T>& a, const Pair<T>& b)
198
{
199
    return !(a == b);
200
201
202
}


203
204
template<class T>
bool operator<(const Pair<T>& a, const Pair<T>& b)
205
206
207
208
209
210
211
212
213
214
215
216
217
{
    return
    (
        a.first() < b.first()
     ||
        (
            !(b.first() < a.first())
         && a.second() < b.second()
        )
    );
}


218
219
template<class T>
bool operator<=(const Pair<T>& a, const Pair<T>& b)
220
221
222
223
224
{
    return !(b < a);
}


225
226
template<class T>
bool operator>(const Pair<T>& a, const Pair<T>& b)
227
228
229
230
231
{
    return (b < a);
}


232
233
template<class T>
bool operator>=(const Pair<T>& a, const Pair<T>& b)
234
235
236
{
    return !(a < b);
}
237
238
239
240
241
242
243
244


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

} // End namespace Foam

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

245
246
247
248
#include "PairI.H"

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

249
250
251
#endif

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