Struct Collection
Array-based collection, that provides a set of useful operations and handles object ownership.
struct Collection(T, bool ownItems = false)
if (is(T == class) || is(T == interface));
For class objects only.
Retains item order during add/remove operations. When instantiated with ownItems = true
,
the container will destroy items on its destruction and on shrinking.
Fields
Type | Name | Description |
size_t
|
len
|
|
T[]
|
list
|
|
Properties
Type | Name | Description |
size_t
|
capacity [get]
|
Returns currently allocated size (>= count )
|
size_t
|
count [get]
|
Number of items in the collection
|
bool
|
empty [get]
|
True if there are no items in the collection
|
Methods
Name | Description |
append
(item)
|
Append item to the end of collection
|
back
()
|
Pick the last item. Fails if no items
|
clear
(destroyItems)
|
Remove all items and optionally destroy them
|
front
()
|
Pick the first item. Fails if no items
|
indexOf
(item)
|
Returns index of the first occurrence of item, returns -1 if not found
|
indexOf
(id)
|
Find child index for item by id, returns -1 if not found
|
insert
(index, item)
|
Insert item before specified position
|
opApply
(callback)
|
Foreach support
|
opBinaryRight
(item)
|
Support for in operator (linear search)
|
opIndex
(index)
|
|
opIndex
()
|
|
popBack
()
|
Remove the last item and return it. Fails if no items
|
popFront
()
|
Remove the first item and return it. Fails if no items
|
pushBack
(item)
|
Insert item at the end of collection
|
pushFront
(item)
|
Insert item at the beginning of collection
|
remove
(index)
|
Remove single item and return it
|
removeValue
(value)
|
Remove single item by value. Returns true if item was found and removed
|
replace
(index, item)
|
Replace one item with another by index. Returns removed item.
|
replace
(oldItem, newItem)
|
Replace one item with another. Appends if not found
|
reserve
(count)
|
Make sure the capacity is at least count items
(e.g. to reserve big space to avoid multiple reallocations)
|
resize
(count)
|
Expand or shrink the collection, possibly destroying items or adding null ones
|
unsafe_slice
()
|
Get a mutable slice of items. Don't try to resize it!
|
Example
// add
Collection!Widget widgets;
widgets ~= new Widget("id1");
widgets ~= new Widget("id2");
Widget w3 = new Widget("id3");
widgets ~= w3;
// remove by index
widgets.remove(1);
// iterate
foreach (w; widgets)
writeln("widget: ", w.id);
// remove by value
widgets -= w3;
writeln(widgets[0].id);