aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/cookies/lctable.h
blob: 09c88eafb8069e6acad15eb24321c25e43118a58 (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
#pragma once

#include <library/cpp/digest/lower_case/lchash.h>

#include <util/generic/hash_multi_map.h>
#include <util/generic/strbuf.h>
#include <util/generic/algorithm.h>
#include <util/generic/singleton.h>

struct TStrBufHash {
    inline size_t operator()(const TStringBuf& s) const noexcept {
        return FnvCaseLess<size_t>(s);
    }
};

struct TStrBufEqualToCaseLess {
    inline bool operator()(const TStringBuf& c1, const TStringBuf& c2) const noexcept {
        typedef TLowerCaseIterator<const TStringBuf::TChar> TIter;

        return (c1.size() == c2.size()) && std::equal(TIter(c1.begin()), TIter(c1.end()), TIter(c2.begin()));
    }
};

template <class T>
class TLowerCaseTable: private THashMultiMap<TStringBuf, T, TStrBufHash, TStrBufEqualToCaseLess> {
    typedef THashMultiMap<TStringBuf, T, TStrBufHash, TStrBufEqualToCaseLess> TBase;

public:
    typedef typename TBase::const_iterator const_iterator;
    typedef std::pair<const_iterator, const_iterator> TConstIteratorPair;

    using TBase::TBase;
    using TBase::begin;
    using TBase::end;

    inline TConstIteratorPair EqualRange(const TStringBuf& name) const {
        return TBase::equal_range(name);
    }

    inline const T& Get(const TStringBuf& name, size_t numOfValue = 0) const {
        TConstIteratorPair range = EqualRange(name);

        if (range.first == TBase::end())
            return Default<T>();

        if (numOfValue == 0)
            return range.first->second;

        const_iterator next = range.first;
        for (size_t c = 0; c < numOfValue; ++c) {
            ++next;
            if (next == range.second)
                return Default<T>();
        }

        return next->second;
    }

    inline bool Has(const TStringBuf& name) const {
        return TBase::find(name) != TBase::end();
    }

    size_t NumOfValues(const TStringBuf& name) const {
        return TBase::count(name);
    }

    inline size_t Size() const noexcept {
        return TBase::size();
    }

    inline bool Empty() const noexcept {
        return TBase::empty();
    }

    inline void Add(const TStringBuf& key, const T& val) {
        TBase::insert(typename TBase::value_type(key, val));
    }

    inline void Clear() noexcept {
        TBase::clear();
    }

    inline size_t Erase(const TStringBuf& key) {
        return TBase::erase(key);
    }
};