In removable.diesel:
A
removable_collection
supports in-place removal of elements. The remove
function removes one element equal to its second argument,
or raises an error if not found (also defined is a variation that
takes a closure to handle the not-found case). The remove_if
function removes all elements that satisfy the test, and
returns the number of elements removed. The remove_any
function removes and returns a single arbitrary element of the collection
or raises an error if the collection is empty (again, a variation on
remove_any
passes a closure to handle this case in a client-defined
manner). The remove_all
function removes all elements of the
collection. Subclasses need to implement these functions; a default
remove_all
is provided, but it is likely that subclasses can provide a much
more efficient implementation.
The
functionally_extensible_collection
class generalizes collections that
are extensible in place (i.e., add mutates) and those that are extensible,
but not in place (i.e., add returns a new collection). The
add_functional
operation adds an element to the collection (in
some unspecified location), returning a collection with the element
added. This returned collection may either be the receiver
collection (if the add is performed in-place) or some new
collection. Since the receiver collection may or may not be changed
and may or may not contain the new element, the caller should use
the returned value (and should not use the argument to
add_functional
after the call).
The weird name of the collection is intended to suggest a functional language where add returns a new collection.
functionally_extensible_collection
and
removable_collection
.
extensible_collection
supports adding new elements in-place, as well as
removing elements. The add function adds a new element to the collection
(in some unspecified place). The add_nonmember
function can be used if the
element being added is known not to be in the collection already. Its
effect is that same as that of add for collections which allow duplicates,
but it may be faster than a generic add for collections that do not permit
duplicates (such as set and its subclasses). The add_all
and
add_all_nonmember
functions support adding all the elements of some other
collection to the receiver collection.
copy_empty
produces a new, empty extensible collection of a
similar kind to the receiver collection