Table of Contents

Introduction

One of the major additions to Ada 2005 is the container library. This library enables the Ada developer to manipulate data structures such as doubly linked lists, maps, sets and vectors. This page will show how the [http://www.adaic.com/standards/05rat/html/Rat-8-2.html#I1386 Ada.Containers.Doubly_Linked_Lists] library work.

Theory

A doubly linked list is merely a linked list where each element is not only linked to the next, but also the previous. For more information, see http://en.wikipedia.org/wiki/Doubly_linked_list

Example

This is example usage is from an existing project.

worm.ads

This file represents the specification of a worm.

with Ada.Containers.Doubly_Linked_Lists;
with Wormlevel, Protocol; --, Handlers;                                                                                                                                                       
 
package Worm is
   use Wormlevel, Protocol; --, Handlers;                                                                                                                                                               
 
   package Worm_Position_Container is new Ada.Containers.Doubly_Linked_Lists(Position);
   use Worm_Position_Container;
 
   type Worm_State is (Alive,Dead,Observing);
 
   type Worm_Type is
      record
         Worm_Body : List;
         Direction : Course  := North;
         Points    : Natural := 0;
         Number    : Positive;
         State     : Worm_State := Alive;
         Full      : Boolean := False;
      end record;
   type Worm_Type_Access is access Worm_Type;
   procedure Update_Worm (Worm : in Worm_Type_Access;
                          Lvl  : in Level_Access);
   procedure Turn_Left   (Worm : in Worm_Type_Access);
   procedure Turn_Right  (Worm : in Worm_Type_Access);
   procedure Kill        (Worm : in Worm_Type_Access);
end Worm;

As you can see, Ada.Containers.Doubly_Linked_Lists is instantiated with a type called “Position”. This type represents a tile type in a 2-dimensional array.

The worm body is now represented by a doubly linked list - meaning we can loop through each element in either direction, by using the Next and Previous functions.

For those interested, the game can be found at github


Navigation