1/*
2 * Copyright (c) 2015, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef UNICODE_SET
30#define UNICODE_SET
31
32#include "unicode_def.h"
33
34#include <boost/icl/interval_set.hpp>
35
36namespace ue2 {
37
38class CodePointSet {
39public:
40 typedef boost::icl::closed_interval<unichar> interval;
41 typedef boost::icl::interval_set<unichar, std::less, interval> implT;
42 typedef implT::const_iterator const_iterator;
43
44 CodePointSet(void) {}
45
46 explicit CodePointSet(const interval &st) : impl(st) {}
47
48 bool none(void) const {
49 return impl.empty();
50 }
51
52 void set(unichar c) {
53 assert(c <= MAX_UNICODE);
54 impl.insert(c);
55 }
56
57 void unset(unichar c) {
58 assert(c <= MAX_UNICODE);
59 impl.subtract(c);
60 }
61
62 void setRange(unichar from, unichar to) { /* inclusive */
63 assert(from <= to);
64 assert(to <= MAX_UNICODE);
65 impl.insert(interval(from, to));
66 }
67
68 void unsetRange(unichar from, unichar to) { /* inclusive */
69 assert(from <= to);
70 assert(to <= MAX_UNICODE);
71 impl.subtract(interval(from, to));
72 }
73
74 void flip(void) {
75 impl = implT(interval(0, MAX_UNICODE)) - impl;
76 }
77
78 void operator|=(const CodePointSet &a) {
79 impl += a.impl;
80 }
81
82 const_iterator begin(void) const {
83 return impl.begin();
84 }
85
86 const_iterator end(void) const {
87 return impl.end();
88 }
89
90 size_t count(void) const {
91 return cardinality(impl);
92 }
93
94 CodePointSet operator~(void) const {
95 CodePointSet rv = *this;
96 rv.flip();
97 return rv;
98 }
99
100 bool operator==(const CodePointSet &a) const {
101 return is_element_equal(impl, a.impl);
102 }
103
104 bool operator!=(const CodePointSet &a) const {
105 return !is_element_equal(impl, a.impl);
106 }
107
108 bool isSubset(const CodePointSet &a) const {
109 // Check that adding an interval set has no effect
110 return ((impl + a.impl) == impl);
111 }
112
113 void operator-=(const CodePointSet &a) {
114 impl -= a.impl;
115 }
116
117 /* finds the nth set codepoint, returns INVALID_UNICODE on failure */
118 unichar at(size_t pos) const {
119 for (const_iterator i = begin(), e = end(); i != e; ++i) {
120 size_t int_count = cardinality(*i);
121 if (int_count <= pos) {
122 /* not in this interval, check next */
123 pos -= int_count;
124 continue;
125 } else {
126 return lower(*i) + pos;
127 }
128 }
129
130 return INVALID_UNICODE;
131 }
132
133 void swap(CodePointSet &other) { impl.swap(other.impl); }
134
135private:
136 implT impl;
137};
138
139} // namespace ue2
140
141#endif
142