Add algorithm.searching.count

This commit is contained in:
Eugen Wissner 2018-09-30 15:25:10 +02:00
parent e68fcc3a38
commit 2a90a812db
5 changed files with 82 additions and 6 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -15,7 +15,6 @@
*/
module tanya.container.list;
import std.algorithm.searching;
import tanya.algorithm.comparison;
import tanya.algorithm.mutation;
import tanya.container.entry;

View File

@ -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);

View File

@ -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.