summaryrefslogtreecommitdiff
path: root/include/svl/whichranges.hxx
blob: 53413d8c4c9eb830ba5779e2478807f305bb5d9f (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
177
178
179
180
181
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#pragma once

#include <sal/config.h>
#include <sal/types.h>
#include <svl/svldllapi.h>
#include <array>
#include <memory>
#include <cassert>

typedef std::pair<sal_uInt16, sal_uInt16> WhichPair;

namespace svl
{
namespace detail
{
constexpr bool validRange(sal_uInt16 wid1, sal_uInt16 wid2) { return wid1 != 0 && wid1 <= wid2; }

constexpr bool validGap(sal_uInt16 wid1, sal_uInt16 wid2) { return wid2 > wid1; }

template <sal_uInt16 WID1, sal_uInt16 WID2> constexpr bool validRanges()
{
    return validRange(WID1, WID2);
}

template <sal_uInt16 WID1, sal_uInt16 WID2, sal_uInt16 WID3, sal_uInt16... WIDs>
constexpr bool validRanges()
{
    return validRange(WID1, WID2) && validGap(WID2, WID3) && validRanges<WID3, WIDs...>();
}

// The calculations in rangeSize cannot overflow, assuming
// std::size_t is no smaller than sal_uInt16:
constexpr std::size_t rangeSize(sal_uInt16 wid1, sal_uInt16 wid2)
{
    assert(validRange(wid1, wid2));
    return wid2 - wid1 + 1;
}
}

template <sal_uInt16... WIDs> struct Items_t
{
    using Array = std::array<WhichPair, sizeof...(WIDs) / 2>;
    template <sal_uInt16 WID1, sal_uInt16 WID2, sal_uInt16... Rest>
    static constexpr void fill(typename Array::iterator it)
    {
        it->first = WID1;
        it->second = WID2;
        if constexpr (sizeof...(Rest) > 0)
            fill<Rest...>(++it);
    }
    static constexpr Array make()
    {
        assert(svl::detail::validRanges<WIDs...>());
        Array a{};
        fill<WIDs...>(a.begin());
        return a;
    }
    // This is passed to WhichRangesContainer so we can avoid needing to malloc()
    // for compile-time data.
    static constexpr Array value = make();
};

template <sal_uInt16... WIDs> inline static constexpr auto Items = Items_t<WIDs...>{};
}

#define INVALID_WHICHPAIR_OFFSET (sal_uInt16(0xffff))

/**
 * Most of the time, the which ranges we point at are a compile-time literal.
 * So we take advantage of that, and avoid the cost of allocating our own array and copying into it.
 */
class SVL_DLLPUBLIC WhichRangesContainer
{
    using const_iterator = WhichPair const*;

    WhichPair const* m_pairs = nullptr;
    sal_Int32 m_size = 0;
    mutable sal_uInt16 m_TotalCount = 0;

    // variables for buffering the last used WhichPair to allow fast answers
    // in doesContainWhich
    mutable sal_uInt16 m_aLastWhichPairOffset = INVALID_WHICHPAIR_OFFSET;
    mutable sal_uInt16 m_aLastWhichPairFirst = 0;
    mutable sal_uInt16 m_aLastWhichPairSecond = 0;

    /** if true, we allocated and need to delete the pairs, if not, we are pointing
      * at a global const literal */
    bool m_bOwnRanges = false;

    /**
     * Determines the number of sal_uInt16s in a container of pairs of
     * sal_uInt16s, each representing a range of sal_uInt16s, and total capacity of the ranges.
     */
    void CountRanges() const;

public:
#if !defined NDEBUG
    inline bool validRanges2() const
    {
        for (sal_Int32 i = 0; i < size(); ++i)
        {
            auto p = (*this)[i];
            if (!svl::detail::validRange(p.first, p.second))
                return false;
            // ranges must be sorted
            if (i < size() - 1 && !svl::detail::validGap(p.second, (*this)[i + 1].first))
                return false;
        }
        return true;
    }
#endif

    WhichRangesContainer() = default;

    WhichRangesContainer(std::unique_ptr<WhichPair[]> wids, sal_Int32 nSize)
        : m_pairs(wids.release())
        , m_size(nSize)
        , m_TotalCount(0)
        , m_aLastWhichPairOffset(INVALID_WHICHPAIR_OFFSET)
        , m_aLastWhichPairFirst(0)
        , m_aLastWhichPairSecond(0)
        , m_bOwnRanges(true)
    {
        CountRanges();
    }
    template <sal_uInt16... WIDs>
    WhichRangesContainer(svl::Items_t<WIDs...>)
        : m_pairs(svl::Items_t<WIDs...>::value.data())
        , m_size(svl::Items_t<WIDs...>::value.size())
        , m_TotalCount(0)
        , m_aLastWhichPairOffset(INVALID_WHICHPAIR_OFFSET)
        , m_aLastWhichPairFirst(0)
        , m_aLastWhichPairSecond(0)
        , m_bOwnRanges(false)
    {
        CountRanges();
    }
    WhichRangesContainer(const WhichPair* wids, sal_Int32 nSize);
    WhichRangesContainer(sal_uInt16 nWhichStart, sal_uInt16 nWhichEnd);
    WhichRangesContainer(WhichRangesContainer const& other) { operator=(other); }
    WhichRangesContainer(WhichRangesContainer&& other);
    ~WhichRangesContainer();

    WhichRangesContainer& operator=(WhichRangesContainer&& other);
    WhichRangesContainer& operator=(WhichRangesContainer const& other);

    bool operator==(WhichRangesContainer const& other) const;

    const_iterator begin() const noexcept { return m_pairs; }
    const_iterator end() const noexcept { return begin() + size(); }
    bool empty() const noexcept { return m_size == 0; }
    sal_Int32 size() const noexcept { return m_size; }

    WhichPair const& operator[](sal_Int32 idx) const noexcept
    {
        assert(idx >= 0 && idx < size() && "index out of range");
        return m_pairs[idx];
    }

    void reset();

    // calculate and return if nWhich is member of one of
    // the local WhichRanges and such contained in the
    // defined range
    bool doesContainWhich(sal_uInt16 nWhich) const;

    // Adds a range to which ranges, keeping the ranges in valid state (sorted, non-overlapping)
    SAL_WARN_UNUSED_RESULT WhichRangesContainer MergeRange(sal_uInt16 nFrom, sal_uInt16 nTo) const;

    sal_uInt16 TotalCount() const { return m_TotalCount; }
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */