From 2a90a812db53728d29a3a821cb292c032437d806 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 30 Sep 2018 15:25:10 +0200 Subject: [PATCH] Add algorithm.searching.count --- source/tanya/algorithm/searching.d | 78 ++++++++++++++++++++++++++++++ source/tanya/container/array.d | 2 - source/tanya/container/list.d | 1 - source/tanya/container/string.d | 3 +- source/tanya/network/socket.d | 4 +- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 source/tanya/algorithm/searching.d 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); +} diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d index 78e4dd5..4d58650 100644 --- a/source/tanya/container/array.d +++ b/source/tanya/container/array.d @@ -16,11 +16,9 @@ module tanya.container.array; import core.checkedint; import std.algorithm.mutation : bringToFront, - copy, fill, initializeAll, uninitializedFill; -import std.meta; import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.exception; diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d index 64ea740..38bc680 100644 --- a/source/tanya/container/list.d +++ b/source/tanya/container/list.d @@ -15,7 +15,6 @@ */ module tanya.container.list; -import std.algorithm.searching; import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.container.entry; diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d index 5e28fb8..e8c2d71 100644 --- a/source/tanya/container/string.d +++ b/source/tanya/container/string.d @@ -27,7 +27,6 @@ module tanya.container.string; import std.algorithm.mutation : bringToFront; -import std.algorithm.searching : count; import tanya.algorithm.comparison; import tanya.algorithm.mutation; import tanya.hash.lookup; @@ -1485,6 +1484,8 @@ struct String /// @nogc pure @safe unittest { + import tanya.algorithm.searching : count; + auto s = String("Из пословицы слова не выкинешь."); assert(s.remove(s[5 .. 24]).length == 33); diff --git a/source/tanya/network/socket.d b/source/tanya/network/socket.d index 95ac9ee..1b2d058 100644 --- a/source/tanya/network/socket.d +++ b/source/tanya/network/socket.d @@ -53,10 +53,10 @@ module tanya.network.socket; import core.stdc.errno; import core.time; public import std.socket : SocketOption, SocketOptionLevel; -import std.traits; -import std.typecons; import tanya.algorithm.comparison; +import tanya.bitmanip; import tanya.memory; +import tanya.meta.trait; import tanya.os.error; /// Value returned by socket operations on error.