aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/compact_set.h
blob: 910eef88eb07816afe332ab0a26f02b01fdd322c (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
//===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the TCompactSet class.
//
//===----------------------------------------------------------------------===//

#pragma once

#include "compact_vector.h"

#include <util/system/yassert.h>

#include <cstddef>
#include <iterator>
#include <set>
#include <type_traits>

namespace NYT {

/// TCompactSet - This maintains a set of unique values, optimizing for the case
/// when the set is small (less than N).  In this case, the set can be
/// maintained with no mallocs.  If the set gets large, we expand to using an
/// std::set to maintain reasonable lookup times.
///
/// Note that any modification of the set may invalidate *all* iterators.
template <typename T, unsigned N, typename C = std::less<T>>
class TCompactSet
{
private:
    /// Use a CompactVector to hold the elements here (even though it will never
    /// reach its 'large' stage) to avoid calling the default ctors of elements
    /// we will never use.
    TCompactVector<T, N> Vector;
    std::set<T, C> Set;

    using TSetConstIterator = typename std::set<T, C>::const_iterator;
    using TVectorConstIterator = typename TCompactVector<T, N>::const_iterator;

public:
    class const_iterator;
    using size_type = std::size_t;
    using key_type = T;

    TCompactSet() {}

    [[nodiscard]] bool empty() const;

    size_type size() const;

    const T& front() const;

    /// count - Return true if the element is in the set.
    size_type count(const T& v) const;

    /// contains - Return true if the element is in the set.
    bool contains(const T& v) const;

    /// insert - Insert an element into the set if it isn't already there.
    std::pair<const_iterator, bool> insert(const T& v);

    template <typename TIter>
    void insert(TIter i, TIter e);

    bool erase(const T& v);

    void clear();

    const_iterator begin() const;
    const_iterator cbegin() const;

    const_iterator end() const;
    const_iterator cend() const;

private:
    bool IsSmall() const;
};

} // namespace NYT

#define COMPACT_SET_INL_H_
#include "compact_set-inl.h"
#undef COMPACT_SET_INL_H_