Containers

Classes NodeList and EdgeList, the typed descendants of class YList, are the most frequently encountered yFiles data structures to store graph elements. Figure 7.6, “yFiles graph element containers” shows their relationship.

Figure 7.6. yFiles graph element containers

The yFiles graph element containers.

Common to all lists is their support for the methods from interface Collection via the implementation in class YList. Beyond this implementation, class YList provides advanced list access and manipulation services which strongly rely on the possibilities offered by class ListCell.

High-Level Features

In addition to the methods from interface Collection there are several further methods offered. For instance, it is possible to add elements explicitly to the front or the back of a list. Also, removing elements from the list is possible from both the front and the back. Note that the method names for removing elements from either end of the list resemble those of abstract data type stack.

Other methods offer list sorting, reversing a list, or the splicing of two lists into one.

Example 7.8. Adding elements to and removing elements from a list

// 'list' is of type com.yworks.yfiles.base.YList.

// Add new elements to either end of the list.
list.addFirst("I am the first node!");
list.addLast("I am the last node!");

// Remove elements from either end of the list.
list.pop();
list.popLast();

To iterate over the elements of the list it provides a YCursor (respectively NodeCursor and EdgeCursor with classes NodeList and EdgeList). See also the discussion in the section called “Iteration Mechanisms”.

Example 7.9, “Efficiently removing elements from a list using a cursor” shows how to efficiently remove an element from a list using a cursor. The cursor directly points to the element, there is no need to search the list.

Example 7.9. Efficiently removing elements from a list using a cursor

// 'list' is of type com.yworks.yfiles.base.YList.

// Remove unwanted objects from the list.
for (var c:YCursor = list.cursor(); c.ok(); c.next()) {
  if (isUnwanted(c.current())) {
    list.removeAt(c);
  }
}

Low-Level Features

Class ListCell is the building block of the doubly linked list structure. It knows both its predecessor and its successor, and provides read/write behavior for the actual data it is holding.

Example 7.10. Using class ListCell

// 'list' is of type com.yworks.yfiles.base.YList.

// Get the first cell of the list.
var firstCell:ListCell = list.firstCell();

// Get the actual data the cell is holding.
var obj:Object = firstCell.info;

// Change the actual data.
firstCell.info = "Updated Data.";

Using an instance of type ListCell, for example, it is possible to have fast access to the preceeding, respectively succeeding cell from the containing list, or to insert new cells relatively to already contained ones. By way of the successors (predecessors) of a ListCell instance, direct iteration over the elements of a list is possible.

Example 7.11. Directly iterating over the cells of a YList

// 'list' is of type com.yworks.yfiles.base.YList.

// Directly (backward) iterate over the list.
for (var lc:ListCell = list.lastCell(); lc != null; lc = list.predCell(lc)) {
  if (conditionIsSatisfied(lc.info)) {
    // Insert a new cell with given data relatively to the held reference cell.
    // The newly allocated cell will be returned.
    var newCell:ListCell = list.insertBefore("Prepended Data.", lc);
  }
}