Remove resizeArray alias
This commit is contained in:
parent
d0ada39fa7
commit
b90c56395c
@ -8,7 +8,7 @@
|
|||||||
* Copyright: Eugene Wissner 2016.
|
* Copyright: Eugene Wissner 2016.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:belka@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
*/
|
*/
|
||||||
module tanya.math.random;
|
module tanya.math.random;
|
||||||
|
|
||||||
@ -156,7 +156,7 @@ version (linux)
|
|||||||
*
|
*
|
||||||
* output = entropy.random;
|
* output = entropy.random;
|
||||||
*
|
*
|
||||||
* defaultAllocator.finalize(entropy);
|
* defaultAllocator.dispose(entropy);
|
||||||
* ---
|
* ---
|
||||||
*/
|
*/
|
||||||
class Entropy
|
class Entropy
|
||||||
@ -185,7 +185,7 @@ class Entropy
|
|||||||
}
|
}
|
||||||
body
|
body
|
||||||
{
|
{
|
||||||
allocator.resizeArray(sources, maxSources);
|
allocator.resize(sources, maxSources);
|
||||||
|
|
||||||
version (linux)
|
version (linux)
|
||||||
{
|
{
|
||||||
|
@ -29,6 +29,8 @@ mixin template DefaultAllocator()
|
|||||||
/**
|
/**
|
||||||
* Params:
|
* Params:
|
||||||
* allocator = The allocator should be used.
|
* allocator = The allocator should be used.
|
||||||
|
*
|
||||||
|
* Precondition: $(D_INLINECODE allocator_ !is null)
|
||||||
*/
|
*/
|
||||||
this(shared Allocator allocator)
|
this(shared Allocator allocator)
|
||||||
in
|
in
|
||||||
@ -46,7 +48,7 @@ mixin template DefaultAllocator()
|
|||||||
*
|
*
|
||||||
* Returns: Used allocator.
|
* Returns: Used allocator.
|
||||||
*
|
*
|
||||||
* Postcondition: $(D_INLINECODE allocator_ !is null)
|
* Postcondition: $(D_INLINECODE allocator !is null)
|
||||||
*/
|
*/
|
||||||
protected @property shared(Allocator) allocator() nothrow @safe @nogc
|
protected @property shared(Allocator) allocator() nothrow @safe @nogc
|
||||||
out (allocator)
|
out (allocator)
|
||||||
@ -142,7 +144,7 @@ size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal function used to create, resize or destroy a dynamic array. It
|
* Internal function used to create, resize or destroy a dynamic array. It
|
||||||
* throws $(D_PSYMBOL OutOfMemoryError) if $(D_PARAM Throws) is set. The new
|
* may throw $(D_PSYMBOL OutOfMemoryError). The new
|
||||||
* allocated part of the array is initialized only if $(D_PARAM Init)
|
* allocated part of the array is initialized only if $(D_PARAM Init)
|
||||||
* is set. This function can be trusted only in the data structures that
|
* is set. This function can be trusted only in the data structures that
|
||||||
* can ensure that the array is allocated/rellocated/deallocated with the
|
* can ensure that the array is allocated/rellocated/deallocated with the
|
||||||
@ -151,34 +153,27 @@ size_t alignedSize(in size_t size, in size_t alignment = 8) pure nothrow @safe @
|
|||||||
* Params:
|
* Params:
|
||||||
* T = Element type of the array being created.
|
* T = Element type of the array being created.
|
||||||
* Init = If should be initialized.
|
* Init = If should be initialized.
|
||||||
* Throws = If $(D_PSYMBOL OutOfMemoryError) should be throwsn.
|
|
||||||
* allocator = The allocator used for getting memory.
|
* allocator = The allocator used for getting memory.
|
||||||
* array = A reference to the array being changed.
|
* array = A reference to the array being changed.
|
||||||
* length = New array length.
|
* length = New array length.
|
||||||
*
|
*
|
||||||
* Returns: $(D_KEYWORD true) upon success, $(D_KEYWORD false) if memory could
|
* Returns: $(D_PARAM array).
|
||||||
* not be reallocated. In the latter
|
|
||||||
*/
|
*/
|
||||||
package(tanya) bool resize(T,
|
package(tanya) T[] resize(T,
|
||||||
bool Init = true,
|
bool Init = true)
|
||||||
bool Throws = true)
|
|
||||||
(shared Allocator allocator,
|
(shared Allocator allocator,
|
||||||
ref T[] array,
|
auto ref T[] array,
|
||||||
in size_t length) @trusted
|
const size_t length) @trusted
|
||||||
{
|
{
|
||||||
void[] buf = array;
|
void[] buf = array;
|
||||||
static if (Init)
|
static if (Init)
|
||||||
{
|
{
|
||||||
immutable oldLength = array.length;
|
const oldLength = array.length;
|
||||||
}
|
}
|
||||||
if (!allocator.reallocate(buf, length * T.sizeof))
|
if (!allocator.reallocate(buf, length * T.sizeof))
|
||||||
{
|
|
||||||
static if (Throws)
|
|
||||||
{
|
{
|
||||||
onOutOfMemoryError;
|
onOutOfMemoryError;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Casting from void[] is unsafe, but we know we cast to the original type.
|
// Casting from void[] is unsafe, but we know we cast to the original type.
|
||||||
array = cast(T[]) buf;
|
array = cast(T[]) buf;
|
||||||
|
|
||||||
@ -189,25 +184,23 @@ package(tanya) bool resize(T,
|
|||||||
array[oldLength .. $] = T.init;
|
array[oldLength .. $] = T.init;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return array;
|
||||||
}
|
}
|
||||||
package(tanya) alias resizeArray = resize;
|
|
||||||
|
|
||||||
///
|
private unittest
|
||||||
unittest
|
|
||||||
{
|
{
|
||||||
int[] p;
|
int[] p;
|
||||||
|
|
||||||
defaultAllocator.resizeArray(p, 20);
|
p = defaultAllocator.resize(p, 20);
|
||||||
assert(p.length == 20);
|
assert(p.length == 20);
|
||||||
|
|
||||||
defaultAllocator.resizeArray(p, 30);
|
p = defaultAllocator.resize(p, 30);
|
||||||
assert(p.length == 30);
|
assert(p.length == 30);
|
||||||
|
|
||||||
defaultAllocator.resizeArray(p, 10);
|
p = defaultAllocator.resize(p, 10);
|
||||||
assert(p.length == 10);
|
assert(p.length == 10);
|
||||||
|
|
||||||
defaultAllocator.resizeArray(p, 0);
|
p = defaultAllocator.resize(p, 0);
|
||||||
assert(p is null);
|
assert(p is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user