next up previous index
Next: Unordered collections Up: Collections Previous: Basic collections   Index

Removing and adding elements

In removable.diesel:

extend module Stdlib;
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.

module RemovableCollection;
abstract class removable_collection[T] isa collection_exactly[T];
fun remove(:removable_collection[`T], x:T, if_absent:&():void):void;
fun remove(c:removable_collection[`T], x:T):void;
fun remove_if(c:removable_collection[`T], pred:&(T):bool):int;
fun remove_any(c:removable_collection[`T], if_empty:&():`S):T|S;
fun remove_any(c:removable_collection[`T]):T;
fun remove_all(c:removable_collection[`T]):void;
end module RemovableCollection;
In extensible.diesel:

extend module Stdlib;
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.

module FunctionallyExtensibleCollection;
abstract class functionally_extensible_collection[T]
                                                isa collection_exactly[T];
  fun add_functional(:functionally_extensible_collection[`T], x:T
                            ):functionally_extensible_collection[T];
end module FunctionallyExtensibleCollection;
module FunctionallyExtensibleRemovableCollection;
A blend of functionally_extensible_collection and removable_collection.

abstract class functionally_extensible_removable_collection[T]
        isa removable_collection[T], functionally_extensible_collection[T];
end module FunctionallyExtensibleRemovableCollection;
An 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.

module ExtensibleCollection;
abstract class extensible_collection[T]
        isa functionally_extensible_removable_collection[T];
fun add(:extensible_collection[`T], x:T):void;
fun add_nonmember(c:extensible_collection[`T], x:T):void;
fun add_all(c:extensible_collection[`T], xs:collection[T]):void;
fun add_all_nonmember(c:extensible_collection[`T],
                             xs:collection[T]):void;
copy_empty produces a new, empty extensible collection of a similar kind to the receiver collection

fun copy_empty(:extensible_collection[`T]):extensible_collection[T];
end module ExtensibleCollection;


next up previous index
Next: Unordered collections Up: Collections Previous: Basic collections   Index

Cecil/Vortex Project