summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/sort.h
blob: d5c73df9b96c4ee4cc0f8a66c9bca67c221c957e (plain) (blame)
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
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
221
222
223
224
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/objects/nbo.h>
#include <functional>
#include <limits>
#include <algorithm>

namespace vespalib {

template<typename T, bool asc=true>
class convertForSort
{
};

template<>
class convertForSort<float, true>
{
public:
    typedef float InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(float value)
    {
        union { float f; UIntType u; } val;
        val.f=value;
        return (static_cast<IntType>(val.u) >= 0)
                ? (val.u ^ (UIntType(std::numeric_limits<IntType>::max()) + 1))
                : (val.u ^ std::numeric_limits<UIntType>::max());
    }
};

template<>
class convertForSort<float, false>
{
public:
    typedef float InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(float value)
    {
        union { float f; UIntType u; } val;
        val.f=value;
        return (static_cast<IntType>(val.u) >= 0)
                ? (val.u ^ std::numeric_limits<IntType>::max())
                : val.u;
    }
};


template<>
class convertForSort<double, true>
{
public:
    typedef double InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(double value)
    {
        union { double f; UIntType u; } val;
        val.f=value;
        return (static_cast<IntType>(val.u) >= 0)
                ? (val.u ^ (UIntType(std::numeric_limits<IntType>::max()) + 1))
                : (val.u ^ std::numeric_limits<UIntType>::max());
    }
};

template<>
class convertForSort<double, false>
{
public:
    typedef double InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(double value)
    {
        union { double f; UIntType u; } val;
        val.f=value;
        return (static_cast<IntType>(val.u) >= 0)
                ? (val.u ^ std::numeric_limits<IntType>::max())
                : val.u;
    }
};

template<>
class convertForSort<uint8_t, true>
{
public:
    typedef uint8_t InputType;
    typedef int8_t  IntType;
    typedef uint8_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(UIntType value)   { return value; }
};

template<>
class convertForSort<uint8_t, false>
{
public:
    typedef uint8_t InputType;
    typedef int8_t  IntType;
    typedef uint8_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(UIntType value)  { return ~value; }
};
template<>
class convertForSort<uint16_t, true>
{
public:
    typedef uint16_t InputType;
    typedef int16_t  IntType;
    typedef uint16_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(UIntType value)  { return value; }
};
template<>
class convertForSort<uint16_t, false>
{
public:
    typedef uint16_t InputType;
    typedef int16_t  IntType;
    typedef uint16_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(UIntType value) { return ~value; }
};
template<>
class convertForSort<uint32_t, true>
{
public:
    typedef uint32_t InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(UIntType value)  { return value; }
};
template<>
class convertForSort<uint32_t, false>
{
public:
    typedef uint32_t InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(UIntType value) { return ~value; }
};
template<>
class convertForSort<uint64_t, true>
{
public:
    typedef uint64_t InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(UIntType value)  { return value; }
};
template<>
class convertForSort<uint64_t, false>
{
public:
    typedef uint64_t InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(UIntType value) { return ~value; }
};

template<>
class convertForSort<int8_t, true>
{
public:
    typedef int8_t  InputType;
    typedef int8_t  IntType;
    typedef uint8_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(IntType value)   { return value ^ (std::numeric_limits<IntType>::max() + 1); }
};
template<>
class convertForSort<int8_t, false>
{
public:
    typedef int8_t  InputType;
    typedef int8_t  IntType;
    typedef uint8_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(IntType value)  { return value ^ std::numeric_limits<IntType>::max(); }
};
template<>
class convertForSort<int16_t, true>
{
public:
    typedef int16_t  InputType;
    typedef int16_t  IntType;
    typedef uint16_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(IntType value)  { return value ^ (std::numeric_limits<IntType>::max() + 1); }
};
template<>
class convertForSort<int16_t, false>
{
public:
    typedef int16_t  InputType;
    typedef int16_t  IntType;
    typedef uint16_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(IntType value) { return value ^ std::numeric_limits<IntType>::max(); }
};
template<>
class convertForSort<int32_t, true>
{
public:
    typedef int32_t  InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(IntType value)  { return value ^ (UIntType(std::numeric_limits<IntType>::max()) + 1); }
};
template<>
class convertForSort<int32_t, false>
{
public:
    typedef int32_t  InputType;
    typedef int32_t  IntType;
    typedef uint32_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(IntType value) { return value ^ std::numeric_limits<IntType>::max(); }
};
template<>
class convertForSort<int64_t, true>
{
public:
    typedef int64_t  InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::less<InputType> Compare;
    static inline UIntType convert(IntType value)  { return value ^ (UIntType(std::numeric_limits<IntType>::max()) + 1); }
};
template<>
class convertForSort<int64_t, false>
{
public:
    typedef int64_t  InputType;
    typedef int64_t  IntType;
    typedef uint64_t UIntType;
    typedef std::greater<InputType> Compare;
    static inline UIntType convert(IntType value) { return value ^ std::numeric_limits<IntType>::max(); }
};

template<typename C>
uint32_t serializeForSort(typename C::InputType v, void * dst) {
    typename C::UIntType nbo(vespalib::nbo::n2h(C::convert(v)));
    memcpy(dst, &nbo, sizeof(nbo));
    return sizeof(nbo);
}

} // namespace vespalib