The most frequently used kinds of collections are the List, Map and Array protocols. They have some elements in common. They all comply with the Collection protocol, most importantly, which means they have methods with which items can be added, retrieved, and removed. Also, each can be used to create an index object, which can make management of lists an easier chore.
It is very important to note that these collections are primarily intended to manage objects, not integers or floating point values. [1] If you need an array of integers (or floats or chars or whatever else), just use an ordinary C array. When it is necessary to use strings, integers, or floating point values in a Swarm collection, there are two workarounds. One is to use typecasting to put those other values inside the space allocated for a pointer to an object. For various reasons, that approach is not as desirable as the alternative of creating "wrapper objects" that can contain those other types of variables. In short, while typecasting will often work, it is generally a better strategy is to design more carefully the objects you want to keep in Swarm collections and use recommended procedures for retrieving them.
Some commands that work in Swarm collections are:
getCount: Returns the number of members in the collection
atOffset: i: Retrieves the ith member of the collection
atOffset:i put: obj: Inserts obj at location i
contains: obj Returns 1 if obj is member
remove: obj: Removes object obj
removeAll: Removes all objects from collection, but leaves the objects in memory
deleteAll: Removes all objects from collection and deletes them from memory
forEach:M(message): Sends message to all members
This chapter does not discuss the Swarm Set protocol because, at the current time, it has no functionality beyond the regular Swarm List protocol.
[1] | Type casting for both storage of variables in collections as well as usage of non-object values for keys was discussed in the original Swarm design. Roger Burkhart defined a protocol MemberType which would have been adopted by the Collection protocol: "The MemberType option may be used to declare the type of member which a collection contains. Its value must be an object having one of the ValueType types defined in defobj. (..Currently no ValueType objects are implemented, so MemberType is not supported.)" This protocol was to have two methods: -setMemberType: aDataType and -getMemberType. |