| 1 |
pure @safe nothrow @nogc unittest |
| 2 |
{ |
| 3 |
import std.range; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
int[5] a = [ 1, 2, 3, 4, 5 ]; |
| 7 |
int[5] b = [ 5, 4, 3, 2, 1 ]; |
| 8 |
assert(equal(retro(a[]), b[])); |
| 9 |
assert(retro(a[]).source is a[]); |
| 10 |
assert(retro(retro(a[])) is a[]); |
| 11 |
} |
| 12 |
|
| 13 |
pure @safe nothrow unittest |
| 14 |
{ |
| 15 |
import std.range; |
| 16 |
|
| 17 |
import std.algorithm.comparison : equal; |
| 18 |
|
| 19 |
int[] a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]; |
| 20 |
assert(equal(stride(a, 3), [ 1, 4, 7, 10 ][])); |
| 21 |
assert(stride(stride(a, 2), 3) == stride(a, 6)); |
| 22 |
} |
| 23 |
|
| 24 |
pure @safe nothrow unittest |
| 25 |
{ |
| 26 |
import std.range; |
| 27 |
|
| 28 |
import std.algorithm.comparison : equal; |
| 29 |
|
| 30 |
int[] arr1 = [ 1, 2, 3, 4 ]; |
| 31 |
int[] arr2 = [ 5, 6 ]; |
| 32 |
int[] arr3 = [ 7 ]; |
| 33 |
auto s = chain(arr1, arr2, arr3); |
| 34 |
assert(s.length == 7); |
| 35 |
assert(s[5] == 6); |
| 36 |
assert(equal(s, [1, 2, 3, 4, 5, 6, 7][])); |
| 37 |
} |
| 38 |
|
| 39 |
pure @safe nothrow unittest |
| 40 |
{ |
| 41 |
import std.range; |
| 42 |
|
| 43 |
import std.algorithm.comparison : equal; |
| 44 |
import std.algorithm.sorting : sort; |
| 45 |
|
| 46 |
int[] arr1 = [5, 2, 8]; |
| 47 |
int[] arr2 = [3, 7, 9]; |
| 48 |
int[] arr3 = [1, 4, 6]; |
| 49 |
|
| 50 |
// in-place sorting across all of the arrays |
| 51 |
auto s = arr1.chain(arr2, arr3).sort; |
| 52 |
|
| 53 |
assert(s.equal([1, 2, 3, 4, 5, 6, 7, 8, 9])); |
| 54 |
assert(arr1.equal([1, 2, 3])); |
| 55 |
assert(arr2.equal([4, 5, 6])); |
| 56 |
assert(arr3.equal([7, 8, 9])); |
| 57 |
} |
| 58 |
|
| 59 |
pure @safe nothrow unittest |
| 60 |
{ |
| 61 |
import std.range; |
| 62 |
|
| 63 |
import std.utf : byChar, byCodeUnit; |
| 64 |
|
| 65 |
auto s1 = "string one"; |
| 66 |
auto s2 = "string two"; |
| 67 |
// s1 and s2 front is dchar because of auto-decoding |
| 68 |
static assert(is(typeof(s1.front) == dchar) && is(typeof(s2.front) == dchar)); |
| 69 |
|
| 70 |
auto r1 = s1.chain(s2); |
| 71 |
// chains of ranges of the same character type give that same type |
| 72 |
static assert(is(typeof(r1.front) == dchar)); |
| 73 |
|
| 74 |
auto s3 = "string three".byCodeUnit; |
| 75 |
static assert(is(typeof(s3.front) == immutable char)); |
| 76 |
auto r2 = s1.chain(s3); |
| 77 |
// chaining ranges of mixed character types gives `dchar` |
| 78 |
static assert(is(typeof(r2.front) == dchar)); |
| 79 |
|
| 80 |
// use byChar on character ranges to correctly convert them to UTF-8 |
| 81 |
auto r3 = s1.byChar.chain(s3); |
| 82 |
static assert(is(typeof(r3.front) == immutable char)); |
| 83 |
} |
| 84 |
|
| 85 |
@safe nothrow pure @nogc unittest |
| 86 |
{ |
| 87 |
import std.range; |
| 88 |
|
| 89 |
import std.algorithm.comparison : equal; |
| 90 |
import std.algorithm.iteration : filter, map; |
| 91 |
|
| 92 |
auto data1 = only(1, 2, 3, 4).filter!(a => a != 3); |
| 93 |
auto data2 = only(5, 6, 7, 8).map!(a => a + 1); |
| 94 |
|
| 95 |
// choose() is primarily useful when you need to select one of two ranges |
| 96 |
// with different types at runtime. |
| 97 |
static assert(!is(typeof(data1) == typeof(data2))); |
| 98 |
|
| 99 |
auto chooseRange(bool pickFirst) |
| 100 |
{ |
| 101 |
// The returned range is a common wrapper type that can be used for |
| 102 |
// returning or storing either range without running into a type error. |
| 103 |
return choose(pickFirst, data1, data2); |
| 104 |
|
| 105 |
// Simply returning the chosen range without using choose() does not |
| 106 |
// work, because map() and filter() return different types. |
| 107 |
//return pickFirst ? data1 : data2; // does not compile |
| 108 |
} |
| 109 |
|
| 110 |
auto result = chooseRange(true); |
| 111 |
assert(result.equal(only(1, 2, 4))); |
| 112 |
|
| 113 |
result = chooseRange(false); |
| 114 |
assert(result.equal(only(6, 7, 8, 9))); |
| 115 |
} |
| 116 |
|
| 117 |
@safe nothrow pure @nogc unittest |
| 118 |
{ |
| 119 |
import std.range; |
| 120 |
|
| 121 |
auto test() |
| 122 |
{ |
| 123 |
import std.algorithm.comparison : equal; |
| 124 |
|
| 125 |
int[4] sarr1 = [1, 2, 3, 4]; |
| 126 |
int[2] sarr2 = [5, 6]; |
| 127 |
int[1] sarr3 = [7]; |
| 128 |
auto arr1 = sarr1[]; |
| 129 |
auto arr2 = sarr2[]; |
| 130 |
auto arr3 = sarr3[]; |
| 131 |
|
| 132 |
{ |
| 133 |
auto s = chooseAmong(0, arr1, arr2, arr3); |
| 134 |
auto t = s.save; |
| 135 |
assert(s.length == 4); |
| 136 |
assert(s[2] == 3); |
| 137 |
s.popFront(); |
| 138 |
assert(equal(t, only(1, 2, 3, 4))); |
| 139 |
} |
| 140 |
{ |
| 141 |
auto s = chooseAmong(1, arr1, arr2, arr3); |
| 142 |
assert(s.length == 2); |
| 143 |
s.front = 8; |
| 144 |
assert(equal(s, only(8, 6))); |
| 145 |
} |
| 146 |
{ |
| 147 |
auto s = chooseAmong(1, arr1, arr2, arr3); |
| 148 |
assert(s.length == 2); |
| 149 |
s[1] = 9; |
| 150 |
assert(equal(s, only(8, 9))); |
| 151 |
} |
| 152 |
{ |
| 153 |
auto s = chooseAmong(1, arr2, arr1, arr3)[1 .. 3]; |
| 154 |
assert(s.length == 2); |
| 155 |
assert(equal(s, only(2, 3))); |
| 156 |
} |
| 157 |
{ |
| 158 |
auto s = chooseAmong(0, arr1, arr2, arr3); |
| 159 |
assert(s.length == 4); |
| 160 |
assert(s.back == 4); |
| 161 |
s.popBack(); |
| 162 |
s.back = 5; |
| 163 |
assert(equal(s, only(1, 2, 5))); |
| 164 |
s.back = 3; |
| 165 |
assert(equal(s, only(1, 2, 3))); |
| 166 |
} |
| 167 |
{ |
| 168 |
uint[5] foo = [1, 2, 3, 4, 5]; |
| 169 |
uint[5] bar = [6, 7, 8, 9, 10]; |
| 170 |
auto c = chooseAmong(1, foo[], bar[]); |
| 171 |
assert(c[3] == 9); |
| 172 |
c[3] = 42; |
| 173 |
assert(c[3] == 42); |
| 174 |
assert(c.moveFront() == 6); |
| 175 |
assert(c.moveBack() == 10); |
| 176 |
assert(c.moveAt(4) == 10); |
| 177 |
} |
| 178 |
{ |
| 179 |
import std.range : cycle; |
| 180 |
auto s = chooseAmong(0, cycle(arr2), cycle(arr3)); |
| 181 |
assert(isInfinite!(typeof(s))); |
| 182 |
assert(!s.empty); |
| 183 |
assert(s[100] == 8); |
| 184 |
assert(s[101] == 9); |
| 185 |
assert(s[0 .. 3].equal(only(8, 9, 8))); |
| 186 |
} |
| 187 |
return 0; |
| 188 |
} |
| 189 |
// works at runtime |
| 190 |
auto a = test(); |
| 191 |
// and at compile time |
| 192 |
static b = test(); |
| 193 |
} |
| 194 |
|
| 195 |
@safe unittest |
| 196 |
{ |
| 197 |
import std.range; |
| 198 |
|
| 199 |
import std.algorithm.comparison : equal; |
| 200 |
|
| 201 |
int[] a = [ 1, 2, 3 ]; |
| 202 |
int[] b = [ 10, 20, 30, 40 ]; |
| 203 |
auto r = roundRobin(a, b); |
| 204 |
assert(equal(r, [ 1, 10, 2, 20, 3, 30, 40 ])); |
| 205 |
} |
| 206 |
|
| 207 |
@safe unittest |
| 208 |
{ |
| 209 |
import std.range; |
| 210 |
|
| 211 |
import std.algorithm.comparison : equal; |
| 212 |
|
| 213 |
auto interleave(R, E)(R range, E element) |
| 214 |
if ((isInputRange!R && hasLength!R) || isForwardRange!R) |
| 215 |
{ |
| 216 |
static if (hasLength!R) |
| 217 |
immutable len = range.length; |
| 218 |
else |
| 219 |
immutable len = range.save.walkLength; |
| 220 |
|
| 221 |
return roundRobin( |
| 222 |
range, |
| 223 |
element.repeat(len - 1) |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
assert(interleave([1, 2, 3], 0).equal([1, 0, 2, 0, 3])); |
| 228 |
} |
| 229 |
|
| 230 |
@safe unittest |
| 231 |
{ |
| 232 |
import std.range; |
| 233 |
|
| 234 |
import std.algorithm.comparison : equal; |
| 235 |
int[] a = [ 1, 2, 3, 4, 5 ]; |
| 236 |
assert(equal(radial(a), [ 3, 4, 2, 5, 1 ])); |
| 237 |
a = [ 1, 2, 3, 4 ]; |
| 238 |
assert(equal(radial(a), [ 2, 3, 1, 4 ])); |
| 239 |
|
| 240 |
// If the left end is reached first, the remaining elements on the right |
| 241 |
// are concatenated in order: |
| 242 |
a = [ 0, 1, 2, 3, 4, 5 ]; |
| 243 |
assert(equal(radial(a, 1), [ 1, 2, 0, 3, 4, 5 ])); |
| 244 |
|
| 245 |
// If the right end is reached first, the remaining elements on the left |
| 246 |
// are concatenated in reverse order: |
| 247 |
assert(equal(radial(a, 4), [ 4, 5, 3, 2, 1, 0 ])); |
| 248 |
} |
| 249 |
|
| 250 |
pure @safe nothrow unittest |
| 251 |
{ |
| 252 |
import std.range; |
| 253 |
|
| 254 |
import std.algorithm.comparison : equal; |
| 255 |
|
| 256 |
int[] arr1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; |
| 257 |
auto s = take(arr1, 5); |
| 258 |
assert(s.length == 5); |
| 259 |
assert(s[4] == 5); |
| 260 |
assert(equal(s, [ 1, 2, 3, 4, 5 ][])); |
| 261 |
} |
| 262 |
|
| 263 |
pure @safe nothrow unittest |
| 264 |
{ |
| 265 |
import std.range; |
| 266 |
|
| 267 |
import std.algorithm.comparison : equal; |
| 268 |
|
| 269 |
int[] arr2 = [ 1, 2, 3 ]; |
| 270 |
auto t = take(arr2, 5); |
| 271 |
assert(t.length == 3); |
| 272 |
assert(equal(t, [ 1, 2, 3 ])); |
| 273 |
} |
| 274 |
|
| 275 |
pure @safe nothrow unittest |
| 276 |
{ |
| 277 |
import std.range; |
| 278 |
|
| 279 |
import std.algorithm.comparison : equal; |
| 280 |
|
| 281 |
auto a = [ 1, 2, 3, 4, 5 ]; |
| 282 |
|
| 283 |
auto b = takeExactly(a, 3); |
| 284 |
assert(equal(b, [1, 2, 3])); |
| 285 |
static assert(is(typeof(b.length) == size_t)); |
| 286 |
assert(b.length == 3); |
| 287 |
assert(b.front == 1); |
| 288 |
assert(b.back == 3); |
| 289 |
} |
| 290 |
|
| 291 |
pure @safe nothrow unittest |
| 292 |
{ |
| 293 |
import std.range; |
| 294 |
|
| 295 |
auto s = takeOne([42, 43, 44]); |
| 296 |
static assert(isRandomAccessRange!(typeof(s))); |
| 297 |
assert(s.length == 1); |
| 298 |
assert(!s.empty); |
| 299 |
assert(s.front == 42); |
| 300 |
s.front = 43; |
| 301 |
assert(s.front == 43); |
| 302 |
assert(s.back == 43); |
| 303 |
assert(s[0] == 43); |
| 304 |
s.popFront(); |
| 305 |
assert(s.length == 0); |
| 306 |
assert(s.empty); |
| 307 |
} |
| 308 |
|
| 309 |
pure @safe nothrow @nogc unittest |
| 310 |
{ |
| 311 |
import std.range; |
| 312 |
|
| 313 |
auto range = takeNone!(int[])(); |
| 314 |
assert(range.length == 0); |
| 315 |
assert(range.empty); |
| 316 |
} |
| 317 |
|
| 318 |
pure @safe nothrow unittest |
| 319 |
{ |
| 320 |
import std.range; |
| 321 |
|
| 322 |
import std.algorithm.iteration : filter; |
| 323 |
assert(takeNone([42, 27, 19]).empty); |
| 324 |
assert(takeNone("dlang.org").empty); |
| 325 |
assert(takeNone(filter!"true"([42, 27, 19])).empty); |
| 326 |
} |
| 327 |
|
| 328 |
pure @safe nothrow unittest |
| 329 |
{ |
| 330 |
import std.range; |
| 331 |
|
| 332 |
// tail -c n |
| 333 |
assert([1, 2, 3].tail(1) == [3]); |
| 334 |
assert([1, 2, 3].tail(2) == [2, 3]); |
| 335 |
assert([1, 2, 3].tail(3) == [1, 2, 3]); |
| 336 |
assert([1, 2, 3].tail(4) == [1, 2, 3]); |
| 337 |
assert([1, 2, 3].tail(0).length == 0); |
| 338 |
|
| 339 |
// tail --lines=n |
| 340 |
import std.algorithm.comparison : equal; |
| 341 |
import std.algorithm.iteration : joiner; |
| 342 |
import std.exception : assumeWontThrow; |
| 343 |
import std.string : lineSplitter; |
| 344 |
assert("one\ntwo\nthree" |
| 345 |
.lineSplitter |
| 346 |
.tail(2) |
| 347 |
.joiner("\n") |
| 348 |
.equal("two\nthree") |
| 349 |
.assumeWontThrow); |
| 350 |
} |
| 351 |
|
| 352 |
@safe unittest |
| 353 |
{ |
| 354 |
import std.range; |
| 355 |
|
| 356 |
import std.algorithm.comparison : equal; |
| 357 |
|
| 358 |
assert([0, 2, 1, 5, 0, 3].drop(3) == [5, 0, 3]); |
| 359 |
assert("hello world".drop(6) == "world"); |
| 360 |
assert("hello world".drop(50).empty); |
| 361 |
assert("hello world".take(6).drop(3).equal("lo ")); |
| 362 |
} |
| 363 |
|
| 364 |
@safe unittest |
| 365 |
{ |
| 366 |
import std.range; |
| 367 |
|
| 368 |
import std.algorithm.comparison : equal; |
| 369 |
|
| 370 |
assert([0, 2, 1, 5, 0, 3].dropBack(3) == [0, 2, 1]); |
| 371 |
assert("hello world".dropBack(6) == "hello"); |
| 372 |
assert("hello world".dropBack(50).empty); |
| 373 |
assert("hello world".drop(4).dropBack(4).equal("o w")); |
| 374 |
} |
| 375 |
|
| 376 |
@safe unittest |
| 377 |
{ |
| 378 |
import std.range; |
| 379 |
|
| 380 |
import std.algorithm.comparison : equal; |
| 381 |
import std.algorithm.iteration : filterBidirectional; |
| 382 |
|
| 383 |
auto a = [1, 2, 3]; |
| 384 |
assert(a.dropExactly(2) == [3]); |
| 385 |
assert(a.dropBackExactly(2) == [1]); |
| 386 |
|
| 387 |
string s = "日本語"; |
| 388 |
assert(s.dropExactly(2) == "語"); |
| 389 |
assert(s.dropBackExactly(2) == "日"); |
| 390 |
|
| 391 |
auto bd = filterBidirectional!"true"([1, 2, 3]); |
| 392 |
assert(bd.dropExactly(2).equal([3])); |
| 393 |
assert(bd.dropBackExactly(2).equal([1])); |
| 394 |
} |
| 395 |
|
| 396 |
pure @safe nothrow unittest |
| 397 |
{ |
| 398 |
import std.range; |
| 399 |
|
| 400 |
import std.algorithm.comparison : equal; |
| 401 |
import std.algorithm.iteration : filterBidirectional; |
| 402 |
import std.container.dlist : DList; |
| 403 |
|
| 404 |
auto dl = DList!int(9, 1, 2, 3, 9); |
| 405 |
assert(dl[].dropOne().dropBackOne().equal([1, 2, 3])); |
| 406 |
|
| 407 |
auto a = [1, 2, 3]; |
| 408 |
assert(a.dropOne() == [2, 3]); |
| 409 |
assert(a.dropBackOne() == [1, 2]); |
| 410 |
|
| 411 |
string s = "日本語"; |
| 412 |
import std.exception : assumeWontThrow; |
| 413 |
assert(assumeWontThrow(s.dropOne() == "本語")); |
| 414 |
assert(assumeWontThrow(s.dropBackOne() == "日本")); |
| 415 |
|
| 416 |
auto bd = filterBidirectional!"true"([1, 2, 3]); |
| 417 |
assert(bd.dropOne().equal([2, 3])); |
| 418 |
assert(bd.dropBackOne().equal([1, 2])); |
| 419 |
} |
| 420 |
|
| 421 |
pure @safe nothrow unittest |
| 422 |
{ |
| 423 |
import std.range; |
| 424 |
|
| 425 |
import std.algorithm.comparison : equal; |
| 426 |
|
| 427 |
assert(5.repeat().take(4).equal([5, 5, 5, 5])); |
| 428 |
} |
| 429 |
|
| 430 |
pure @safe nothrow unittest |
| 431 |
{ |
| 432 |
import std.range; |
| 433 |
|
| 434 |
import std.algorithm.comparison : equal; |
| 435 |
|
| 436 |
assert(5.repeat(4).equal([5, 5, 5, 5])); |
| 437 |
} |
| 438 |
|
| 439 |
@safe pure nothrow unittest |
| 440 |
{ |
| 441 |
import std.range; |
| 442 |
|
| 443 |
import std.algorithm.comparison : equal; |
| 444 |
import std.algorithm.iteration : map; |
| 445 |
|
| 446 |
int i = 1; |
| 447 |
auto powersOfTwo = generate!(() => i *= 2)().take(10); |
| 448 |
assert(equal(powersOfTwo, iota(1, 11).map!"2^^a"())); |
| 449 |
} |
| 450 |
|
| 451 |
@safe pure nothrow unittest |
| 452 |
{ |
| 453 |
import std.range; |
| 454 |
|
| 455 |
import std.algorithm.comparison : equal; |
| 456 |
|
| 457 |
//Returns a run-time delegate |
| 458 |
auto infiniteIota(T)(T low, T high) |
| 459 |
{ |
| 460 |
T i = high; |
| 461 |
return (){if (i == high) i = low; return i++;}; |
| 462 |
} |
| 463 |
//adapted as a range. |
| 464 |
assert(equal(generate(infiniteIota(1, 4)).take(10), [1, 2, 3, 1, 2, 3, 1, 2, 3, 1])); |
| 465 |
} |
| 466 |
|
| 467 |
@safe unittest |
| 468 |
{ |
| 469 |
import std.range; |
| 470 |
|
| 471 |
import std.format : format; |
| 472 |
import std.random : uniform; |
| 473 |
|
| 474 |
auto r = generate!(() => uniform(0, 6)).take(10); |
| 475 |
format("%(%s %)", r); |
| 476 |
} |
| 477 |
|
| 478 |
@safe unittest |
| 479 |
{ |
| 480 |
import std.range; |
| 481 |
|
| 482 |
import std.algorithm.comparison : equal; |
| 483 |
import std.range : cycle, take; |
| 484 |
|
| 485 |
// Here we create an infinitive cyclic sequence from [1, 2] |
| 486 |
// (i.e. get here [1, 2, 1, 2, 1, 2 and so on]) then |
| 487 |
// take 5 elements of this sequence (so we have [1, 2, 1, 2, 1]) |
| 488 |
// and compare them with the expected values for equality. |
| 489 |
assert(cycle([1, 2]).take(5).equal([ 1, 2, 1, 2, 1 ])); |
| 490 |
} |
| 491 |
|
| 492 |
@nogc nothrow pure @safe unittest |
| 493 |
{ |
| 494 |
import std.range; |
| 495 |
|
| 496 |
import std.algorithm.comparison : equal; |
| 497 |
import std.algorithm.iteration : map; |
| 498 |
|
| 499 |
// pairwise sum |
| 500 |
auto arr = only(0, 1, 2); |
| 501 |
auto part1 = zip(arr, arr.dropOne).map!"a[0] + a[1]"; |
| 502 |
assert(part1.equal(only(1, 3))); |
| 503 |
} |
| 504 |
|
| 505 |
nothrow pure @safe unittest |
| 506 |
{ |
| 507 |
import std.range; |
| 508 |
|
| 509 |
import std.conv : to; |
| 510 |
|
| 511 |
int[] a = [ 1, 2, 3 ]; |
| 512 |
string[] b = [ "a", "b", "c" ]; |
| 513 |
string[] result; |
| 514 |
|
| 515 |
foreach (tup; zip(a, b)) |
| 516 |
{ |
| 517 |
result ~= tup[0].to!string ~ tup[1]; |
| 518 |
} |
| 519 |
|
| 520 |
assert(result == [ "1a", "2b", "3c" ]); |
| 521 |
|
| 522 |
size_t idx = 0; |
| 523 |
// unpacking tuple elements with foreach |
| 524 |
foreach (e1, e2; zip(a, b)) |
| 525 |
{ |
| 526 |
assert(e1 == a[idx]); |
| 527 |
assert(e2 == b[idx]); |
| 528 |
++idx; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
nothrow pure @safe unittest |
| 533 |
{ |
| 534 |
import std.range; |
| 535 |
|
| 536 |
import std.algorithm.sorting : sort; |
| 537 |
|
| 538 |
int[] a = [ 1, 2, 3 ]; |
| 539 |
string[] b = [ "a", "c", "b" ]; |
| 540 |
zip(a, b).sort!((t1, t2) => t1[0] > t2[0]); |
| 541 |
|
| 542 |
assert(a == [ 3, 2, 1 ]); |
| 543 |
// b is sorted according to a's sorting |
| 544 |
assert(b == [ "b", "c", "a" ]); |
| 545 |
} |
| 546 |
|
| 547 |
pure @safe unittest |
| 548 |
{ |
| 549 |
import std.range; |
| 550 |
|
| 551 |
import std.algorithm.comparison : equal; |
| 552 |
import std.exception : assertThrown; |
| 553 |
import std.range.primitives; |
| 554 |
import std.typecons : tuple; |
| 555 |
|
| 556 |
auto a = [1, 2, 3]; |
| 557 |
auto b = [4, 5, 6, 7]; |
| 558 |
|
| 559 |
auto shortest = zip(StoppingPolicy.shortest, a, b); |
| 560 |
assert(shortest.equal([ |
| 561 |
tuple(1, 4), |
| 562 |
tuple(2, 5), |
| 563 |
tuple(3, 6) |
| 564 |
])); |
| 565 |
|
| 566 |
auto longest = zip(StoppingPolicy.longest, a, b); |
| 567 |
assert(longest.equal([ |
| 568 |
tuple(1, 4), |
| 569 |
tuple(2, 5), |
| 570 |
tuple(3, 6), |
| 571 |
tuple(0, 7) |
| 572 |
])); |
| 573 |
|
| 574 |
auto same = zip(StoppingPolicy.requireSameLength, a, b); |
| 575 |
same.popFrontN(3); |
| 576 |
assertThrown!Exception(same.popFront); |
| 577 |
} |
| 578 |
|
| 579 |
pure @safe unittest |
| 580 |
{ |
| 581 |
import std.range; |
| 582 |
|
| 583 |
int[6] arr1 = [1,2,3,4,5,100]; |
| 584 |
int[5] arr2 = [6,7,8,9,10]; |
| 585 |
|
| 586 |
foreach (ref a, b; lockstep(arr1[], arr2[])) |
| 587 |
{ |
| 588 |
a += b; |
| 589 |
} |
| 590 |
|
| 591 |
assert(arr1 == [7,9,11,13,15,100]); |
| 592 |
} |
| 593 |
|
| 594 |
pure @safe unittest |
| 595 |
{ |
| 596 |
import std.range; |
| 597 |
|
| 598 |
int[3] arr1 = [1,2,3]; |
| 599 |
int[3] arr2 = [4,5,6]; |
| 600 |
|
| 601 |
foreach (index, a, b; lockstep(arr1[], arr2[])) |
| 602 |
{ |
| 603 |
assert(arr1[index] == a); |
| 604 |
assert(arr2[index] == b); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
pure @safe nothrow unittest |
| 609 |
{ |
| 610 |
import std.range; |
| 611 |
|
| 612 |
import std.algorithm.comparison : equal; |
| 613 |
|
| 614 |
// The Fibonacci numbers, using function in string form: |
| 615 |
// a[0] = 1, a[1] = 1, and compute a[n+1] = a[n-1] + a[n] |
| 616 |
auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1); |
| 617 |
assert(fib.take(10).equal([1, 1, 2, 3, 5, 8, 13, 21, 34, 55])); |
| 618 |
|
| 619 |
// The factorials, using function in lambda form: |
| 620 |
auto fac = recurrence!((a,n) => a[n-1] * n)(1); |
| 621 |
assert(take(fac, 10).equal([ |
| 622 |
1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 |
| 623 |
])); |
| 624 |
|
| 625 |
// The triangular numbers, using function in explicit form: |
| 626 |
static size_t genTriangular(R)(R state, size_t n) |
| 627 |
{ |
| 628 |
return state[n-1] + n; |
| 629 |
} |
| 630 |
auto tri = recurrence!genTriangular(0); |
| 631 |
assert(take(tri, 10).equal([0, 1, 3, 6, 10, 15, 21, 28, 36, 45])); |
| 632 |
} |
| 633 |
|
| 634 |
pure @safe nothrow @nogc unittest |
| 635 |
{ |
| 636 |
import std.range; |
| 637 |
|
| 638 |
auto odds = sequence!("a[0] + n * a[1]")(1, 2); |
| 639 |
assert(odds.front == 1); |
| 640 |
odds.popFront(); |
| 641 |
assert(odds.front == 3); |
| 642 |
odds.popFront(); |
| 643 |
assert(odds.front == 5); |
| 644 |
} |
| 645 |
|
| 646 |
pure @safe nothrow @nogc unittest |
| 647 |
{ |
| 648 |
import std.range; |
| 649 |
|
| 650 |
auto tri = sequence!((a,n) => n*(n+1)/2)(); |
| 651 |
|
| 652 |
// Note random access |
| 653 |
assert(tri[0] == 0); |
| 654 |
assert(tri[3] == 6); |
| 655 |
assert(tri[1] == 1); |
| 656 |
assert(tri[4] == 10); |
| 657 |
assert(tri[2] == 3); |
| 658 |
} |
| 659 |
|
| 660 |
@safe nothrow @nogc unittest |
| 661 |
{ |
| 662 |
import std.range; |
| 663 |
|
| 664 |
import std.math.exponential : pow; |
| 665 |
import std.math.rounding : round; |
| 666 |
import std.math.algebraic : sqrt; |
| 667 |
static ulong computeFib(S)(S state, size_t n) |
| 668 |
{ |
| 669 |
// Binet's formula |
| 670 |
return cast(ulong)(round((pow(state[0], n+1) - pow(state[1], n+1)) / |
| 671 |
state[2])); |
| 672 |
} |
| 673 |
auto fib = sequence!computeFib( |
| 674 |
(1.0 + sqrt(5.0)) / 2.0, // Golden Ratio |
| 675 |
(1.0 - sqrt(5.0)) / 2.0, // Conjugate of Golden Ratio |
| 676 |
sqrt(5.0)); |
| 677 |
|
| 678 |
// Note random access with [] operator |
| 679 |
assert(fib[1] == 1); |
| 680 |
assert(fib[4] == 5); |
| 681 |
assert(fib[3] == 3); |
| 682 |
assert(fib[2] == 2); |
| 683 |
assert(fib[9] == 55); |
| 684 |
} |
| 685 |
|
| 686 |
pure @safe unittest |
| 687 |
{ |
| 688 |
import std.range; |
| 689 |
|
| 690 |
import std.algorithm.comparison : equal; |
| 691 |
import std.math.operations : isClose; |
| 692 |
|
| 693 |
auto r = iota(0, 10, 1); |
| 694 |
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])); |
| 695 |
assert(equal(r, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])); |
| 696 |
assert(3 in r); |
| 697 |
assert(r.contains(3)); //Same as above |
| 698 |
assert(!(10 in r)); |
| 699 |
assert(!(-8 in r)); |
| 700 |
r = iota(0, 11, 3); |
| 701 |
assert(equal(r, [0, 3, 6, 9])); |
| 702 |
assert(r[2] == 6); |
| 703 |
assert(!(2 in r)); |
| 704 |
auto rf = iota(0.0, 0.5, 0.1); |
| 705 |
assert(isClose(rf, [0.0, 0.1, 0.2, 0.3, 0.4])); |
| 706 |
} |
| 707 |
|
| 708 |
@safe pure unittest |
| 709 |
{ |
| 710 |
import std.range; |
| 711 |
|
| 712 |
import std.algorithm.comparison : equal; |
| 713 |
import std.exception : assertThrown; |
| 714 |
|
| 715 |
auto arr = [[1, 2], [3, 4, 5]]; |
| 716 |
|
| 717 |
auto r1 = arr.frontTransversal!(TransverseOptions.assumeJagged); |
| 718 |
assert(r1.equal([1, 3])); |
| 719 |
|
| 720 |
// throws on construction |
| 721 |
assertThrown!Exception(arr.frontTransversal!(TransverseOptions.enforceNotJagged)); |
| 722 |
|
| 723 |
auto r2 = arr.frontTransversal!(TransverseOptions.assumeNotJagged); |
| 724 |
assert(r2.equal([1, 3])); |
| 725 |
|
| 726 |
// either assuming or checking for equal lengths makes |
| 727 |
// the result a random access range |
| 728 |
assert(r2[0] == 1); |
| 729 |
static assert(!__traits(compiles, r1[0])); |
| 730 |
} |
| 731 |
|
| 732 |
pure @safe nothrow unittest |
| 733 |
{ |
| 734 |
import std.range; |
| 735 |
|
| 736 |
import std.algorithm.comparison : equal; |
| 737 |
int[][] x = new int[][2]; |
| 738 |
x[0] = [1, 2]; |
| 739 |
x[1] = [3, 4]; |
| 740 |
auto ror = frontTransversal(x); |
| 741 |
assert(equal(ror, [ 1, 3 ][])); |
| 742 |
} |
| 743 |
|
| 744 |
@safe unittest |
| 745 |
{ |
| 746 |
import std.range; |
| 747 |
|
| 748 |
import std.algorithm.comparison : equal; |
| 749 |
int[][] x = new int[][2]; |
| 750 |
x[0] = [1, 2]; |
| 751 |
x[1] = [3, 4]; |
| 752 |
auto ror = transversal(x, 1); |
| 753 |
assert(equal(ror, [ 2, 4 ])); |
| 754 |
} |
| 755 |
|
| 756 |
@safe unittest |
| 757 |
{ |
| 758 |
import std.range; |
| 759 |
|
| 760 |
import std.algorithm.comparison : equal; |
| 761 |
import std.algorithm.iteration : map; |
| 762 |
int[][] y = [[1, 2, 3], [4, 5, 6]]; |
| 763 |
auto z = y.front.walkLength.iota.map!(i => transversal(y, i)); |
| 764 |
assert(equal!equal(z, [[1, 4], [2, 5], [3, 6]])); |
| 765 |
} |
| 766 |
|
| 767 |
@safe unittest |
| 768 |
{ |
| 769 |
import std.range; |
| 770 |
|
| 771 |
import std.algorithm.comparison : equal; |
| 772 |
int[][] ror = [ |
| 773 |
[1, 2, 3], |
| 774 |
[4, 5, 6] |
| 775 |
]; |
| 776 |
auto xp = transposed(ror); |
| 777 |
assert(equal!"a.equal(b)"(xp, [ |
| 778 |
[1, 4], |
| 779 |
[2, 5], |
| 780 |
[3, 6] |
| 781 |
])); |
| 782 |
} |
| 783 |
|
| 784 |
@safe unittest |
| 785 |
{ |
| 786 |
import std.range; |
| 787 |
|
| 788 |
int[][] x = new int[][2]; |
| 789 |
x[0] = [1, 2]; |
| 790 |
x[1] = [3, 4]; |
| 791 |
auto tr = transposed(x); |
| 792 |
int[][] witness = [ [ 1, 3 ], [ 2, 4 ] ]; |
| 793 |
uint i; |
| 794 |
|
| 795 |
foreach (e; tr) |
| 796 |
{ |
| 797 |
assert(array(e) == witness[i++]); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
@safe unittest |
| 802 |
{ |
| 803 |
import std.range; |
| 804 |
|
| 805 |
auto ind = indexed([1, 2, 3, 4, 5], [1, 3, 4]); |
| 806 |
assert(ind.physicalIndex(0) == 1); |
| 807 |
|
| 808 |
} |
| 809 |
|
| 810 |
@safe unittest |
| 811 |
{ |
| 812 |
import std.range; |
| 813 |
|
| 814 |
import std.algorithm.comparison : equal; |
| 815 |
auto source = [1, 2, 3, 4, 5]; |
| 816 |
auto indices = [4, 3, 1, 2, 0, 4]; |
| 817 |
auto ind = indexed(source, indices); |
| 818 |
assert(equal(ind, [5, 4, 2, 3, 1, 5])); |
| 819 |
assert(equal(retro(ind), [5, 1, 3, 2, 4, 5])); |
| 820 |
} |
| 821 |
|
| 822 |
@safe unittest |
| 823 |
{ |
| 824 |
import std.range; |
| 825 |
|
| 826 |
import std.algorithm.comparison : equal; |
| 827 |
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 828 |
auto chunks = chunks(source, 4); |
| 829 |
assert(chunks[0] == [1, 2, 3, 4]); |
| 830 |
assert(chunks[1] == [5, 6, 7, 8]); |
| 831 |
assert(chunks[2] == [9, 10]); |
| 832 |
assert(chunks.back == chunks[2]); |
| 833 |
assert(chunks.front == chunks[0]); |
| 834 |
assert(chunks.length == 3); |
| 835 |
assert(equal(retro(array(chunks)), array(retro(chunks)))); |
| 836 |
} |
| 837 |
|
| 838 |
@system unittest |
| 839 |
{ |
| 840 |
import std.range; |
| 841 |
|
| 842 |
import std.algorithm.comparison : equal; |
| 843 |
|
| 844 |
int i; |
| 845 |
|
| 846 |
// The generator doesn't save state, so it cannot be a forward range. |
| 847 |
auto inputRange = generate!(() => ++i).take(10); |
| 848 |
|
| 849 |
// We can still process it in chunks, but it will be single-pass only. |
| 850 |
auto chunked = inputRange.chunks(2); |
| 851 |
|
| 852 |
assert(chunked.front.equal([1, 2])); |
| 853 |
assert(chunked.front.empty); // Iterating the chunk has consumed it |
| 854 |
chunked.popFront; |
| 855 |
assert(chunked.front.equal([3, 4])); |
| 856 |
} |
| 857 |
|
| 858 |
@safe unittest |
| 859 |
{ |
| 860 |
import std.range; |
| 861 |
|
| 862 |
import std.algorithm.comparison : equal; |
| 863 |
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 864 |
auto chunks = evenChunks(source, 3); |
| 865 |
assert(chunks[0] == [1, 2, 3, 4]); |
| 866 |
assert(chunks[1] == [5, 6, 7]); |
| 867 |
assert(chunks[2] == [8, 9, 10]); |
| 868 |
} |
| 869 |
|
| 870 |
@safe pure nothrow unittest |
| 871 |
{ |
| 872 |
import std.range; |
| 873 |
|
| 874 |
import std.algorithm.comparison : equal; |
| 875 |
|
| 876 |
assert([0, 1, 2, 3].slide(2).equal!equal( |
| 877 |
[[0, 1], [1, 2], [2, 3]] |
| 878 |
)); |
| 879 |
|
| 880 |
assert(5.iota.slide(3).equal!equal( |
| 881 |
[[0, 1, 2], [1, 2, 3], [2, 3, 4]] |
| 882 |
)); |
| 883 |
} |
| 884 |
|
| 885 |
@safe pure nothrow unittest |
| 886 |
{ |
| 887 |
import std.range; |
| 888 |
|
| 889 |
import std.algorithm.comparison : equal; |
| 890 |
|
| 891 |
assert(6.iota.slide(1, 2).equal!equal( |
| 892 |
[[0], [2], [4]] |
| 893 |
)); |
| 894 |
|
| 895 |
assert(6.iota.slide(2, 4).equal!equal( |
| 896 |
[[0, 1], [4, 5]] |
| 897 |
)); |
| 898 |
|
| 899 |
assert(iota(7).slide(2, 2).equal!equal( |
| 900 |
[[0, 1], [2, 3], [4, 5], [6]] |
| 901 |
)); |
| 902 |
|
| 903 |
assert(iota(12).slide(2, 4).equal!equal( |
| 904 |
[[0, 1], [4, 5], [8, 9]] |
| 905 |
)); |
| 906 |
} |
| 907 |
|
| 908 |
@safe pure nothrow unittest |
| 909 |
{ |
| 910 |
import std.range; |
| 911 |
|
| 912 |
import std.algorithm.comparison : equal; |
| 913 |
|
| 914 |
assert(3.iota.slide!(No.withPartial)(4).empty); |
| 915 |
assert(3.iota.slide!(Yes.withPartial)(4).equal!equal( |
| 916 |
[[0, 1, 2]] |
| 917 |
)); |
| 918 |
} |
| 919 |
|
| 920 |
@safe pure nothrow unittest |
| 921 |
{ |
| 922 |
import std.range; |
| 923 |
|
| 924 |
import std.algorithm.iteration : each; |
| 925 |
|
| 926 |
int[dstring] d; |
| 927 |
"AGAGA"d.slide!(Yes.withPartial)(2).each!(a => d[a]++); |
| 928 |
assert(d == ["AG"d: 2, "GA"d: 2]); |
| 929 |
} |
| 930 |
|
| 931 |
@safe pure nothrow unittest |
| 932 |
{ |
| 933 |
import std.range; |
| 934 |
|
| 935 |
import std.algorithm.comparison : equal; |
| 936 |
|
| 937 |
assert(5.iota.slide!(Yes.withPartial)(3, 4).equal!equal([[0, 1, 2], [4]])); |
| 938 |
assert(6.iota.slide!(Yes.withPartial)(3, 4).equal!equal([[0, 1, 2], [4, 5]])); |
| 939 |
assert(7.iota.slide!(Yes.withPartial)(3, 4).equal!equal([[0, 1, 2], [4, 5, 6]])); |
| 940 |
|
| 941 |
assert(5.iota.slide!(No.withPartial)(3, 4).equal!equal([[0, 1, 2]])); |
| 942 |
assert(6.iota.slide!(No.withPartial)(3, 4).equal!equal([[0, 1, 2]])); |
| 943 |
assert(7.iota.slide!(No.withPartial)(3, 4).equal!equal([[0, 1, 2], [4, 5, 6]])); |
| 944 |
} |
| 945 |
|
| 946 |
@safe unittest |
| 947 |
{ |
| 948 |
import std.range; |
| 949 |
|
| 950 |
import std.algorithm.comparison : equal; |
| 951 |
import std.algorithm.iteration : filter, joiner, map; |
| 952 |
import std.algorithm.searching : findSplitBefore; |
| 953 |
import std.uni : isUpper; |
| 954 |
|
| 955 |
assert(equal(only('♡'), "♡")); |
| 956 |
assert([1, 2, 3, 4].findSplitBefore(only(3))[0] == [1, 2]); |
| 957 |
|
| 958 |
assert(only("one", "two", "three").joiner(" ").equal("one two three")); |
| 959 |
|
| 960 |
string title = "The D Programming Language"; |
| 961 |
assert(title |
| 962 |
.filter!isUpper // take the upper case letters |
| 963 |
.map!only // make each letter its own range |
| 964 |
.joiner(".") // join the ranges together lazily |
| 965 |
.equal("T.D.P.L")); |
| 966 |
} |
| 967 |
|
| 968 |
pure @safe nothrow unittest |
| 969 |
{ |
| 970 |
import std.range; |
| 971 |
|
| 972 |
import std.array : assocArray; |
| 973 |
import std.range : enumerate; |
| 974 |
|
| 975 |
bool[int] aa = true.repeat(3).enumerate(-1).assocArray(); |
| 976 |
assert(aa[-1]); |
| 977 |
assert(aa[0]); |
| 978 |
assert(aa[1]); |
| 979 |
} |
| 980 |
|
| 981 |
@safe unittest |
| 982 |
{ |
| 983 |
import std.range; |
| 984 |
|
| 985 |
void func1(int a, int b); |
| 986 |
void func2(int a, float b); |
| 987 |
|
| 988 |
static assert(isTwoWayCompatible!(func1, int, int)); |
| 989 |
static assert(isTwoWayCompatible!(func1, short, int)); |
| 990 |
static assert(!isTwoWayCompatible!(func2, int, float)); |
| 991 |
|
| 992 |
void func3(ref int a, ref int b); |
| 993 |
static assert( isTwoWayCompatible!(func3, int, int)); |
| 994 |
static assert(!isTwoWayCompatible!(func3, short, int)); |
| 995 |
} |
| 996 |
|
| 997 |
@safe unittest |
| 998 |
{ |
| 999 |
import std.range; |
| 1000 |
|
| 1001 |
import std.algorithm.comparison : equal; |
| 1002 |
|
| 1003 |
auto a = assumeSorted([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 1004 |
auto p1 = a.upperBound!(SearchPolicy.binarySearch)(3); |
| 1005 |
assert(p1.equal([4, 5, 6, 7, 8, 9])); |
| 1006 |
|
| 1007 |
auto p2 = a.lowerBound!(SearchPolicy.gallop)(4); |
| 1008 |
assert(p2.equal([0, 1, 2, 3])); |
| 1009 |
} |
| 1010 |
|
| 1011 |
@safe pure unittest |
| 1012 |
{ |
| 1013 |
import std.range; |
| 1014 |
|
| 1015 |
// create a SortedRange, that's checked strictly |
| 1016 |
SortedRange!(int[],"a < b", SortedRangeOptions.checkStrictly)([ 1, 3, 5, 7, 9 ]); |
| 1017 |
} |
| 1018 |
|
| 1019 |
@safe unittest |
| 1020 |
{ |
| 1021 |
import std.range; |
| 1022 |
|
| 1023 |
import std.algorithm.sorting : sort; |
| 1024 |
int[3] data = [ 1, 2, 3 ]; |
| 1025 |
auto a = assumeSorted(data[]); |
| 1026 |
assert(a == sort!"a < b"(data[])); |
| 1027 |
int[] p = a.release(); |
| 1028 |
assert(p == [ 1, 2, 3 ]); |
| 1029 |
|
| 1030 |
} |
| 1031 |
|
| 1032 |
@safe unittest |
| 1033 |
{ |
| 1034 |
import std.range; |
| 1035 |
|
| 1036 |
import std.algorithm.comparison : equal; |
| 1037 |
auto a = assumeSorted([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); |
| 1038 |
auto p = a.lowerBound(4); |
| 1039 |
assert(equal(p, [ 0, 1, 2, 3 ])); |
| 1040 |
|
| 1041 |
} |
| 1042 |
|
| 1043 |
@safe unittest |
| 1044 |
{ |
| 1045 |
import std.range; |
| 1046 |
|
| 1047 |
import std.algorithm.comparison : equal; |
| 1048 |
auto a = assumeSorted([ 1, 2, 3, 3, 3, 4, 4, 5, 6 ]); |
| 1049 |
auto p = a.upperBound(3); |
| 1050 |
assert(equal(p, [4, 4, 5, 6])); |
| 1051 |
|
| 1052 |
} |
| 1053 |
|
| 1054 |
@safe unittest |
| 1055 |
{ |
| 1056 |
import std.range; |
| 1057 |
|
| 1058 |
import std.algorithm.comparison : equal; |
| 1059 |
auto a = [ 1, 2, 3, 3, 3, 4, 4, 5, 6 ]; |
| 1060 |
auto r = a.assumeSorted.equalRange(3); |
| 1061 |
assert(equal(r, [ 3, 3, 3 ])); |
| 1062 |
|
| 1063 |
} |
| 1064 |
|
| 1065 |
@safe unittest |
| 1066 |
{ |
| 1067 |
import std.range; |
| 1068 |
|
| 1069 |
import std.algorithm.comparison : equal; |
| 1070 |
auto a = [ 1, 2, 3, 3, 3, 4, 4, 5, 6 ]; |
| 1071 |
auto r = assumeSorted(a).trisect(3); |
| 1072 |
assert(equal(r[0], [ 1, 2 ])); |
| 1073 |
assert(equal(r[1], [ 3, 3, 3 ])); |
| 1074 |
assert(equal(r[2], [ 4, 4, 5, 6 ])); |
| 1075 |
|
| 1076 |
} |
| 1077 |
|
| 1078 |
@safe unittest |
| 1079 |
{ |
| 1080 |
import std.range; |
| 1081 |
|
| 1082 |
import std.algorithm.sorting : sort; |
| 1083 |
auto a = [ 1, 2, 3, 42, 52, 64 ]; |
| 1084 |
auto r = assumeSorted(a); |
| 1085 |
assert(r.contains(3)); |
| 1086 |
assert(!(32 in r)); |
| 1087 |
auto r1 = sort!"a > b"(a); |
| 1088 |
assert(3 in r1); |
| 1089 |
assert(!r1.contains(32)); |
| 1090 |
assert(r1.release() == [ 64, 52, 42, 3, 2, 1 ]); |
| 1091 |
} |
| 1092 |
|
| 1093 |
@safe unittest |
| 1094 |
{ |
| 1095 |
import std.range; |
| 1096 |
|
| 1097 |
import std.algorithm.mutation : swap; |
| 1098 |
auto a = [ 1, 2, 3, 42, 52, 64 ]; |
| 1099 |
auto r = assumeSorted(a); |
| 1100 |
assert(r.contains(42)); |
| 1101 |
swap(a[3], a[5]); // illegal to break sortedness of original range |
| 1102 |
assert(!r.contains(42)); // passes although it shouldn't |
| 1103 |
} |
| 1104 |
|
| 1105 |
@safe unittest |
| 1106 |
{ |
| 1107 |
import std.range; |
| 1108 |
|
| 1109 |
import std.algorithm.comparison : equal; |
| 1110 |
static struct S { int i; } |
| 1111 |
static bool byI(A, B)(A a, B b) |
| 1112 |
{ |
| 1113 |
static if (is(A == S)) |
| 1114 |
return a.i < b; |
| 1115 |
else |
| 1116 |
return a < b.i; |
| 1117 |
} |
| 1118 |
auto r = assumeSorted!byI([S(1), S(2), S(3)]); |
| 1119 |
auto lessThanTwo = r.lowerBound(2); |
| 1120 |
assert(equal(lessThanTwo, [S(1)])); |
| 1121 |
} |
| 1122 |
|
| 1123 |
@safe unittest |
| 1124 |
{ |
| 1125 |
import std.range; |
| 1126 |
|
| 1127 |
import std.algorithm.comparison : equal; |
| 1128 |
|
| 1129 |
int[] a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 1130 |
auto p = assumeSorted(a); |
| 1131 |
|
| 1132 |
assert(equal(p.lowerBound(4), [0, 1, 2, 3])); |
| 1133 |
assert(equal(p.lowerBound(5), [0, 1, 2, 3, 4])); |
| 1134 |
assert(equal(p.lowerBound(6), [0, 1, 2, 3, 4, 5])); |
| 1135 |
assert(equal(p.lowerBound(6.9), [0, 1, 2, 3, 4, 5, 6])); |
| 1136 |
} |
| 1137 |
|
| 1138 |
@system unittest |
| 1139 |
{ |
| 1140 |
import std.range; |
| 1141 |
|
| 1142 |
import std.algorithm.searching : find; |
| 1143 |
ubyte[] buffer = [1, 9, 45, 12, 22]; |
| 1144 |
auto found1 = find(buffer, 45); |
| 1145 |
assert(found1 == [45, 12, 22]); |
| 1146 |
assert(buffer == [1, 9, 45, 12, 22]); |
| 1147 |
|
| 1148 |
auto wrapped1 = refRange(&buffer); |
| 1149 |
auto found2 = find(wrapped1, 45); |
| 1150 |
assert(*found2.ptr == [45, 12, 22]); |
| 1151 |
assert(buffer == [45, 12, 22]); |
| 1152 |
|
| 1153 |
auto found3 = find(wrapped1.save, 22); |
| 1154 |
assert(*found3.ptr == [22]); |
| 1155 |
assert(buffer == [45, 12, 22]); |
| 1156 |
|
| 1157 |
string str = "hello world"; |
| 1158 |
auto wrappedStr = refRange(&str); |
| 1159 |
assert(str.front == 'h'); |
| 1160 |
str.popFrontN(5); |
| 1161 |
assert(str == " world"); |
| 1162 |
assert(wrappedStr.front == ' '); |
| 1163 |
assert(*wrappedStr.ptr == " world"); |
| 1164 |
} |
| 1165 |
|
| 1166 |
@system unittest |
| 1167 |
{ |
| 1168 |
import std.range; |
| 1169 |
|
| 1170 |
ubyte[] buffer1 = [1, 2, 3, 4, 5]; |
| 1171 |
ubyte[] buffer2 = [6, 7, 8, 9, 10]; |
| 1172 |
auto wrapped1 = refRange(&buffer1); |
| 1173 |
auto wrapped2 = refRange(&buffer2); |
| 1174 |
assert(wrapped1.ptr is &buffer1); |
| 1175 |
assert(wrapped2.ptr is &buffer2); |
| 1176 |
assert(wrapped1.ptr !is wrapped2.ptr); |
| 1177 |
assert(buffer1 != buffer2); |
| 1178 |
|
| 1179 |
wrapped1 = wrapped2; |
| 1180 |
|
| 1181 |
//Everything points to the same stuff as before. |
| 1182 |
assert(wrapped1.ptr is &buffer1); |
| 1183 |
assert(wrapped2.ptr is &buffer2); |
| 1184 |
assert(wrapped1.ptr !is wrapped2.ptr); |
| 1185 |
|
| 1186 |
//But buffer1 has changed due to the assignment. |
| 1187 |
assert(buffer1 == [6, 7, 8, 9, 10]); |
| 1188 |
assert(buffer2 == [6, 7, 8, 9, 10]); |
| 1189 |
|
| 1190 |
buffer2 = [11, 12, 13, 14, 15]; |
| 1191 |
|
| 1192 |
//Everything points to the same stuff as before. |
| 1193 |
assert(wrapped1.ptr is &buffer1); |
| 1194 |
assert(wrapped2.ptr is &buffer2); |
| 1195 |
assert(wrapped1.ptr !is wrapped2.ptr); |
| 1196 |
|
| 1197 |
//But buffer2 has changed due to the assignment. |
| 1198 |
assert(buffer1 == [6, 7, 8, 9, 10]); |
| 1199 |
assert(buffer2 == [11, 12, 13, 14, 15]); |
| 1200 |
|
| 1201 |
wrapped2 = null; |
| 1202 |
|
| 1203 |
//The pointer changed for wrapped2 but not wrapped1. |
| 1204 |
assert(wrapped1.ptr is &buffer1); |
| 1205 |
assert(wrapped2.ptr is null); |
| 1206 |
assert(wrapped1.ptr !is wrapped2.ptr); |
| 1207 |
|
| 1208 |
//buffer2 is not affected by the assignment. |
| 1209 |
assert(buffer1 == [6, 7, 8, 9, 10]); |
| 1210 |
assert(buffer2 == [11, 12, 13, 14, 15]); |
| 1211 |
} |
| 1212 |
|
| 1213 |
@safe pure unittest |
| 1214 |
{ |
| 1215 |
import std.range; |
| 1216 |
|
| 1217 |
import std.algorithm.comparison : equal; |
| 1218 |
import std.format : format; |
| 1219 |
|
| 1220 |
// 00000011 00001001 |
| 1221 |
ubyte[] arr = [3, 9]; |
| 1222 |
auto r = arr.bitwise; |
| 1223 |
|
| 1224 |
// iterate through it as with any other range |
| 1225 |
assert(format("%(%d%)", r) == "1100000010010000"); |
| 1226 |
assert(format("%(%d%)", r.retro).equal("1100000010010000".retro)); |
| 1227 |
|
| 1228 |
auto r2 = r[5 .. $]; |
| 1229 |
// set a bit |
| 1230 |
r[2] = 1; |
| 1231 |
assert(arr[0] == 7); |
| 1232 |
assert(r[5] == r2[0]); |
| 1233 |
} |
| 1234 |
|
| 1235 |
@safe unittest |
| 1236 |
{ |
| 1237 |
import std.range; |
| 1238 |
|
| 1239 |
import std.algorithm.comparison : equal; |
| 1240 |
import std.random : rndGen; |
| 1241 |
|
| 1242 |
auto rb = rndGen.bitwise; |
| 1243 |
static assert(isInfinite!(typeof(rb))); |
| 1244 |
|
| 1245 |
auto rb2 = rndGen.bitwise; |
| 1246 |
// Don't forget that structs are passed by value |
| 1247 |
assert(rb.take(10).equal(rb2.take(10))); |
| 1248 |
} |
| 1249 |
|
| 1250 |
@safe nothrow unittest |
| 1251 |
{ |
| 1252 |
import std.range; |
| 1253 |
|
| 1254 |
import std.algorithm.iteration : map; |
| 1255 |
import std.algorithm.mutation : copy; |
| 1256 |
[4, 5, 6].map!(x => x * 2).copy(nullSink); // data is discarded |
| 1257 |
} |
| 1258 |
|
| 1259 |
@safe unittest |
| 1260 |
{ |
| 1261 |
import std.range; |
| 1262 |
|
| 1263 |
import std.csv : csvNextToken; |
| 1264 |
|
| 1265 |
string line = "a,b,c"; |
| 1266 |
|
| 1267 |
// ignore the first column |
| 1268 |
line.csvNextToken(nullSink, ',', '"'); |
| 1269 |
line.popFront; |
| 1270 |
|
| 1271 |
// look at the second column |
| 1272 |
Appender!string app; |
| 1273 |
line.csvNextToken(app, ',', '"'); |
| 1274 |
assert(app.data == "b"); |
| 1275 |
} |
| 1276 |
|
| 1277 |
@safe unittest |
| 1278 |
{ |
| 1279 |
import std.range; |
| 1280 |
|
| 1281 |
import std.algorithm.comparison : equal; |
| 1282 |
import std.algorithm.iteration : filter, map; |
| 1283 |
|
| 1284 |
// Sum values while copying |
| 1285 |
int[] values = [1, 4, 9, 16, 25]; |
| 1286 |
int sum = 0; |
| 1287 |
auto newValues = values.tee!(a => sum += a).array; |
| 1288 |
assert(equal(newValues, values)); |
| 1289 |
assert(sum == 1 + 4 + 9 + 16 + 25); |
| 1290 |
|
| 1291 |
// Count values that pass the first filter |
| 1292 |
int count = 0; |
| 1293 |
auto newValues4 = values.filter!(a => a < 10) |
| 1294 |
.tee!(a => count++) |
| 1295 |
.map!(a => a + 1) |
| 1296 |
.filter!(a => a < 10); |
| 1297 |
|
| 1298 |
//Fine, equal also evaluates any lazy ranges passed to it. |
| 1299 |
//count is not 3 until equal evaluates newValues4 |
| 1300 |
assert(equal(newValues4, [2, 5])); |
| 1301 |
assert(count == 3); |
| 1302 |
} |
| 1303 |
|
| 1304 |
@safe pure unittest |
| 1305 |
{ |
| 1306 |
import std.range; |
| 1307 |
|
| 1308 |
import std.algorithm.comparison : equal; |
| 1309 |
|
| 1310 |
assert([1, 2, 3, 4].padLeft(0, 6).equal([0, 0, 1, 2, 3, 4])); |
| 1311 |
assert([1, 2, 3, 4].padLeft(0, 3).equal([1, 2, 3, 4])); |
| 1312 |
|
| 1313 |
assert("abc".padLeft('_', 6).equal("___abc")); |
| 1314 |
} |
| 1315 |
|
| 1316 |
@safe pure unittest |
| 1317 |
{ |
| 1318 |
import std.range; |
| 1319 |
|
| 1320 |
import std.algorithm.comparison : equal; |
| 1321 |
|
| 1322 |
assert([1, 2, 3, 4].padRight(0, 6).equal([1, 2, 3, 4, 0, 0])); |
| 1323 |
assert([1, 2, 3, 4].padRight(0, 4).equal([1, 2, 3, 4])); |
| 1324 |
|
| 1325 |
assert("abc".padRight('_', 6).equal("abc___")); |
| 1326 |
} |
| 1327 |
|
| 1328 |
@safe unittest |
| 1329 |
{ |
| 1330 |
import std.range; |
| 1331 |
|
| 1332 |
import std.path : chainPath; |
| 1333 |
import std.range : chain; |
| 1334 |
|
| 1335 |
void someLibraryMethod(R)(R argument) |
| 1336 |
if (isSomeFiniteCharInputRange!R) |
| 1337 |
{ |
| 1338 |
// implementation detail, would iterate over each character of argument |
| 1339 |
} |
| 1340 |
|
| 1341 |
someLibraryMethod("simple strings work"); |
| 1342 |
someLibraryMethod(chain("chained", " ", "strings", " ", "work")); |
| 1343 |
someLibraryMethod(chainPath("chained", "paths", "work")); |
| 1344 |
// you can also use custom structs implementing a char range |
| 1345 |
} |
| 1346 |
|