YSTest: bitseg.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

bitseg.hpp
浏览该文件的文档.
1 /*
2 © 2013-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_bitseg_hpp_
29 #define YB_INC_ystdex_bitseg_hpp_ 1
30
31 #include "iterator_op.hpp" // for size_t, std::iterator,
32 // std::random_access_iterator_tag, byte, ptrdiff_t, iterator_operators_t,
33 // std::iterator_traits, CHAR_BIT;
34
35 namespace ystdex
36 {
37
50 template<size_t _vN, bool _bEndian = false>
51 class bitseg_iterator : public
52 random_access_iteratable<bitseg_iterator<_vN, _bEndian>, ptrdiff_t, byte&>
53 {
54 static_assert(_vN != 0, "A bit segment should contain at least one bit.");
55 static_assert(_vN != CHAR_BIT, "A bit segment should not be a byte.");
56 static_assert(CHAR_BIT % _vN == 0,
57 "A byte should be divided by number of segments without padding.");
58
59 public:
61 using iterator_category = std::random_access_iterator_tag;
63 using value_type = byte;
64 using difference_type = ptrdiff_t;
65 using pointer = byte*;
66 using reference = byte&;
67
69 static yconstexpr const bool lsb = _bEndian;
70 static yconstexpr const unsigned char seg_n = CHAR_BIT / _vN;
71 static yconstexpr const unsigned char seg_size = 1 << _vN;
72 static yconstexpr const unsigned char seg_width = _vN;
73
74 protected:
75 byte* base;
77 unsigned char shift;
78 mutable byte value;
79
80 public:
82
83
89 bitseg_iterator(byte* p = {}, size_t n = 0) ynothrow
90 : base(p), shift(n)
91 {
92 yassume(shift < seg_n);
93 }
94
95 bitseg_iterator&
96 operator+=(difference_type n) ynothrowv
97 {
98 yconstraint(base);
99 yassume(shift < seg_n);
100
101 const size_t new_shift(shift + size_t(n));
102
103 yunseq(base += new_shift / seg_n, shift = new_shift % seg_n);
104 return *this;
105 }
106
107 bitseg_iterator&
108 operator-=(difference_type n) ynothrowv
109 {
110 base += -n;
111 return *this;
112 }
113
114 reference
115 operator*() const ynothrowv
116 {
117 yconstraint(base);
118 return value = *base >> seg_width * (lsb ? seg_n - 1 - shift : shift)
119 & ((1 << seg_width) - 1);
120 }
121
122 inline bitseg_iterator&
123 operator++() ynothrowv
124 {
125 yconstraint(base);
126 yassume(shift < seg_n);
127 if(++shift == seg_n)
128 yunseq(shift = 0, ++base);
129 return *this;
130 }
131
132 inline bitseg_iterator&
133 operator--() ynothrowv
134 {
135 yconstraint(base);
136 yassume(shift < seg_n);
137 if(shift == 0)
138 yunseq(--base, shift = seg_n - 1);
139 else
140 --shift;
141 return *this;
142 }
143
145
146 friend bool
147 operator==(const bitseg_iterator& x, const bitseg_iterator& y) ynothrow
148 {
149 return x.base == y.base && x.shift == y.shift;
150 }
151
152 friend bool
153 operator<(const bitseg_iterator& x, const bitseg_iterator& y) ynothrow
154 {
155 return x.base < y.base || (x.base == y.base && x.shift < y.shift);
156 }
158
159 explicit yconstfn
160 operator pointer() const ynothrow
161 {
162 return base;
163 }
164
165 yconstfn size_t
166 get_shift() const ynothrow
167 {
168 return shift;
169 }
171 };
172
173 } // namespace ystdex;
174
175 #endif
176
ystdex::bitseg_iterator::operator<
friend bool operator<(const bitseg_iterator &x, const bitseg_iterator &y)
Definition: bitseg.hpp:153
ystdex::bitseg_iterator::operator+=
bitseg_iterator & operator+=(difference_type n)
Definition: bitseg.hpp:96
ystdex::bitseg_iterator::operator*
reference operator*() const
Definition: bitseg.hpp:115
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:99
platform::p
while p p
Definition: YCLib/FileSystem.h:225
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
ystdex::bitseg_iterator::operator-=
bitseg_iterator & operator-=(difference_type n)
Definition: bitseg.hpp:108
ystdex::bitseg_iterator
位段迭代器。
Definition: bitseg.hpp:51
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:653
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::bitseg_iterator::operator--
bitseg_iterator & operator--()
Definition: bitseg.hpp:133
ystdex::bitseg_iterator::iterator_category
std::random_access_iterator_tag iterator_category
Definition: bitseg.hpp:61
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:82
ystdex::details::ebases
Definition: operators.hpp:100
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
ystdex::bitseg_iterator::reference
byte & reference
Definition: bitseg.hpp:66
ystdex::bitseg_iterator::difference_type
ptrdiff_t difference_type
Definition: bitseg.hpp:64
ystdex::bitseg_iterator::seg_size
static const unsigned char seg_size
Definition: bitseg.hpp:71
ystdex::bitseg_iterator::value_type
byte value_type
Definition: bitseg.hpp:63
ystdex::bitseg_iterator::operator++
bitseg_iterator & operator++()
Definition: bitseg.hpp:123
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:996
ystdex::bitseg_iterator::shift
unsigned char shift
Definition: bitseg.hpp:77
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::bitseg_iterator::pointer
byte * pointer
Definition: bitseg.hpp:65
ystdex::bitseg_iterator::operator==
friend bool operator==(const bitseg_iterator &x, const bitseg_iterator &y)
Definition: bitseg.hpp:147
ystdex::bitseg_iterator::seg_n
static const unsigned char seg_n
Definition: bitseg.hpp:70
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
ystdex::bitseg_iterator::base
byte * base
Definition: bitseg.hpp:75
iterator_op.hpp
迭代器操作。
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
ystdex::bitseg_iterator::lsb
static const bool lsb
Definition: bitseg.hpp:69
ystdex::bitseg_iterator::value
byte value
Definition: bitseg.hpp:78
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::bitseg_iterator::seg_width
static const unsigned char seg_width
Definition: bitseg.hpp:72
ystdex::bitseg_iterator::bitseg_iterator
bitseg_iterator(byte *p={}, size_t n=0)
构造：使用基指针和偏移位。
Definition: bitseg.hpp:89
ystdex::bitseg_iterator::get_shift
size_t get_shift() const
Definition: bitseg.hpp:166
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321