Chapter 10. Working with Lists

Table of Contents
10.1. The List Class
10.2. Basic List Syntax
10.3. Lists: Managing Objects in the Model Swarm
10.4. Lists: Passing Information Among Levels in a Swarm Model
10.5. Lists: Organizing Repetitive Chores inside Objects

Programmers who have worked primarily in non-object-oriented languages like Fortran, Pascal, or C, are sometimes perplexed at the way Swarm programs manage repetitive tasks. While there is no hard rule that iterative chores have to be managed in a certain way, one will find a fairly common approach that uses a Swarm object called List. Since this usage is both widespread in Swarm and different from the usual strategy in other languages, it deserves some discussion.

10.1. The List Class

This section is not intended as a comprehensive review of Swarm's Collections library. That job is left to a later chapter. Instead, the purpose of this section is to introduce some popular usages of the List class and discuss the implications for simulation modeling.

The concept of a "linked list" may be familiar to C programmers. The motivating idea of a linked list is that one can develop a collection of entities by defining a series of structures that refer to each other. The first structure contains not only the information needed to describe a single entity a pointer to the next entity in the series. A linked list has a major advantage that it is flexible. Unlike an array that is allocated to allow N members, a linked list can grow indefinitely as members are added and it can shrink as members are deleted. The complicated problem is to make sure that the structures always correctly refer to each other as entities are added and removed.

The Swarm Collections library provides a protocol called List that provides the swarm program with all of the benefits of a linked list and none of the hassles. The usage of List objects seems rather informal. A list can be told to add an object at the end or the beginning, or to retrieve an object that is in a certain position in a list. Working together with another Swarm protocol, the Index protocol, the List object has a great deal of power and many uses. It should be noted that Swarm provides other, more structured "container" classes as well (Array, OrderedSet, Map, and so forth), but a treatment of them is left to a later chapter.