Add algorithm.searching.count
This commit is contained in:
parent
e68fcc3a38
commit
2a90a812db
78
source/tanya/algorithm/searching.d
Normal file
78
source/tanya/algorithm/searching.d
Normal 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);
|
||||
}
|
@ -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;
|
||||
|
@ -15,7 +15,6 @@
|
||||
*/
|
||||
module tanya.container.list;
|
||||
|
||||
import std.algorithm.searching;
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.entry;
|
||||
|
@ -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);
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user