SList: Add length and opEquals

This commit is contained in:
Eugen Wissner 2017-01-25 19:41:05 +01:00
parent 3e36ec0984
commit b2baba9237
5 changed files with 100 additions and 25 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
.dub
__test__*__
__test__*__.core
/tanya-test-library
/docs/
/docs.json

View File

@ -1,6 +1,6 @@
{
"name": "tanya",
"description": "D library with event loop",
"description": "General purpose, @nogc library",
"license": "MPL-2.0",
"copyright": "(c) Eugene Wissner <info@caraus.de>",
"authors": [

View File

@ -10,6 +10,8 @@
*/
module tanya.container.list;
import std.algorithm.comparison;
import std.algorithm.searching;
import std.traits;
import tanya.container.entry;
import tanya.memory;
@ -31,6 +33,11 @@ private struct Range(E)
return this;
}
@property size_t length() const
{
return count(opIndex());
}
@property bool empty() const
{
return head is null;
@ -158,6 +165,72 @@ struct SList(T)
assert(l.front == 9);
}
/**
* Returns: How many elements are in the list.
*/
@property size_t length() const
{
return count(opIndex());
}
///
unittest
{
SList!int l;
l.insertFront(8);
l.insertFront(9);
assert(l.length == 2);
l.removeFront();
assert(l.length == 1);
l.removeFront();
assert(l.length == 0);
}
/**
* Comparison for equality.
*
* Params:
* that = The list to compare with.
*
* Returns: $(D_KEYWORD true) if the lists are equal, $(D_KEYWORD false)
* otherwise.
*/
bool opEquals()(auto ref typeof(this) that) @trusted
{
return equal(opIndex(), that[]);
}
/// Ditto.
bool opEquals()(in auto ref typeof(this) that) const @trusted
{
return equal(opIndex(), that[]);
}
///
unittest
{
SList!int l1, l2;
l1.insertFront(8);
l1.insertFront(9);
l2.insertFront(8);
l2.insertFront(10);
assert(l1 != l2);
l1.removeFront();
assert(l1 != l2);
l2.removeFront();
assert(l1 == l2);
l1.removeFront();
assert(l1 != l2);
l2.removeFront();
assert(l1 == l2);
}
/**
* Returns: $(D_KEYWORD true) if the list is empty.
*/

View File

@ -100,10 +100,8 @@ struct Queue(T)
*
* Params:
* x = New element.
*
* Returns: $(D_KEYWORD this).
*/
ref typeof(this) enqueue(ref T x)
void enqueue(ref T x)
{
auto temp = allocateEntry();
@ -111,12 +109,10 @@ struct Queue(T)
temp.content = x;
enqueueEntry(temp);
return this;
}
/// Ditto.
ref typeof(this) enqueue(T x)
void enqueue(T x)
{
auto temp = allocateEntry();
@ -124,8 +120,6 @@ struct Queue(T)
(*temp).next = null;
enqueueEntry(temp);
return this;
}
///
@ -134,7 +128,8 @@ struct Queue(T)
Queue!int q;
assert(q.empty);
q.enqueue(8).enqueue(9);
q.enqueue(8);
q.enqueue(9);
assert(q.dequeue() == 8);
assert(q.dequeue() == 9);
}
@ -183,7 +178,8 @@ struct Queue(T)
{
Queue!int q;
q.enqueue(8).enqueue(9);
q.enqueue(8);
q.enqueue(9);
assert(q.dequeue() == 8);
assert(q.dequeue() == 9);
}
@ -232,7 +228,9 @@ struct Queue(T)
Queue!int q;
size_t j;
q.enqueue(5).enqueue(4).enqueue(9);
q.enqueue(5);
q.enqueue(4);
q.enqueue(9);
foreach (i, e; q)
{
assert(i != 2 || e == 9);
@ -244,7 +242,9 @@ struct Queue(T)
assert(q.empty);
j = 0;
q.enqueue(5).enqueue(4).enqueue(9);
q.enqueue(5);
q.enqueue(4);
q.enqueue(9);
foreach (e; q)
{
assert(j != 2 || e == 9);
@ -270,7 +270,8 @@ unittest
q.enqueue(5);
assert(!q.empty);
q.enqueue(4).enqueue(9);
q.enqueue(4);
q.enqueue(9);
assert(q.dequeue() == 5);

View File

@ -1060,42 +1060,42 @@ struct Vector(T)
* Comparison for equality.
*
* Params:
* v = The vector to compare with.
* that = The vector to compare with.
*
* Returns: $(D_KEYWORD true) if the vectors are equal, $(D_KEYWORD false)
* otherwise.
*/
bool opEquals()(auto ref typeof(this) v) @trusted
bool opEquals()(auto ref typeof(this) that) @trusted
{
return equal(vector[0 .. length_], v.vector[0 .. v.length_]);
return equal(vector[0 .. length_], that.vector[0 .. that.length_]);
}
/// Ditto.
bool opEquals()(in auto ref typeof(this) v) const @trusted
bool opEquals()(in auto ref typeof(this) that) const @trusted
{
return equal(vector[0 .. length_], v.vector[0 .. length_]);
return equal(vector[0 .. length_], that.vector[0 .. that.length_]);
}
/// Ditto.
bool opEquals(Range!T v)
bool opEquals(Range!T that)
{
return equal(opIndex(), v);
return equal(opIndex(), that);
}
/**
* Comparison for equality.
*
* Params:
* R = Right hand side type.
* v = Right hand side vector range.
* R = Right hand side type.
* that = Right hand side vector range.
*
* Returns: $(D_KEYWORD true) if the vector and the range are equal,
* $(D_KEYWORD false) otherwise.
*/
bool opEquals(R)(Range!R v) const
bool opEquals(R)(Range!R that) const
if (is(Unqual!R == T))
{
return equal(opIndex(), v);
return equal(opIndex(), that);
}
///