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

View File

@ -15,8 +15,10 @@ module tanya.memory.allocator;
*/ */
interface 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. * Allocates $(D_PARAM size) bytes of memory.
@ -24,9 +26,9 @@ interface Allocator
* Params: * Params:
* size = Amount of memory to allocate. * 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. * Deallocates a memory block.
@ -36,7 +38,7 @@ interface Allocator
* *
* Returns: Whether the deallocation was successful. * 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. * Increases or decreases the size of a memory block.
@ -45,7 +47,7 @@ interface Allocator
* p = A pointer to the memory block. * p = A pointer to the memory block.
* size = Size of the reallocated 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 | | | * | 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: @nogc:
shared static this() shared static this()
@ -80,9 +71,9 @@ class MmapPool : Allocator
* Params: * Params:
* size = Amount of memory to allocate. * 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) if (!size)
{ {