Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
RangeSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Containers/Array.h"
7#include "Math/Range.h"
8#include "Serialization/Archive.h"
9
10
11/**
12 * Template for range sets.
13 *
14 * @todo gmp: Implement more efficient storage of range sets
15 */
16template<typename ElementType> class TRangeSet
17{
18 typedef TRangeBound<ElementType> BoundsType;
19 typedef TRange<ElementType> RangeType;
20
21 /*~ Typedef used to pass/return small element types by value rather than const& */
22 typedef typename TCallTraits<ElementType>::ParamType ElementValueOrConstRef;
23
24public:
25
26 /** Default constructor. */
28
29 /** Destructor. */
30 ~TRangeSet() { }
31
32public:
33
34 /**
35 * Adds a range to the set.
36 *
37 * This method merges overlapping ranges into a single range (i.e. {[1, 5], [4, 6]} becomes [1, 6]).
38 * Adjacent ranges (i.e. {[1, 4), [4, 6)} are also merged.
39 *
40 * @param Range The range to add.
41 */
42 void Add(RangeType Range)
43 {
44 for (int32 Index = 0; Index < Ranges.Num(); ++Index)
45 {
46 const RangeType& Current = Ranges[Index];
47
49 {
53 );
54
56 }
57 }
58
60 }
61
62 /**
63 * Merges another range set into this set.
64 *
65 * @param Other The range set to merge.
66 */
67 void Merge(const TRangeSet& Other)
68 {
69 for (typename TArray<RangeType>::TConstIterator It(Other.Ranges); It; ++It)
70 {
71 Add(*It);
72 }
73 }
74
75 /**
76 * Removes a range from the set.
77 *
78 * Ranges that overlap with the removed range will be split.
79 *
80 * @param Range The range to remove.
81 *//*
82 void Remove(const RangeType& Range)
83 {
84 for (int32 Index = 0; Index < Ranges.Num(); ++Index)
85 {
86 const RangeType& Current = Ranges(Index);
87
88 if (Current.Overlaps(Range))
89 {
90 if (Current.GetLowerBound() < Range.GetLowerBound())
91 {
92 Ranges.Add(RangeType(Current.GetLowerBound(), Range.GetLowerBound()));
93 }
94
95 if (Current.GetUpperBound() > Range.GetUpperBound())
96 {
97 Ranges.Add(RangeType(Range.GetUpperBound(), Current.GetUpperBound()));
98 }
99
100 Ranges.RemoveAtSwap(Index--);
101 }
102 }
103 }*/
104
105 /** Removes all ranges from the set. */
106 void Empty()
107 {
108 Ranges.Empty();
109 }
110
111public:
112
113 /**
114 * Checks whether this set contains the specified element.
115 *
116 * @param Element The element to check.
117 * @return true if the element is in the set, false otherwise.
118 */
120 {
121 for (typename TArray<RangeType>::TConstIterator It(Ranges); It; ++It)
122 {
123 if (It->Contains(Element))
124 {
125 return true;
126 }
127 }
128
129 return false;
130 }
131
132 /**
133 * Checks whether this set contains the specified range.
134 *
135 * @param Range The range to check.
136 * @return true if the set contains the range, false otherwise.
137 */
138 bool Contains(const RangeType& Range) const
139 {
140 for (typename TArray<RangeType>::TConstIterator It(Ranges); It; ++It)
141 {
142 if (It->Contains(Range))
143 {
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 /**
152 * Gets the range set's lowest bound.
153 *
154 * @return Lowest bound.
155 * @see GetMaxBound, GetMinBoundValue, HasMinBound
156 */
158 {
160
161 if (Ranges.Num())
162 {
164 for (int32 i = 1; i < Ranges.Num(); i++)
165 {
167 }
168 }
169
170 return Result;
171 }
172
173 /**
174 * Gets the value of the lowest bound.
175 *
176 * Use HasMinBound() to ensure that this range set actually has a lowest bound.
177 *
178 * @return Bound value.
179 * @see GetMaxBoundValue, GetMinBound, HasMinBound
180 */
181 ElementType GetMinBoundValue() const
182 {
183 return GetMinBound().GetValue();
184 }
185
186 /**
187 * Gets the range set's uppermost bound.
188 *
189 * @return Uppermost bound.
190 * @see GetMaxBoundValue, GetMinBound, HasMaxBound
191 */
193 {
195
196 if (Ranges.Num())
197 {
199 for (int32 i = 1; i < Ranges.Num(); i++)
200 {
202 }
203 }
204
205 return Result;
206 }
207
208 /**
209 * Gets the value of the uppermost bound.
210 *
211 * Use HasMaxBound() to ensure that this range actually has an upper bound.
212 *
213 * @return Bound value.
214 * @see GetMaxBound, GetMinBoundValue, HasMaxBound
215 */
216 ElementType GetMaxBoundValue() const
217 {
218 return GetMaxBound().GetValue();
219 }
220
221
222 /**
223 * Returns a read-only collection of the ranges contained in this set.
224 *
225 * @param Allocator The array allocator to use.
226 * @param OutRanges Will contain the collection of ranges.
227 */
228 template<typename Allocator>
229 const void GetRanges(TArray<RangeType, Allocator>& OutRanges) const
230 {
232 }
233
234 /**
235 * Checks whether the range has a lowest bound.
236 *
237 * @return true if the range has a lowest bound, false otherwise.
238 * @see GetMinBound, GetMinBoundValue, HasMaxBound
239 */
240 bool HasMinBound() const
241 {
242 return GetMinBound().IsClosed();
243 }
244
245 /**
246 * Checks whether the range has an uppermost bound.
247 *
248 * @return true if the range has an uppermost bound, false otherwise.
249 * @see GetUpperBound, GetUpperBoundValue, HasMinBound
250 */
251 bool HasMaxBound() const
252 {
253 return GetMaxBound().IsClosed();
254 }
255
256 /**
257 * Checks whether this range set is empty.
258 *
259 * @return true if the range set is empty, false otherwise.
260 */
261 bool IsEmpty() const
262 {
263 return (Ranges.Num() == 0);
264 }
265
266 /**
267 * Checks whether this range set overlaps with the specified range.
268 *
269 * @param Range The range to check.
270 * @return true if this set overlaps with the range, false otherwise.
271 */
272 bool Overlaps(const RangeType& Range) const
273 {
274 for (typename TArray<RangeType>::TConstIterator It(Ranges); It; ++It)
275 {
276 if (It->Overlaps(Range))
277 {
278 return true;
279 }
280 }
281
282 return false;
283 }
284
285 /**
286 * Checks whether this range set overlaps with another.
287 *
288 * @param Other The other range set.
289 * @return true if the range sets overlap, false otherwise.
290 *
291 * @todo gmp: This could be optimized to O(n*logn) using a line sweep on a pre-sorted array of bounds.
292 */
293 bool Overlaps(const TRangeSet& Other) const
294 {
295 for (typename TArray<RangeType>::TConstIterator It(Other.Ranges); It; ++It)
296 {
297 if (Overlaps(*It))
298 {
299 return true;
300 }
301 }
302
303 return false;
304 }
305
306public:
307
308 /**
309 * Serializes the given range set from or into the specified archive.
310 *
311 * @param Ar The archive to serialize from or into.
312 * @param RangeSet The range set to serialize.
313 * @return The archive.
314 */
315 friend class FArchive& operator<<(class FArchive& Ar, TRangeSet& RangeSet)
316 {
317 return Ar << RangeSet.Ranges;
318 }
319
320private:
321
322 /** Holds the set of ranges. */
324};
bool HasMaxBound() const
Definition RangeSet.h:251
bool Contains(ElementValueOrConstRef Element) const
Definition RangeSet.h:119
BoundsType GetMaxBound() const
Definition RangeSet.h:192
TCallTraits< ElementType >::ParamType ElementValueOrConstRef
Definition RangeSet.h:22
BoundsType GetMinBound() const
Definition RangeSet.h:157
bool Contains(const RangeType &Range) const
Definition RangeSet.h:138
bool Overlaps(const RangeType &Range) const
Definition RangeSet.h:272
const void GetRanges(TArray< RangeType, Allocator > &OutRanges) const
Definition RangeSet.h:229
void Empty()
Definition RangeSet.h:106
TRangeBound< ElementType > BoundsType
Definition RangeSet.h:18
bool IsEmpty() const
Definition RangeSet.h:261
bool Overlaps(const TRangeSet &Other) const
Definition RangeSet.h:293
TRangeSet()
Definition RangeSet.h:27
ElementType GetMinBoundValue() const
Definition RangeSet.h:181
bool HasMinBound() const
Definition RangeSet.h:240
void Add(RangeType Range)
Definition RangeSet.h:42
TArray< RangeType > Ranges
Definition RangeSet.h:323
ElementType GetMaxBoundValue() const
Definition RangeSet.h:216
TRange< ElementType > RangeType
Definition RangeSet.h:19
~TRangeSet()
Definition RangeSet.h:30
void Merge(const TRangeSet &Other)
Definition RangeSet.h:67