Queue whitespaces.
This commit is contained in:
parent
2688591c6a
commit
424e6e5f98
|
@ -7,7 +7,7 @@
|
||||||
module dyaml.queue;
|
module dyaml.queue;
|
||||||
|
|
||||||
|
|
||||||
///Queue collection.
|
/// Queue collection.
|
||||||
import core.stdc.stdlib;
|
import core.stdc.stdlib;
|
||||||
import core.memory;
|
import core.memory;
|
||||||
|
|
||||||
|
@ -17,37 +17,35 @@ import std.traits;
|
||||||
|
|
||||||
package:
|
package:
|
||||||
|
|
||||||
/**
|
/// Simple queue implemented as a singly linked list with a tail pointer.
|
||||||
* Simple queue implemented as a singly linked list with a tail pointer.
|
///
|
||||||
*
|
/// Needed in some D:YAML code that needs a queue-like structure without too much
|
||||||
* Needed in some D:YAML code that needs a queue-like structure without too
|
/// reallocation that goes with an array.
|
||||||
* much reallocation that goes with an array.
|
///
|
||||||
*
|
/// This should be replaced once Phobos has a decent queue/linked list.
|
||||||
* This should be replaced once Phobos has a decent queue/linked list.
|
///
|
||||||
*
|
/// Uses manual allocation through malloc/free.
|
||||||
* Uses manual allocation through malloc/free.
|
///
|
||||||
*
|
/// Also has some features uncommon for a queue, e.g. iteration. Couldn't bother with
|
||||||
* Also has some features uncommon for a queue, e.g. iteration.
|
/// implementing a range, as this is used only as a placeholder until Phobos gets a
|
||||||
* Couldn't bother with implementing a range, as this is used only as
|
/// decent replacement.
|
||||||
* a placeholder until Phobos gets a decent replacement.
|
|
||||||
*/
|
|
||||||
struct Queue(T)
|
struct Queue(T)
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
///Linked list node containing one element and pointer to the next node.
|
/// Linked list node containing one element and pointer to the next node.
|
||||||
struct Node
|
struct Node
|
||||||
{
|
{
|
||||||
T payload_;
|
T payload_;
|
||||||
Node* next_ = null;
|
Node* next_ = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
///Start of the linked list - first element added in time (end of the queue).
|
/// Start of the linked list - first element added in time (end of the queue).
|
||||||
Node* first_ = null;
|
Node* first_ = null;
|
||||||
///Last element of the linked list - last element added in time (start of the queue).
|
/// Last element of the linked list - last element added in time (start of the queue).
|
||||||
Node* last_ = null;
|
Node* last_ = null;
|
||||||
///Cursor pointing to the current node in iteration.
|
/// Cursor pointing to the current node in iteration.
|
||||||
Node* cursor_ = null;
|
Node* cursor_ = null;
|
||||||
///Length of the queue.
|
/// Length of the queue.
|
||||||
size_t length_ = 0;
|
size_t length_ = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -89,17 +87,17 @@ struct Queue(T)
|
||||||
return cursor_ is null;
|
return cursor_ is null;
|
||||||
}
|
}
|
||||||
|
|
||||||
///Push new item to the queue.
|
/// Push new item to the queue.
|
||||||
void push(T item) @trusted nothrow
|
void push(T item) @trusted nothrow
|
||||||
{
|
{
|
||||||
Node* newLast = allocate!Node(item, cast(Node*)null);
|
Node* newLast = allocate!Node(item, cast(Node*)null);
|
||||||
if(last_ !is null){last_.next_ = newLast;}
|
if(last_ !is null) { last_.next_ = newLast; }
|
||||||
if(first_ is null){first_ = newLast;}
|
if(first_ is null) { first_ = newLast; }
|
||||||
last_ = newLast;
|
last_ = newLast;
|
||||||
++length_;
|
++length_;
|
||||||
}
|
}
|
||||||
|
|
||||||
///Insert a new item putting it to specified index in the linked list.
|
/// Insert a new item putting it to specified index in the linked list.
|
||||||
void insert(T item, in size_t idx) @trusted nothrow
|
void insert(T item, in size_t idx) @trusted nothrow
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
@ -109,31 +107,31 @@ struct Queue(T)
|
||||||
{
|
{
|
||||||
if(idx == 0)
|
if(idx == 0)
|
||||||
{
|
{
|
||||||
//Add after the first element - so this will be the next to pop.
|
// Add after the first element - so this will be the next to pop.
|
||||||
first_ = allocate!Node(item, first_);
|
first_ = allocate!Node(item, first_);
|
||||||
++length_;
|
++length_;
|
||||||
}
|
}
|
||||||
else if(idx == length_)
|
else if(idx == length_)
|
||||||
{
|
{
|
||||||
//Adding before last added element, so we can just push.
|
// Adding before last added element, so we can just push.
|
||||||
push(item);
|
push(item);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//Get the element before one we're inserting.
|
// Get the element before one we're inserting.
|
||||||
Node* current = first_;
|
Node* current = first_;
|
||||||
foreach(i; 1 .. idx)
|
foreach(i; 1 .. idx)
|
||||||
{
|
{
|
||||||
current = current.next_;
|
current = current.next_;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Insert a new node after current, and put current.next_ behind it.
|
// Insert a new node after current, and put current.next_ behind it.
|
||||||
current.next_ = allocate!Node(item, current.next_);
|
current.next_ = allocate!Node(item, current.next_);
|
||||||
++length_;
|
++length_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Return the next element in the queue and remove it.
|
/// Return the next element in the queue and remove it.
|
||||||
T pop() @trusted nothrow
|
T pop() @trusted nothrow
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
@ -181,22 +179,22 @@ struct Queue(T)
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
///Allocate a struct, passing arguments to its constructor or default initializer.
|
/// Allocate a struct, passing arguments to its constructor or default initializer.
|
||||||
T* allocate(T, Args...)(Args args) @system nothrow
|
T* allocate(T, Args...)(Args args) @system nothrow
|
||||||
{
|
{
|
||||||
T* ptr = cast(T*)malloc(T.sizeof);
|
T* ptr = cast(T*)malloc(T.sizeof);
|
||||||
*ptr = T(args);
|
*ptr = T(args);
|
||||||
//The struct might contain references to GC-allocated memory, so tell the GC about it.
|
// The struct might contain references to GC-allocated memory, so tell the GC about it.
|
||||||
static if(hasIndirections!T){GC.addRange(cast(void*)ptr, T.sizeof);}
|
static if(hasIndirections!T) { GC.addRange(cast(void*)ptr, T.sizeof); }
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
///Deallocate struct pointed at by specified pointer.
|
/// Deallocate struct pointed at by specified pointer.
|
||||||
void free(T)(T* ptr) @system nothrow
|
void free(T)(T* ptr) @system nothrow
|
||||||
{
|
{
|
||||||
//GC doesn't need to care about any references in this struct anymore.
|
// GC doesn't need to care about any references in this struct anymore.
|
||||||
static if(hasIndirections!T){GC.removeRange(cast(void*)ptr);}
|
static if(hasIndirections!T) { GC.removeRange(cast(void*)ptr); }
|
||||||
static if(hasMember!(T, "__dtor")){(*ptr).destroy;}
|
static if(hasMember!(T, "__dtor")) { (*ptr).destroy; }
|
||||||
core.stdc.stdlib.free(ptr);
|
core.stdc.stdlib.free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue