TNB Library
TnbRange.h
[詳解]
1#pragma once
13#include "TnbSerializer.h"
14#include "TnbComparable.h"
15
16
17
18//T-TestCaseコードカバレッジDisable
19#pragma comment(user,"T-Coverage Disable")
20
21
22
23//TNB Library
24namespace TNB
25{
26
27
28
42template<typename TYP, typename DIF=TYP>
43class CRangeT : public IComparableT< TNB::CRangeT<TYP,DIF> >, public ISerializable
44{
45 TYP m_first;
46 TYP m_last;
47
49 void m_CheckRange(void)
50 {
51 if ( m_first > m_last )
52 {
53 Swap(m_first, m_last);
54 ASSERTLIB(m_first <= m_last);
55 }
56 }
57
58public:
59
64 CRangeT(const TYP& t = TYP()) : m_first(t), m_last(t)
65 {
66 }
67
74 CRangeT(const TYP& t1, const TYP& t2) : m_first(t1), m_last(t2)
75 {
76 m_CheckRange();
77 }
78
83 CRangeT(const CRangeT& other) : m_first(other.m_first), m_last(other.m_last)
84 {
85 ASSERTLIB(m_first <= m_last);
86 }
87
93 CRangeT& operator=(const CRangeT& other)
94 {
95 m_first = other.m_first;
96 m_last = other.m_last;
97 ASSERTLIB(m_first <= m_last);
98 return *this;
99 }
100
107 void Set(const TYP& t1, const TYP& t2)
108 {
109 m_first = t1;
110 m_last = t2;
111 m_CheckRange();
112 }
113
119 void SetFirst(const TYP& t)
120 {
121 m_first = t;
122 m_CheckRange();
123 }
124
130 void SetLast(const TYP& t)
131 {
132 m_first = t;
133 m_CheckRange();
134 }
135
142 void SetRange(const DIF& d)
143 {
144 m_last = m_first + d;
145 m_CheckRange();
146 }
147
152 TYP GetFirst(void)
153 {
154 return m_first;
155 }
156
161 const TYP& GetFirst(void) const
162 {
163 return m_first;
164 }
165
170 TYP GetLast(void)
171 {
172 return m_last;
173 }
174
179 const TYP& GetLast(void) const
180 {
181 return m_last;
182 }
183
189 DIF Difference(void) const
190 {
191 return m_last - m_first;
192 }
193
199 TYP Center(void) const
200 {
201 TYP r = m_first;
202 return r += (Difference() / 2);
203 }
204
211 CRangeT& operator+=(const DIF& dif)
212 {
213 m_first += dif;
214 m_last += dif;
215 m_CheckRange();
216 return *this;
217 }
218
226 inline friend CRangeT operator+(const CRangeT& rng, const DIF& dif)
227 {
228 CRangeT r = rng;
229 return r += dif;
230 }
231
238 CRangeT& operator-=(const DIF& dif)
239 {
240 m_first -= dif;
241 m_last -= dif;
242 m_CheckRange();
243 return *this;
244 }
245
253 inline friend CRangeT operator-(const CRangeT& rng, const DIF& dif)
254 {
255 CRangeT r = rng;
256 return r -= dif;
257 }
258
265 bool IsInRange(const TYP& t) const
266 {
267 return m_first <= t && m_last >= t;
268 }
269
277 bool IsInRange(const CRangeT& rng) const
278 {
279 return m_first <= rng.m_first && m_last >= rng.m_last;
280 }
281
288 bool InRange(const TYP& t) const
289 {
290 return IsInRange(t);
291 }
292
300 bool InRange(const CRangeT& rng) const
301 {
302 return IsInRange(rng);
303 }
304
307 {
310 WARPPED
311 };
312
322 {
323 CRangeT r1 = *this;
324 CRangeT r2 = rng;
325 if ( *this > rng )
326 {
327 r1 = rng;
328 r2 = *this;
329 }
330 if ( r1.m_last >= r2.m_last )
331 {
332 return WARPPED;
333 }
334 if ( r1.m_last >= r2.m_first )
335 {
336 if ( r1.m_first == r2.m_first )
337 {
338 return WARPPED;
339 }
340 return TOUCHED;
341 }
342 return UNRELATED;
343 }
344
352 CRangeT Intersect(const CRangeT& rng) const
353 {
354 CRangeT r1 = *this;
355 CRangeT r2 = rng;
356 if ( *this > rng )
357 {
358 r1 = rng;
359 r2 = *this;
360 }
361 ERelationType rt = r1.GetRelation(r2);
362 if ( rt == UNRELATED )
363 {
364 throw CEmptyException(); //無関係。重なっている部分はありません。
365 }
366 else if ( rt == WARPPED )
367 {
368 //内包。片方が全て含んでいます。
369 if ( r1.m_first == r2.m_first )
370 {
371 return r1;
372 }
373 return r2;
374 }
375 return CRangeT(r2.m_first, r1.m_last);
376 }
377
384 CRangeT Union(const CRangeT& rng) const
385 {
386 TYP t1 = min(m_first, rng.m_first);
387 TYP t2 = max(m_last, rng.m_last);
388 return CRangeT(t1, t2);
389 }
390
399 virtual INT_PTR Compare(const CRangeT& rng) const
400 {
401 INT_PTR r = IComparatorT<TYP>::GetDefault().CompareTo(m_first, rng.m_first);
402 return (r != 0) ? r : IComparatorT<TYP>::GetDefault().CompareTo(m_last, rng.m_last);
403 }
404
410 virtual void Serialize(ISerializer& _sr) const
411 {
412 _sr << m_first << m_last;
413 }
414
420 virtual void Deserialize(const IDeserializer& ds)
421 {
422 ds >> m_first >> m_last;
423 }
424};
425
426
427
428}; // TNB
429
430
431
432//T-TestCaseコードカバレッジEnable
433#pragma comment(user,"T-Coverage Enable")
比較機能関係のヘッダ
シリアライザー関係のヘッダ
取得要素(空き)無し例外
Definition: TnbException.h:107
範囲管理テンプレートクラス
Definition: TnbRange.h:44
virtual INT_PTR Compare(const CRangeT &rng) const
[確認] 比較
Definition: TnbRange.h:399
bool IsInRange(const CRangeT &rng) const
[確認] 範囲内確認
Definition: TnbRange.h:277
void SetRange(const DIF &d)
[代入] Diff値代入
Definition: TnbRange.h:142
CRangeT & operator=(const CRangeT &other)
[代入] コピーオペレータ
Definition: TnbRange.h:93
void SetFirst(const TYP &t)
[代入] First値代入
Definition: TnbRange.h:119
void Set(const TYP &t1, const TYP &t2)
[代入] 代入
Definition: TnbRange.h:107
virtual void Deserialize(const IDeserializer &ds)
[通知] デシリアライズ
Definition: TnbRange.h:420
bool IsInRange(const TYP &t) const
[確認] 範囲内確認
Definition: TnbRange.h:265
const TYP & GetLast(void) const
[取得] Last値取得
Definition: TnbRange.h:179
friend CRangeT operator+(const CRangeT &rng, const DIF &dif)
[取得] 加算
Definition: TnbRange.h:226
ERelationType
関係タイプ
Definition: TnbRange.h:307
@ WARPPED
内包
Definition: TnbRange.h:310
@ TOUCHED
一部接触
Definition: TnbRange.h:309
@ UNRELATED
無関係
Definition: TnbRange.h:308
bool InRange(const TYP &t) const
[確認] 範囲内確認
Definition: TnbRange.h:288
CRangeT(const TYP &t1, const TYP &t2)
コンストラクタ
Definition: TnbRange.h:74
DIF Difference(void) const
[計算] Diff値取得
Definition: TnbRange.h:189
CRangeT(const TYP &t=TYP())
コンストラクタ
Definition: TnbRange.h:64
CRangeT Union(const CRangeT &rng) const
[取得] 内包範囲取得
Definition: TnbRange.h:384
CRangeT Intersect(const CRangeT &rng) const
[取得] 重複範囲取得
Definition: TnbRange.h:352
ERelationType GetRelation(const CRangeT &rng) const
[確認] 関係確認
Definition: TnbRange.h:321
friend CRangeT operator-(const CRangeT &rng, const DIF &dif)
[取得] 減算
Definition: TnbRange.h:253
CRangeT & operator+=(const DIF &dif)
[計算] 加算
Definition: TnbRange.h:211
const TYP & GetFirst(void) const
[取得] First値取得
Definition: TnbRange.h:161
TYP GetFirst(void)
[取得] First値取得
Definition: TnbRange.h:152
TYP GetLast(void)
[取得] Last値取得
Definition: TnbRange.h:170
virtual void Serialize(ISerializer &_sr) const
[通知] シリアライズ
Definition: TnbRange.h:410
CRangeT(const CRangeT &other)
コピーコンストラクタ
Definition: TnbRange.h:83
bool InRange(const CRangeT &rng) const
[確認] 範囲内確認
Definition: TnbRange.h:300
void SetLast(const TYP &t)
[代入] Last値代入
Definition: TnbRange.h:130
CRangeT & operator-=(const DIF &dif)
[計算] 減算
Definition: TnbRange.h:238
TYP Center(void) const
[取得] 中央計算
Definition: TnbRange.h:199
void Swap(T &t1, T &t2)
[変換] スワッパー.
Definition: TnbDef.h:963
TNB Library
Definition: TnbDoxyTitle.txt:2
比較機能インターフェース.
Definition: TnbComparable.h:54
virtual INT_PTR CompareTo(const TYP &t1, const TYP &t2) const =0
[確認] 比較
static IComparatorT & GetDefault(void)
[作成] 汎用コンパレータ取得.
デシリアライザーインターフェースクラス.
シリアライザブルインターフェース.
Definition: TnbSerializer.h:47
シリアライザーインターフェースクラス.