summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/unordered_pair.h
blob: 9ea75b9128fd3e952f5b510ebe5e91bb11551c3a (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
// Copyright (C) 2010  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_UNORDERED_PAiR_Hh_
#define DLIB_UNORDERED_PAiR_Hh_

#include "serialize.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    struct unordered_pair 
    {
        /*!
            REQUIREMENTS ON T
                T must be default constructable, copyable, and comparable using
                operator < and ==

            WHAT THIS OBJECT REPRESENTS
                This object is very similar to the std::pair struct except unordered_pair 
                is only capable of representing an unordered set of two items rather than 
                an ordered list of two items like std::pair.  

                This is best illustrated by example.  Suppose we have the following
                five variables:
                    std::pair<int,int> p1(1, 5), p2(5,1);
                    unordered_pair<int> up1(1,5), up2(5,1), up3(6,7);

                Then it is the case that:   
                    up1 == up2
                    up1 != up3
                    p1 != p2

               So the unordered_pair doesn't care about the order of the arguments.
               In this case, up1 and up2 are both equivalent.

        !*/

        typedef T type;
        typedef T first_type;
        typedef T second_type;

        const T first;
        const T second;

        unordered_pair() : first(), second() 
        /*!
            ensures
                - #first and #second are default initialized
        !*/ {}

        unordered_pair(
            const T& a, 
            const T& b
        ) :
            first( a < b ? a : b),
            second(a < b ? b : a)
        /*!
            ensures
                - #first <= #second
                - #first and #second contain copies of the items a and b.
        !*/ {}

        template <typename U>
        unordered_pair (
            const unordered_pair <U>& p
        ) :
            first(p.first),
            second(p.second)
        /*!
            ensures
                - #*this is a copy of p
        !*/ {}

        unordered_pair& operator= (
            const unordered_pair& item
        ) 
        /*!
            ensures
                - #*this == item
        !*/
        {
            const_cast<T&>(first) = item.first;
            const_cast<T&>(second) = item.second;
            return *this;
        }
    };

// ----------------------------------------------------------------------------------------

    template <typename T>
    bool operator==(const unordered_pair<T>& a, const unordered_pair <T>& b)
    {
        return a.first == b.first && a.second == b.second;
    }

    template <typename T>
    bool operator!=(const unordered_pair<T>& a, const unordered_pair <T>& b)
    {
        return !(a == b);
    }

    template <typename T>
    bool operator<(const unordered_pair<T>& a, const unordered_pair<T>& b)
    {
        return (a.first < b.first || (!(b.first < a.first) && a.second < b.second));
    }

    template <typename T>
    bool operator>(const unordered_pair<T>& a, const unordered_pair <T>& b)
    {
        return b < a;
    }

    template <typename T>
    bool operator<=(const unordered_pair<T>& a, const unordered_pair <T>& b)
    {
        return !(b < a);
    }

    template <typename T>
    bool operator>=(const unordered_pair<T>& a, const unordered_pair <T>& b)
    {
        return !(a < b);
    }

    template <typename T>
    unordered_pair<T> make_unordered_pair (const T& a, const T& b)
    {
        return unordered_pair<T>(a,b);
    }

// ----------------------------------------------------------------------------------------

    template <typename T>
    void serialize (
        const unordered_pair<T>& item,
        std::ostream& out
    )
    {
        try
        { 
            serialize(item.first,out); 
            serialize(item.second,out); 
        }
        catch (serialization_error& e)
        { throw serialization_error(e.info + "\n   while serializing object of type unordered_pair"); }
    }

    template <typename T>
    void deserialize (
        unordered_pair<T>& item,
        std::istream& in 
    )
    {
        try
        { 
            T a, b;
            deserialize(a,in); 
            deserialize(b,in); 
            item = make_unordered_pair(a,b);
        }
        catch (serialization_error& e)
        { throw serialization_error(e.info + "\n   while deserializing object of type unordered_pair"); }
    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_UNORDERED_PAiR_Hh_