-
-
Notifications
You must be signed in to change notification settings - Fork 9
List
List represents the type of a list.
| Type | Size | Possible Values | Memory Management Model | File |
|---|---|---|---|---|
List |
32 bytes | Lists of a Single Type | Ownership | 2.7/List.adept |
struct <$T> List (items *$T, length, capacity usize, ownership Ownership)
where
$T is any valid type
| Name | Type | Description |
|---|---|---|
items |
*$T |
Pointer to raw array of items |
length |
usize |
Length of items in units |
capacity |
usize |
Capacity of items in units |
ownership |
Ownership |
Whether this list is responsible for freeing items
|
func __defer__(this *<$T> List)func __pass__(list POD <$T> List) <$T> Listfunc __array__(this *<$T> List) *$Tfunc __length__(this *<$T> List) usizefunc __access__(this *<$T> List, index usize) *$Tfunc __assign__(this *<$T> List, other POD <$T> List)implicit func __as__(initializer <$T> InitializerList) <$T> Listimplicit func __as__(initializer <long> InitializerList) <int> Listimplicit func __as__(initializer <double> InitializerList) <float> List
Lists can be created from initializer lists:
food <String> List = {"Spaghetti", "Pizza", "Garlic Bread"}
board <<int> List> List = {
{0, 0, 0, 0} as <int> List,
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
}
Lists are automatically freed when they run out of scope.
import basics
func main {
names <String> List
names.add("Isaac")
names.add("Andy")
names.add("Zack")
names.add("Ellie")
// names is freed
}
In order to pass lists around to other scopes, you can use the commit() method. my_list.commit() gives up any ownership held by my_list and returns a copy with the original's ownership:
import basics
func main {
outlives <String> List = getList()
// outlives is freed
}
func getList() <String> List {
shortlived_list <String> List
shortlived_list.add("Apples")
shortlived_list.add("Bananas")
shortlived_list.add("Oranges")
return shortlived_list.commit() // Transfer ownership
}
- Lists are freed when they go out of scope
- Lists are freed when inside a value that goes out of scope
- Lists can transfer scopes using
new_scoped_list = old_scoped_list.commit() - Lists are freed when they are returned and never assigned
- Lists reference the data of other Lists when assigned to them
- Lists can point to specific parts of other Lists
- Variables/Parameters marked as
PODwill not have__pass__and__defer__calls automatically made - When
add()ed to things like otherLists, a reference will be used by default - To commit ownership of a list to another
List, you must uselist_of_lists.add(list.commit())
-
func List(items *$T, length usize, ownership Ownership = Ownership::REFERENCE) <$T> ListConstructs a list from individual components.
-
func List(items *$T, length, capacity usize, ownership Ownership = Ownership::REFERENCE) <$T> ListConstructs a list from individual components.
-
func add(this *<$T> List, item POD $~T) voidAdds an item to a list.
-
func add(this *<$T> List) *$TAdds a blank zero-initialized item to a list and returns a pointer to the new item.
-
func insert(this *<$T> List, index usize) *$TInserts a blank zero-initialized item into a list and returns a pointer to the new item.
-
func insert(this *<$T> List, index usize, item POD $~T) voidInserts an item into a list.
-
func append(this *<$T> List, item POD $~T) voidAppends an item to a list.
-
func prepend(this *<$T> List, item POD $~T) voidPrepends an item to a list.
-
func remove(this *<$T> List, index usize) voidRemoves an item from a list.
-
func reserve(this *<$T> List, count usize) voidEnsures a list can hold at least
countitems before having to expand. -
func get(this *<$T> List, index usize) $TGets an item from a list.
-
func getPointer(this *<$T> List, index usize) *$TGets a pointer to an item in a list. The pointer may be invalidated when the list is modified.
-
func clear(this *<$T> List)Clears a list.
-
func empty(this *<$T> List) boolReturns whether
this.length == 0 -
func commit(this *<$T> List) <$T> ListChanges the ownership of a list to be
Ownership::REFERENCE. If the list previously had ownership, then a list value will be returned withOwnership::GIVEN, otherwise the original list will be returned. This method is used for transferring ownership of internal array data of lists to other lists. -
func donate(this *<$T> List) <$T> ListIf the list has ownership, then the list's ownership will be changed to
Ownership::DONORand a list value will be returned withOwnership::GIVEN, otherwise the original list will be returned. This method is used for transferring ownership of internal array data of lists to other lists. If ownership was transferred, then the subject value will be completely invalidated and will be unable to be used afterwards. -
func give() <$T> ListLike
commit(), except requires ownership. Not having ownership will cause a runtime error. -
func reverse(this *<$T> List) voidReverses a list.
-
func reduce(this *<$T> List) voidRemoves the last item from a list.
-
func reduce(this *<$T> List, amount usize) voidRemoves the last
amountitems from a list. -
func first(this *<$T> List) *$TReturns a pointer to the first item in the list. Will return
nullif the list is empty. -
func last(this *<$T> List) *$TReturns a pointer to the last item in the list. Will return
nullif the list is empty. -
func contains(this *<$T> List, value $~T) boolReturns whether a list contains an item that is equal to
value. -
func map(this *<$T> List, function func($~T) $S) <$S> ListApplies
functionto every item in a list to form another list.
#default List_bounds_checks true
#default List_error_with_type __typeinfo__
#default List_ownership_checks true
#default List_disable_safety false
#default List_preparation_checks false
#default List_experimental false
#default List_warn_experimental true
#default List_error_on_donor_usage true