aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/AggregateFunctions/QuantileTiming.h
blob: 45fbf38258fe41d1b0e4a27421b311346118e006 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#pragma once

#include <IO/ReadBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
#include <Common/HashTable/Hash.h>
#include <Common/PODArray.h>
#include <base/sort.h>


namespace DB
{
struct Settings;

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
    extern const int INCORRECT_DATA;
}

/** Calculates quantile for time in milliseconds, less than 30 seconds.
  * If the value is greater than 30 seconds, the value is set to 30 seconds.
  *
  * If total values is not greater than about 5670, then the calculation is accurate.
  *
  * Otherwise
  *  If time less that 1024 ms, than calculation is accurate.
  *  Otherwise, the computation is rounded to a multiple of 16 ms.
  *
  * Three different data structures are used:
  * - flat array (of all met values) of fixed length, allocated inplace, size 64 bytes; Stores 0..31 values;
  * - flat array (of all values encountered), allocated separately, increasing length;
  * - a histogram (that is, value -> number), consisting of two parts
  * -- for values from 0 to 1023 - in increments of 1;
  * -- for values from 1024 to 30,000 - in increments of 16;
  *
  * NOTE: 64-bit integer weight can overflow, see also QuantileExactWeighted.h::get()
  */

#define TINY_MAX_ELEMS 31
#define BIG_THRESHOLD 30000

namespace detail
{
    /** Helper structure for optimization in the case of a small number of values
      * - flat array of a fixed size "on the stack" in which all encountered values placed in succession.
      * Size - 64 bytes. Must be a POD-type (used in union).
      */
    struct QuantileTimingTiny
    {
        mutable UInt16 elems[TINY_MAX_ELEMS];    /// mutable because array sorting is not considered a state change.
        /// It's important that `count` be at the end of the structure, since the beginning of the structure will be subsequently rewritten by other objects.
        /// You must initialize it by zero itself.
        /// Why? `count` field is reused even in cases where the union contains other structures
        ///  (the size of which falls short of this field.)
        UInt16 count;

        /// Can only be used while `count < TINY_MAX_ELEMS`.
        void insert(UInt64 x)
        {
            if (unlikely(x > BIG_THRESHOLD))
                x = BIG_THRESHOLD;

            elems[count] = x;
            ++count;
        }

        /// Can only be used while `count + rhs.count <= TINY_MAX_ELEMS`.
        void merge(const QuantileTimingTiny & rhs)
        {
            for (size_t i = 0; i < rhs.count; ++i)
            {
                elems[count] = rhs.elems[i];
                ++count;
            }
        }

        void serialize(WriteBuffer & buf) const
        {
            writeBinaryLittleEndian(count, buf);
            buf.write(reinterpret_cast<const char *>(elems), count * sizeof(elems[0]));
        }

        void deserialize(ReadBuffer & buf)
        {
            UInt16 new_count = 0;
            readBinaryLittleEndian(new_count, buf);
            if (new_count > TINY_MAX_ELEMS)
                throw Exception(ErrorCodes::INCORRECT_DATA, "The number of elements {} for the 'tiny' kind of quantileTiming is exceeding the maximum of {}", new_count, TINY_MAX_ELEMS);
            buf.readStrict(reinterpret_cast<char *>(elems), new_count * sizeof(elems[0]));
            count = new_count;
        }

        /** This function must be called before get-functions. */
        void prepare() const
        {
            ::sort(elems, elems + count);
        }

        UInt16 get(double level) const
        {
            return level != 1
                ? elems[static_cast<size_t>(count * level)]
                : elems[count - 1];
        }

        template <typename ResultType>
        void getMany(const double * levels, size_t size, ResultType * result) const
        {
            const double * levels_end = levels + size;

            while (levels != levels_end)
            {
                *result = get(*levels);
                ++levels;
                ++result;
            }
        }

        /// The same, but in the case of an empty state NaN is returned.
        float getFloat(double level) const
        {
            return count
                ? get(level)
                : std::numeric_limits<float>::quiet_NaN();
        }

        void getManyFloat(const double * levels, size_t size, float * result) const
        {
            if (count)
                getMany(levels, size, result);
            else
                for (size_t i = 0; i < size; ++i)
                    result[i] = std::numeric_limits<float>::quiet_NaN();
        }
    };


    /** Auxiliary structure for optimization in case of average number of values
      *  - a flat array, allocated separately, into which all found values are put in succession.
      */
    struct QuantileTimingMedium
    {
        /// sizeof - 24 bytes.
        using Array = PODArray<UInt16, 128>;
        mutable Array elems;    /// mutable because array sorting is not considered a state change.

        QuantileTimingMedium() = default;
        QuantileTimingMedium(const UInt16 * begin, const UInt16 * end) : elems(begin, end) {}

        void insert(UInt64 x)
        {
            if (unlikely(x > BIG_THRESHOLD))
                x = BIG_THRESHOLD;

            elems.emplace_back(x);
        }

        void merge(const QuantileTimingMedium & rhs)
        {
            elems.insert(rhs.elems.begin(), rhs.elems.end());
        }

        void serialize(WriteBuffer & buf) const
        {
            writeBinaryLittleEndian(elems.size(), buf);
            buf.write(reinterpret_cast<const char *>(elems.data()), elems.size() * sizeof(elems[0]));
        }

        void deserialize(ReadBuffer & buf)
        {
            size_t size = 0;
            readBinaryLittleEndian(size, buf);
            if (size > 10'000)
                throw Exception(ErrorCodes::INCORRECT_DATA, "The number of elements {} for the 'medium' kind of quantileTiming is too large", size);

            elems.resize(size);
            buf.readStrict(reinterpret_cast<char *>(elems.data()), size * sizeof(elems[0]));
        }

        UInt16 get(double level)
        {
            UInt16 quantile = 0;

            if (!elems.empty())
            {
                size_t n = level < 1
                    ? static_cast<size_t>(level * elems.size())
                    : (elems.size() - 1);

                /// Sorting an array will not be considered a violation of constancy.
                auto & array = elems;
                ::nth_element(array.begin(), array.begin() + n, array.end());
                quantile = array[n];
            }

            return quantile;
        }

        template <typename ResultType>
        void getMany(const double * levels, const size_t * levels_permutation, size_t size, ResultType * result)
        {
            size_t prev_n = 0;
            auto & array = elems;
            for (size_t i = 0; i < size; ++i)
            {
                auto level_index = levels_permutation[i];
                auto level = levels[level_index];

                size_t n = level < 1
                    ? static_cast<size_t>(level * elems.size())
                    : (elems.size() - 1);

                ::nth_element(array.begin() + prev_n, array.begin() + n, array.end());

                result[level_index] = array[n];
                prev_n = n;
            }
        }

        /// Same, but in the case of an empty state, NaN is returned.
        float getFloat(double level)
        {
            return !elems.empty()
                ? get(level)
                : std::numeric_limits<float>::quiet_NaN();
        }

        void getManyFloat(const double * levels, const size_t * levels_permutation, size_t size, float * result)
        {
            if (!elems.empty())
                getMany(levels, levels_permutation, size, result);
            else
                for (size_t i = 0; i < size; ++i)
                    result[i] = std::numeric_limits<float>::quiet_NaN();
        }
    };


    #define SMALL_THRESHOLD 1024
    #define BIG_SIZE ((BIG_THRESHOLD - SMALL_THRESHOLD) / BIG_PRECISION)
    #define BIG_PRECISION 16

    #define SIZE_OF_LARGE_WITHOUT_COUNT ((SMALL_THRESHOLD + BIG_SIZE) * sizeof(UInt64))


    /** For a large number of values. The size is about 22 680 bytes.
      */
    class QuantileTimingLarge
    {
    private:
        /// Total number of values.
        UInt64 count;
        /// Use of UInt64 is very wasteful.
        /// But UInt32 is definitely not enough, and it's too hard to invent 6-byte values.

        /// Number of values for each value is smaller than `small_threshold`.
        UInt64 count_small[SMALL_THRESHOLD];

        /// The number of values for each value from `small_threshold` to `big_threshold`, rounded to `big_precision`.
        UInt64 count_big[BIG_SIZE];

        /// Get value of quantile by index in array `count_big`.
        static inline UInt16 indexInBigToValue(size_t i)
        {
            return (i * BIG_PRECISION) + SMALL_THRESHOLD
                + (intHash32<0>(i) % BIG_PRECISION - (BIG_PRECISION / 2));    /// A small randomization so that it is not noticeable that all the values are even.
        }

        /// Lets you scroll through the histogram values, skipping zeros.
        class Iterator
        {
        private:
            const UInt64 * begin;
            const UInt64 * pos;
            const UInt64 * end;

            void adjust()
            {
                while (isValid() && 0 == *pos)
                    ++pos;
            }

        public:
            explicit Iterator(const QuantileTimingLarge & parent)
                : begin(parent.count_small), pos(begin), end(&parent.count_big[BIG_SIZE])
            {
                adjust();
            }

            bool isValid() const { return pos < end; }

            void next()
            {
                ++pos;
                adjust();
            }

            UInt64 count() const { return *pos; }

            UInt16 key() const
            {
                return pos - begin < SMALL_THRESHOLD
                    ? pos - begin
                    : indexInBigToValue(pos - begin - SMALL_THRESHOLD);
            }
        };

    public:
        QuantileTimingLarge()
        {
            memset(this, 0, sizeof(*this));
        }

        void insert(UInt64 x) noexcept
        {
            insertWeighted(x, 1);
        }

        void insertWeighted(UInt64 x, size_t weight) noexcept
        {
            count += weight;

            if (x < SMALL_THRESHOLD)
                count_small[x] += weight;
            else if (x < BIG_THRESHOLD)
                count_big[(x - SMALL_THRESHOLD) / BIG_PRECISION] += weight;
        }

        void merge(const QuantileTimingLarge & rhs) noexcept
        {
            count += rhs.count;

            for (size_t i = 0; i < SMALL_THRESHOLD; ++i)
                count_small[i] += rhs.count_small[i];

            for (size_t i = 0; i < BIG_SIZE; ++i)
                count_big[i] += rhs.count_big[i];
        }

        void serialize(WriteBuffer & buf) const
        {
            writeBinaryLittleEndian(count, buf);

            if (count * 2 > SMALL_THRESHOLD + BIG_SIZE)
            {
                /// Simple serialization for a heavily dense case.
                buf.write(reinterpret_cast<const char *>(this) + sizeof(count), SIZE_OF_LARGE_WITHOUT_COUNT);
            }
            else
            {
                /// More compact serialization for a sparse case.

                for (size_t i = 0; i < SMALL_THRESHOLD; ++i)
                {
                    if (count_small[i])
                    {
                        writeBinaryLittleEndian(UInt16(i), buf);
                        writeBinaryLittleEndian(count_small[i], buf);
                    }
                }

                for (size_t i = 0; i < BIG_SIZE; ++i)
                {
                    if (count_big[i])
                    {
                        writeBinaryLittleEndian(UInt16(i + SMALL_THRESHOLD), buf);
                        writeBinaryLittleEndian(count_big[i], buf);
                    }
                }

                /// Symbolizes end of data.
                writeBinaryLittleEndian(UInt16(BIG_THRESHOLD), buf);
            }
        }

        void deserialize(ReadBuffer & buf)
        {
            readBinaryLittleEndian(count, buf);

            if (count * 2 > SMALL_THRESHOLD + BIG_SIZE)
            {
                buf.readStrict(reinterpret_cast<char *>(this) + sizeof(count), SIZE_OF_LARGE_WITHOUT_COUNT);
            }
            else
            {
                while (true)
                {
                    UInt16 index = 0;
                    readBinaryLittleEndian(index, buf);
                    if (index == BIG_THRESHOLD)
                        break;

                    UInt64 elem_count = 0;
                    readBinaryLittleEndian(elem_count, buf);

                    if (index < SMALL_THRESHOLD)
                        count_small[index] = elem_count;
                    else
                        count_big[index - SMALL_THRESHOLD] = elem_count;
                }
            }
        }


        /// Get the value of the `level` quantile. The level must be between 0 and 1.
        UInt16 get(double level) const
        {
            double pos = std::ceil(count * level);

            double accumulated = 0;
            Iterator it(*this);

            while (it.isValid())
            {
                accumulated += it.count();

                if (accumulated >= pos)
                    break;

                it.next();
            }

            return it.isValid() ? it.key() : BIG_THRESHOLD;
        }

        /// Get the `size` values of `levels` quantiles. Write `size` results starting with `result` address.
        /// indices - an array of index levels such that the corresponding elements will go in ascending order.
        template <typename ResultType>
        void getMany(const double * levels, const size_t * indices, size_t size, ResultType * result) const
        {
            const auto * indices_end = indices + size;
            const auto * index = indices;

            double pos = std::ceil(count * levels[*index]);

            double accumulated = 0;
            Iterator it(*this);

            while (it.isValid())
            {
                accumulated += it.count();

                while (accumulated >= pos)
                {
                    result[*index] = it.key();
                    ++index;

                    if (index == indices_end)
                        return;

                    pos = std::ceil(count * levels[*index]);
                }

                it.next();
            }

            while (index != indices_end)
            {
                result[*index] = std::numeric_limits<ResultType>::max() < BIG_THRESHOLD
                    ? std::numeric_limits<ResultType>::max() : BIG_THRESHOLD;
                ++index;
            }
        }

        /// The same, but in the case of an empty state, NaN is returned.
        float getFloat(double level) const
        {
            return count
                ? get(level)
                : std::numeric_limits<float>::quiet_NaN();
        }

        void getManyFloat(const double * levels, const size_t * levels_permutation, size_t size, float * result) const
        {
            if (count)
                getMany(levels, levels_permutation, size, result);
            else
                for (size_t i = 0; i < size; ++i)
                    result[i] = std::numeric_limits<float>::quiet_NaN();
        }
    };
}


/** sizeof - 64 bytes.
  * If there are not enough of them - allocates up to 20 KB of memory in addition.
  */
template <typename>     /// Unused template parameter is for AggregateFunctionQuantile.
class QuantileTiming : private boost::noncopyable
{
private:
    union
    {
        detail::QuantileTimingTiny tiny;
        detail::QuantileTimingMedium medium;
        detail::QuantileTimingLarge * large;
    };

    enum class Kind : uint8_t
    {
        Tiny = 1,
        Medium = 2,
        Large = 3
    };

    Kind which() const
    {
        if (tiny.count <= TINY_MAX_ELEMS)
            return Kind::Tiny;
        if (tiny.count == TINY_MAX_ELEMS + 1)
            return Kind::Medium;
        return Kind::Large;
    }

    void tinyToMedium()
    {
        detail::QuantileTimingTiny tiny_copy = tiny;
        new (&medium) detail::QuantileTimingMedium(tiny_copy.elems, tiny_copy.elems + tiny_copy.count);
        tiny.count = TINY_MAX_ELEMS + 1;
    }

    void mediumToLarge()
    {
        /// While the data is copied from medium, it is not possible to set `large` value (otherwise it will overwrite some data).
        detail::QuantileTimingLarge * tmp_large = new detail::QuantileTimingLarge;

        for (const auto & elem : medium.elems)
            tmp_large->insert(elem);    /// Cannot throw, so don't worry about new.

        medium.~QuantileTimingMedium();
        large = tmp_large;
        tiny.count = TINY_MAX_ELEMS + 2;    /// large will be deleted in destructor.
    }

    void tinyToLarge()
    {
        /// While the data is copied from `medium` it is not possible to set `large` value (otherwise it will overwrite some data).
        detail::QuantileTimingLarge * tmp_large = new detail::QuantileTimingLarge;

        for (size_t i = 0; i < tiny.count; ++i)
            tmp_large->insert(tiny.elems[i]);    /// Cannot throw, so don't worry about new.

        large = tmp_large;
        tiny.count = TINY_MAX_ELEMS + 2;    /// large will be deleted in destructor.
    }

    bool mediumIsWorthToConvertToLarge() const
    {
        return medium.elems.size() >= sizeof(detail::QuantileTimingLarge) / sizeof(medium.elems[0]) / 2;
    }

public:
    QuantileTiming()
    {
        tiny.count = 0;
    }

    ~QuantileTiming()
    {
        Kind kind = which();

        if (kind == Kind::Medium)
        {
            medium.~QuantileTimingMedium();
        }
        else if (kind == Kind::Large)
        {
            delete large;
        }
    }

    template <typename T>
    void add(T x_)
    {
        UInt64 x = static_cast<UInt64>(x_);

        if (tiny.count < TINY_MAX_ELEMS)
        {
            tiny.insert(x);
        }
        else
        {
            if (unlikely(tiny.count == TINY_MAX_ELEMS))
                tinyToMedium();

            if (which() == Kind::Medium)
            {
                if (unlikely(mediumIsWorthToConvertToLarge()))
                {
                    mediumToLarge();
                    large->insert(x);
                }
                else
                    medium.insert(x);
            }
            else
                large->insert(x);
        }
    }

    template <typename T>
    void add(T x_, size_t weight)
    {
        UInt64 x = static_cast<UInt64>(x_);

        /// NOTE: First condition is to avoid overflow.
        if (weight < TINY_MAX_ELEMS && tiny.count + weight <= TINY_MAX_ELEMS)
        {
            for (size_t i = 0; i < weight; ++i)
                tiny.insert(x);
        }
        else
        {
            if (unlikely(tiny.count <= TINY_MAX_ELEMS))
                tinyToLarge();    /// For the weighted variant we do not use `medium` - presumably, it is impractical.

            large->insertWeighted(x, weight);
        }
    }

    /// NOTE Too complicated.
    void merge(const QuantileTiming & rhs)
    {
        if (tiny.count + rhs.tiny.count <= TINY_MAX_ELEMS)
        {
            tiny.merge(rhs.tiny);
        }
        else
        {
            auto kind = which();
            auto rhs_kind = rhs.which();

            /// If one with which we merge has a larger data structure, then we bring the current structure to the same one.
            if (kind == Kind::Tiny && rhs_kind == Kind::Medium)
            {
                tinyToMedium();
                kind = Kind::Medium;
            }
            else if (kind == Kind::Tiny && rhs_kind == Kind::Large)
            {
                tinyToLarge();
                kind = Kind::Large;
            }
            else if (kind == Kind::Medium && rhs_kind == Kind::Large)
            {
                mediumToLarge();
                kind = Kind::Large;
            }
            /// Case when two states are small, but when merged, they will turn into average.
            else if (kind == Kind::Tiny && rhs_kind == Kind::Tiny)
            {
                tinyToMedium();
                kind = Kind::Medium;
            }

            if (kind == Kind::Medium && rhs_kind == Kind::Medium)
            {
                medium.merge(rhs.medium);
            }
            else if (kind == Kind::Large && rhs_kind == Kind::Large)
            {
                large->merge(*rhs.large);
            }
            else if (kind == Kind::Medium && rhs_kind == Kind::Tiny)
            {
                medium.elems.insert(rhs.tiny.elems, rhs.tiny.elems + rhs.tiny.count);
            }
            else if (kind == Kind::Large && rhs_kind == Kind::Tiny)
            {
                for (size_t i = 0; i < rhs.tiny.count; ++i)
                    large->insert(rhs.tiny.elems[i]);
            }
            else if (kind == Kind::Large && rhs_kind == Kind::Medium)
            {
                for (const auto & elem : rhs.medium.elems)
                    large->insert(elem);
            }
            else
                throw Exception(ErrorCodes::LOGICAL_ERROR, "Logical error in QuantileTiming::merge function: not all cases are covered");

            /// For determinism, we should always convert to `large` when size condition is reached
            ///  - regardless of merge order.
            if (kind == Kind::Medium && unlikely(mediumIsWorthToConvertToLarge()))
            {
                mediumToLarge();
            }
        }
    }

    void serialize(WriteBuffer & buf) const
    {
        auto kind = which();
        writeBinaryLittleEndian(kind, buf);

        if (kind == Kind::Tiny)
            tiny.serialize(buf);
        else if (kind == Kind::Medium)
            medium.serialize(buf);
        else
            large->serialize(buf);
    }

    /// Called for an empty object.
    void deserialize(ReadBuffer & buf)
    {
        Kind kind;
        readBinaryLittleEndian(kind, buf);

        if (kind == Kind::Tiny)
        {
            tiny.deserialize(buf);
        }
        else if (kind == Kind::Medium)
        {
            tinyToMedium();
            medium.deserialize(buf);
        }
        else if (kind == Kind::Large)
        {
            tinyToLarge();
            large->deserialize(buf);
        }
        else
            throw Exception(ErrorCodes::INCORRECT_DATA, "Incorrect kind of QuantileTiming");
    }

    /// Get the value of the `level` quantile. The level must be between 0 and 1.
    UInt16 get(double level)
    {
        Kind kind = which();

        if (kind == Kind::Tiny)
        {
            tiny.prepare();
            return tiny.get(level);
        }
        else if (kind == Kind::Medium)
        {
            return medium.get(level);
        }
        else
        {
            return large->get(level);
        }
    }

    /// Get the size values of the quantiles of the `levels` levels. Record `size` results starting with `result` address.
    template <typename ResultType>
    void getMany(const double * levels, const size_t * levels_permutation, size_t size, ResultType * result)
    {
        Kind kind = which();

        if (kind == Kind::Tiny)
        {
            tiny.prepare();
            tiny.getMany(levels, size, result);
        }
        else if (kind == Kind::Medium)
        {
            medium.getMany(levels, levels_permutation, size, result);
        }
        else /*if (kind == Kind::Large)*/
        {
            large->getMany(levels, levels_permutation, size, result);
        }
    }

    /// The same, but in the case of an empty state, NaN is returned.
    float getFloat(double level)
    {
        return tiny.count
            ? get(level)
            : std::numeric_limits<float>::quiet_NaN();
    }

    void getManyFloat(const double * levels, const size_t * levels_permutation, size_t size, float * result)
    {
        if (tiny.count)
            getMany(levels, levels_permutation, size, result);
        else
            for (size_t i = 0; i < size; ++i)
                result[i] = std::numeric_limits<float>::quiet_NaN();
    }

    friend void writeBinary(const Kind & x, WriteBuffer & buf)
    {
        writePODBinary(x, buf);
    }

    friend void readBinary(Kind & x, ReadBuffer & buf)
    {
        readPODBinary(x, buf);
    }
};

#undef SMALL_THRESHOLD
#undef BIG_THRESHOLD
#undef BIG_SIZE
#undef BIG_PRECISION
#undef TINY_MAX_ELEMS

}