yage.core.array
$(DDOC_SECTIONS
Authors:
Eric Poggel
License:
LGPL v3
Array operation functions that are either not part of,
or improved from the standard library.
Note that they can also be accessed by arrayname.function().
Example:
// Removing
int[] numbers = [0, 1, 2];
numbers.remove(0); // numbers is now [0, 1];
- void
addSorted
(T)(ref T[] array, T value, bool increasing = true);
void
addSorted
(T, K)(ref T[] array, T value, bool increasing, K delegate(T elem) getKey, int max_length = (int).max);
- Add an element to an already sorted array, maintaining the same sort order.
Deprecated:
replace this with a Heap.
Params:
array |
The array to use. |
value |
Value to add. |
increasing |
The elements are stored in increasing order. |
getKey |
A function to return a key of type K for each element.
K must be either a primitive type or a type that impelments opCmp.
Only required for arrays of classes and structs. |
- T
amax
(T)(T[] array);
T
amax
(T, K)(T[] array, K delegate(T elem) getKey);
T
amin
(T)(T[] array);
T
amin
(T, K)(T[] array, K delegate(T elem) getKey);
- Return the element with the minimum or maximum value from an unsorted array.
- bool
sorted
(T)(T[] array, bool increasing = true);
bool
sorted
(T, K)(T[] array, bool increasing, K delegate(T elem) getKey);
- .dup for associative arrays.
Params:
deep |
If true
, child dynamic and associative arrays will also be dup'd.
T[K] dup(T, K)(T[K] array, bool deep=false
)
{ T[K] result;
foreach (k, v; array)
{ if (!deep)
result[k] = v;
else
{ static if (isAssocArrayType!(T))
result[k] = dup(v);
else static if (isDynamicArrayType!(T))
result[k] = v.dup;
else
result[k] = v;
} }
return result;
}
unittest // complete coverage
{
// Test shallow aa copy
{ int[int][char[]] foo;
foo["a"] = [1:1, 2:2];
foo["b"] = [0:0];
auto bar = dup(foo, false
);
bar["a"][1] ++;
assert(foo["a"][1] == 2);
assert(bar["a"][1] == 2);
}
// Test deep aa copy
{ int[int][char[]] foo;
foo["a"] = [1:1, 2:2];
foo["b"] = [0:0];
auto bar = dup(foo, true
);
bar["a"][1] ++;
assert(foo["a"][1] == 1);
assert(bar["a"][1] == 2);
}
// Test deep array copy
{ int[][char[]] foo;
foo["a"] = [1, 2][];
foo["b"] = [0][];
auto bar = dup(foo, true
);
bar["a"][0] ++;
assert(foo["a"][0] == 1);
assert(bar["a"][0] == 2);
}
}
Is the array
sorted
? |
Params:
increasing |
Check for ordering by small to big. |
getKey |
A function to return a key of type K for each element.
Only required for arrays of classes and structs. |
Example:
Timer[] array;
// ... fill array with new Timer() ...
array.sorted(true, (Timer a) { return a.get(); }); // should return true
- void
remove
(T)(ref T[] array, int index, bool ordered = true);
- Remove an element from an array.
This takes constant time, or linear time if ordered is true
.
Params:
array |
The array to use. |
index |
Index of the element to
remove
. |
ordered |
Keep all elements in the same order, at the cost of performance. |
- void
radixSort
(T)(T[] array, bool increasing = true);
void
radixSort
(T, K)(T[] array, bool increasing, K delegate(T elem) getKey, bool signed = true);
- Sort the elements of an array using a radix sort.
This runs in linear time and can sort several million items per second on modern hardware.
Special thanks go to Pierre Terdiman for his essay: Radix Sort Revisited.
Params:
getKey |
A function to return a key of type K for each element.
Only required for arrays of classes and structs. |
signed |
interpret the key data as being signed. Defaults to true
. |
Example:
Timer[] array;
// ... fill array with new Timer() ...
array.radixSort((Timer a) { return a.tell(); }); // sort timers by thier time
- struct
ArrayBuilder
(T);
- Behaves the same as built-in arrays, except about 6x faster with concatenation at the expense of the base pointer
being 4 system words instead of two (16 instead of 8 bytes on a 32-bit system).
Internally, it's implemented similarly to java's StringBuffer.
Use .data to access the underlying array.
- AT
opCall
();
- AT
opCall
(T[] elems);
- size_t
capacity
();
- T[]
data
();
- void
dispose
();
- AT
dup
();
- size_t
length
();
- void
length
(size_t l);
- int
opApply
(int delegate(ref T) dg);
- AT
opAssign
(T[] elem);
- AT
opCat
(T elem);
AT
opCat
(T[] elem);
AT
opCat
(AT elem);
- void
opCatAssign
(T elem);
- void
append
(T elem);
void
opCatAssign
(T[] elem);
void
opCatAssign
(AT elem);
- temporary until opCatAssign always works
- T*
opIndex
(size_t i);
T
opIndexAssign
(T val, size_t i);
- TODO:
This returns a copy, so a[i].b = 3; doesn't work!!
- AT
opSlice
();
AT
opSlice
(size_t start, size_t end);
- AT
opSliceAssign
(T v);
- AT
opSliceAssign
(T v, size_t start, size_t end);
- T*
ptr
();
- AT
reverse
();
- void
splice
(size_t index, size_t remove, T[] insert...);
- Add and remove elements from the array, in-place
Params:
size_t index |
|
size_t remove |
Number of elements to remove, including and after index |
T[] insert |
Element to insert before index, after elements have been removed. |
- AT
sort
();
- char[]
toString
();
|