Implement insertion into the vector

This commit is contained in:
Eugen Wissner 2016-12-15 15:00:06 +01:00
parent 061cd6264b
commit c1fb89af99
4 changed files with 667 additions and 588 deletions

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
*/
module tanya.math.mp;
import core.exception;
import std.algorithm.iteration;
import std.algorithm.searching;
import std.algorithm.mutation;
@ -29,7 +30,7 @@ struct Integer
pure nothrow @safe @nogc invariant
{
assert(!rep.count || rep.length || !sign, "0 should be positive.");
assert(rep.length || !sign, "0 should be positive.");
}
/**
@ -137,16 +138,14 @@ struct Integer
}
--size;
}
if (rep.count)
{
allocator.resizeArray(rep, size);
}
else
{
rep = () @trusted {
return cast(ubyte[]) allocator.allocate(size);
}();
}
rep = () @trusted {
void[] rep = this.rep;
if (!allocator.reallocate(rep, size))
{
onOutOfMemoryError();
}
return cast(ubyte[]) rep;
}();
/* Work backward through the int, masking off each byte (up to the
first 0 byte) and copy it into the internal representation in
big-endian format. */

View File

@ -15,8 +15,10 @@ module tanya.memory.allocator;
*/
interface Allocator
{
@nogc:
@property uint alignment() const shared pure nothrow @safe;
/**
* Returns: Alignment.
*/
@property uint alignment() const shared pure nothrow @safe @nogc;
/**
* Allocates $(D_PARAM size) bytes of memory.
@ -24,9 +26,9 @@ interface Allocator
* Params:
* size = Amount of memory to allocate.
*
* Returns: The pointer to the new allocated memory.
* Returns: Pointer to the new allocated memory.
*/
void[] allocate(size_t size, TypeInfo ti = null) shared nothrow @safe;
void[] allocate(size_t size) shared nothrow @safe @nogc;
/**
* Deallocates a memory block.
@ -36,7 +38,7 @@ interface Allocator
*
* Returns: Whether the deallocation was successful.
*/
bool deallocate(void[] p) shared nothrow @safe;
bool deallocate(void[] p) shared nothrow @safe @nogc;
/**
* Increases or decreases the size of a memory block.
@ -45,7 +47,7 @@ interface Allocator
* p = A pointer to the memory block.
* size = Size of the reallocated block.
*
* Returns: Whether the reallocation was successful.
* Returns: Pointer to the allocated memory.
*/
bool reallocate(ref void[] p, size_t size) shared nothrow @safe;
bool reallocate(ref void[] p, size_t size) shared nothrow @safe @nogc;
}

View File

@ -47,17 +47,8 @@ else version (Windows)
* | N | -----------> next| || N | | |
* | | | | | || | | |
* --------------------------------------------------- ------------------------
*
* TODO:
* $(UL
* $(LI Thread safety (core.atomic.cas))
* $(LI If two neighbour blocks are free, they can be merged)
* $(LI Reallocation shoud check if there is enough free space in the
* next block instead of always moving the memory)
* $(LI Make 64 KB regions mininmal region size on Linux)
* )
*/
class MmapPool : Allocator
final class MmapPool : Allocator
{
@nogc:
shared static this()
@ -80,9 +71,9 @@ class MmapPool : Allocator
* Params:
* size = Amount of memory to allocate.
*
* Returns: The pointer to the new allocated memory.
* Returns: Pointer to the new allocated memory.
*/
void[] allocate(size_t size, TypeInfo ti = null) shared nothrow @trusted
void[] allocate(size_t size) shared nothrow @trusted
{
if (!size)
{