Skip to content

Commit 8f72536

Browse files
committed
First cleanup of bitwise adapter + add to docs
1 parent bb3d78f commit 8f72536

File tree

1 file changed

+55
-28
lines changed

1 file changed

+55
-28
lines changed

std/range/package.d

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ The remainder of this module provides a rich set of _range creation and
3333
composition templates that let you construct new ranges out of existing ranges:
3434
3535
$(BOOKTABLE ,
36+
$(TR $(TD $(D $(LREF bitwise)))
37+
$(TD Bitwise adapter over a integral type _range.
38+
))
3639
$(TR $(TD $(D $(LREF chain)))
3740
$(TD Concatenates several ranges into a single _range.
3841
))
@@ -9594,17 +9597,7 @@ auto refRange(R)(R* range)
95949597
foo(r);
95959598
}
95969599

9597-
/**
9598-
Bitwise adapter over integral type ranges. Consumes the range elements bit by bit.
9599-
9600-
Params:
9601-
R = an input range to iterate over
9602-
9603-
Returns:
9604-
At minimum, an input range. If `R` was a forward, bidirectional or random
9605-
access range, `Bitwise!R` will be as well.
9606-
*/
9607-
struct Bitwise(R)
9600+
private struct Bitwise(R)
96089601
if (isInputRange!R && isIntegral!(ElementType!R))
96099602
{
96109603
private:
@@ -9635,7 +9628,7 @@ public:
96359628
{
96369629
static if (hasLength!R)
96379630
{
9638-
return length() == 0;
9631+
return length == 0;
96399632
}
96409633
else static if (isBidirectionalRange!R)
96419634
{
@@ -9664,7 +9657,7 @@ public:
96649657

96659658
bool front()
96669659
{
9667-
assert(!empty());
9660+
assert(!empty);
96689661
return (parent.front & mask(maskPos)) != 0;
96699662
}
96709663

@@ -9707,12 +9700,13 @@ public:
97079700
{
97089701
bool back()
97099702
{
9710-
assert(!empty());
9703+
assert(!empty);
97119704
return (parent.back & mask(backMaskPos)) != 0;
97129705
}
97139706

97149707
void popBack()
97159708
{
9709+
assert(!empty);
97169710
++backMaskPos;
97179711
if (backMaskPos > bitsNum)
97189712
{
@@ -9736,7 +9730,7 @@ public:
97369730
*/
97379731
static if (hasLength!R)
97389732
{
9739-
assert(n < length(), __PRETTY_FUNCTION__ ~ ": Index out of bounds");
9733+
assert(n < length, "Index out of bounds");
97409734
}
97419735
}
97429736
body
@@ -9765,14 +9759,14 @@ public:
97659759
}
97669760

97679761
/**
9768-
Assignes `flag` to the `n`th bit within the range
9762+
Assigns `flag` to the `n`th bit within the range
97699763
*/
97709764
void opIndexAssign(bool flag, size_t n)
97719765
in
97729766
{
97739767
static if (hasLength!R)
97749768
{
9775-
assert(n < length(), __PRETTY_FUNCTION__ ~ ": Index out of bounds");
9769+
assert(n < length, "Index out of bounds");
97769770
}
97779771
}
97789772
body
@@ -9792,13 +9786,13 @@ public:
97929786

97939787
Bitwise!R opSlice()
97949788
{
9795-
return this;
9789+
return this.save;
97969790
}
97979791

97989792
Bitwise!R opSlice(size_t start, size_t end)
97999793
in
98009794
{
9801-
assert(start < end, __PRETTY_FUNCTION__ ~ ": Invalid bounds: end <= start");
9795+
assert(start < end, "Invalid bounds: end <= start");
98029796
}
98039797
body
98049798
{
@@ -9834,9 +9828,43 @@ private:
98349828
}
98359829
}
98369830

9837-
// Test all range types over all integral types
9831+
/**
9832+
Bitwise adapter over a integral type range. Consumes the range elements bit by bit.
9833+
9834+
Params:
9835+
R = an integral input range to iterate over
9836+
9837+
Returns:
9838+
A `Bitwise` input range with propagated forward, bidirectional
9839+
and random access capabilities
9840+
*/
9841+
auto bitwise(R)(auto ref R range)
9842+
if (isInputRange!R && isIntegral!(ElementType!R))
9843+
{
9844+
return Bitwise!R(range);
9845+
}
9846+
98389847
///
9839-
@safe unittest
9848+
@safe pure unittest
9849+
{
9850+
import std.algorithm.comparison : equal;
9851+
import std.format : format;
9852+
9853+
// 00000011 00001001
9854+
ubyte[] arr = [3, 9];
9855+
auto r = arr.bitwise;
9856+
9857+
// iterate through it as with any other range
9858+
assert(format("%(%d%)", r) == "0000001100001001");
9859+
assert(format("%(%d%)", r.retro).equal("0000001100001001".retro));
9860+
9861+
// set a bit
9862+
r[5] = 1;
9863+
assert(arr[0] == 7);
9864+
}
9865+
9866+
// Test all range types over all integral types
9867+
@safe pure nothrow unittest
98409868
{
98419869
import std.internal.test.dummyrange;
98429870

@@ -9874,12 +9902,12 @@ private:
98749902
long numCalls = 42;
98759903
bool initialFrontValue;
98769904

9877-
if (!bw.empty())
9905+
if (!bw.empty)
98789906
{
98799907
initialFrontValue = bw.front;
98809908
}
98819909

9882-
while (!bw.empty() && (--numCalls))
9910+
while (!bw.empty && (--numCalls))
98839911
{
98849912
bw.front;
98859913
assert(bw.front == initialFrontValue);
@@ -9890,7 +9918,7 @@ private:
98909918
more times than it should
98919919
*/
98929920
numCalls = 0;
9893-
while (!bw.empty())
9921+
while (!bw.empty)
98949922
{
98959923
++numCalls;
98969924

@@ -9917,22 +9945,21 @@ private:
99179945

99189946
auto rangeLen = numCalls / (IntegralType.sizeof * 8);
99199947
assert(numCalls == (IntegralType.sizeof * 8 * rangeLen));
9920-
assert(bw.empty());
9948+
assert(bw.empty);
99219949
static if (isForwardRange!T)
99229950
{
9923-
assert(bw2.empty());
9951+
assert(bw2.empty);
99249952
}
99259953

99269954
static if (isBidirectionalRange!T)
99279955
{
9928-
assert(bw3.empty());
9956+
assert(bw3.empty);
99299957
}
99309958
}
99319959
}
99329960
}
99339961

99349962
// Test opIndex and opSlice
9935-
///
99369963
@system unittest
99379964
{
99389965
alias IntegralTypes = AliasSeq!(byte, ubyte, short, ushort, int, uint,

0 commit comments

Comments
 (0)