diff options
Diffstat (limited to 'source/tanya/algorithm/searching.d')
| -rw-r--r-- | source/tanya/algorithm/searching.d | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/source/tanya/algorithm/searching.d b/source/tanya/algorithm/searching.d new file mode 100644 index 0000000..065bcde --- /dev/null +++ b/source/tanya/algorithm/searching.d @@ -0,0 +1,78 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Searching algorithms. + * + * Copyright: Eugene Wissner 2018. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/algorithm/searching.d, + * tanya/algorithm/searching.d) + */ +module tanya.algorithm.searching; + +import tanya.range; + +/** + * Counts the elements in an input range. + * + * If $(D_PARAM R) has length, $(D_PSYMBOL count) returns it, otherwise it + * iterates over the range and counts the elements. + * + * Params: + * R = Input range type. + * range = Input range. + * + * Returns: $(D_PARAM range) length. + */ +size_t count(R)(R range) +if (isInputRange!R) +{ + static if (hasLength!R) + { + return range.length; + } + else + { + size_t counter; + for (; !range.empty; range.popFront(), ++counter) + { + } + return counter; + } +} + +/// +@nogc nothrow pure @safe unittest +{ + int[3] array; + assert(count(array) == 3); +} + +@nogc nothrow pure @safe unittest +{ + static struct Range + { + private int counter = 3; + + int front() const @nogc nothrow pure @safe + { + return this.counter; + } + + void popFront() @nogc nothrow pure @safe + { + --this.counter; + } + + bool empty() const @nogc nothrow pure @safe + { + return this.counter == 0; + } + } + Range range; + assert(count(range) == 3); +} |
