Make SList range public

This commit is contained in:
Eugen Wissner 2017-04-07 15:17:14 +02:00
parent b1d2b9bd9e
commit ba6bf554fb
2 changed files with 45 additions and 37 deletions

View File

@ -20,25 +20,31 @@ import std.traits;
import tanya.container.entry; import tanya.container.entry;
import tanya.memory; import tanya.memory;
private struct Range(Entry) /**
if (__traits(isSame, TemplateOf!Entry, SEntry)) * Forward range for the $(D_PSYMBOL SList).
*
* Params:
* E = Element type.
*/
struct SRange(E)
{ {
private alias T = typeof(E.content); private alias EntryPointer = CopyConstness!(E, SEntry!(Unqual!E)*);
private alias E = CopyConstness!(Entry, Entry*);
private E* head; private EntryPointer* head;
invariant invariant
{ {
assert(head !is null); assert(head !is null);
} }
private this(ref E head) @trusted private this(ref EntryPointer head) @trusted
{ {
this.head = &head; this.head = &head;
} }
@property Range save() @disable this();
@property SRange save()
{ {
return this; return this;
} }
@ -53,7 +59,7 @@ private struct Range(Entry)
return *head is null; return *head is null;
} }
@property ref inout(T) front() inout @property ref inout(E) front() inout
in in
{ {
assert(!empty); assert(!empty);
@ -73,12 +79,12 @@ private struct Range(Entry)
head = &(*head).next; head = &(*head).next;
} }
Range opIndex() SRange opIndex()
{ {
return typeof(return)(*head); return typeof(return)(*head);
} }
Range!(const Entry) opIndex() const SRange!(const E) opIndex() const
{ {
return typeof(return)(*head); return typeof(return)(*head);
} }
@ -340,6 +346,14 @@ struct SList(T)
return moveEntry(head, el); return moveEntry(head, el);
} }
/// Ditto.
size_t insertFront(R)(ref R el) @trusted
if (isImplicitlyConvertible!(R, T))
{
head = allocator.make!Entry(el, head);
return 1;
}
/// Ditto. /// Ditto.
size_t insertFront(R)(R el) @trusted size_t insertFront(R)(R el) @trusted
if (!isInfinite!R if (!isInfinite!R
@ -376,13 +390,6 @@ struct SList(T)
return insertFront!(T[])(el[]); return insertFront!(T[])(el[]);
} }
/// Ditto.
size_t insertFront(ref T el) @trusted
{
head = allocator.make!Entry(el, head);
return 1;
}
/// Ditto. /// Ditto.
alias insert = insertFront; alias insert = insertFront;
@ -407,7 +414,7 @@ struct SList(T)
version (assert) version (assert)
{ {
private bool checkRangeBelonging(ref Range!Entry r) const private bool checkRangeBelonging(ref SRange!T r) const
{ {
const(Entry*)* pos; const(Entry*)* pos;
for (pos = &head; pos != r.head && *pos !is null; pos = &(*pos).next) for (pos = &head; pos != r.head && *pos !is null; pos = &(*pos).next)
@ -429,7 +436,7 @@ struct SList(T)
* *
* Precondition: $(D_PARAM r) is extracted from this list. * Precondition: $(D_PARAM r) is extracted from this list.
*/ */
size_t insertBefore(R)(Range!Entry r, R el) size_t insertBefore(R)(SRange!T r, R el)
if (isImplicitlyConvertible!(R, T)) if (isImplicitlyConvertible!(R, T))
in in
{ {
@ -450,7 +457,7 @@ struct SList(T)
} }
/// Ditto. /// Ditto.
size_t insertBefore(R)(Range!Entry r, R el) size_t insertBefore(R)(SRange!T r, R el)
if (!isInfinite!R if (!isInfinite!R
&& isInputRange!R && isInputRange!R
&& isImplicitlyConvertible!(ElementType!R, T)) && isImplicitlyConvertible!(ElementType!R, T))
@ -482,7 +489,7 @@ struct SList(T)
} }
/// Ditto. /// Ditto.
size_t insertBefore(Range!Entry r, ref T el) @trusted size_t insertBefore(SRange!T r, ref T el) @trusted
in in
{ {
assert(checkRangeBelonging(r)); assert(checkRangeBelonging(r));
@ -515,7 +522,7 @@ struct SList(T)
* *
* Precondition: $(D_PARAM r) is extracted from this list. * Precondition: $(D_PARAM r) is extracted from this list.
*/ */
size_t insertBefore(size_t R)(Range!Entry r, T[R] el) size_t insertBefore(size_t R)(SRange!T r, T[R] el)
{ {
return insertFront!(T[])(el[]); return insertFront!(T[])(el[]);
} }
@ -670,7 +677,7 @@ struct SList(T)
* *
* Precondition: $(D_PARAM r) is extracted from this list. * Precondition: $(D_PARAM r) is extracted from this list.
*/ */
Range!Entry remove(Range!Entry r) SRange!T remove(SRange!T r)
in in
{ {
assert(checkRangeBelonging(r)); assert(checkRangeBelonging(r));
@ -759,13 +766,13 @@ struct SList(T)
* Returns: Range that iterates over all elements of the container, in * Returns: Range that iterates over all elements of the container, in
* forward order. * forward order.
*/ */
Range!Entry opIndex() SRange!T opIndex()
{ {
return typeof(return)(head); return typeof(return)(head);
} }
/// Ditto. /// Ditto.
Range!(const Entry) opIndex() const SRange!(const T) opIndex() const
{ {
return typeof(return)(head); return typeof(return)(head);
} }
@ -827,7 +834,7 @@ struct SList(T)
} }
next = &(*next).next; next = &(*next).next;
} }
remove(Range!Entry(*next)); remove(SRange!T(*next));
return this; return this;
} }

View File

@ -688,19 +688,20 @@ struct Vector(T)
* *
* Returns: The number of elements inserted. * Returns: The number of elements inserted.
*/ */
size_t insertBack(R)(auto ref R el) @trusted size_t insertBack(R)(R el)
if (isImplicitlyConvertible!(R, T)) if (isImplicitlyConvertible!(R, T))
{ {
static if (__traits(isRef, el)) moveBack(el);
return 1;
}
/// Ditto.
size_t insertBack(R)(ref R el) @trusted
if (isImplicitlyConvertible!(R, T))
{ {
reserve(this.length_ + 1); reserve(this.length_ + 1);
emplace(this.data + this.length_, el); emplace(this.data + this.length_, el);
++this.length_; ++this.length_;
}
else
{
moveBack(el);
}
return 1; return 1;
} }