4 Collections
(require rebellion/collection) | package: rebellion |
Rebellion provides several different types of collections. A collection is a container for values, like a list or a hash table. Different types of collections have different properties, for example sets are unordered and have no duplicate elements. For advice on how to choose the right collection type, see Choosing Collections.
Collections may be mutable, immutable, or unmodifiable. A mutable collection can be changed and updated by clients. Immutable collections never change: instead, operations on immutable collections return modified copies of the original collection. Unmodifiable collections lie between these two extremes. An unmodifiable collection cannot be changed by clients, but does not promise that it will not change on its own.
Different collections need not necessarily have different state. A collection view is a collection whose implementation defers to the state of some other collection. Collection views can be used for a wide variety of purposes, including efficiently operating on a subset of a collection, constructing a thread-safe wrapper around a collection meant to be shared between threads, or adapting one collection to the interface of another. Collection views come in a few main varieties:
A read-through view reflects any changes in the underlying collection: updates to the viewed collection cause the view to change.
A write-through view is a read-through view that is mutable, and allows clients of the view to mutate the underlying collection by mutating the view.
An unmodifiable view is a read-through view that does not allow clients to mutate the view directly. Unmodifiable views are useful for sharing access to mutable state with clients who should be allowed to read that state, but not change it. Unmodifiable is not the same as immutable! An unmodifiable view cannot be changed directly, but may change indirectly due to changes in the underlying collection.
A synchronized view is a write-through view that guards access to the underlying collection using a read-write lock. Synchronized views allow thread-safe access to any mutable collection, provided the collection is accessed exclusively through the synchronized view.