Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Type Sample Collection

DEFINITION MODULE SampleCollection ("A-Type");

(* Example library for a collection using operator bindings *)

TYPE SampleCollection = OPAQUE;


(* key and value type *)

TYPE KeyType IS CARDINAL; (* arbitrary type *)

TYPE ValueType = ARRAY 255 OF CHAR; (* arbitrary type *)


(* null key *)

CONST [NIL] nullKey = 0; 


(* allocator *)

PROCEDURE [NEW] new ( VAR dict : SampleCollection );
(* Allocates a new collection and passes a reference to it back in dict *)


(* accessor *)

PROCEDURE [.] valueForKey ( dict : SampleCollection; key : KeyType; VAR found : BOOLEAN ) : ValueType;
(* Searches for key in dict, if found passes TRUE in found and returns its value, otherwise passes FALSE *)


(* mutators *)

PROCEDURE [!] storeValue ( dict : SampleCollection; key : KeyType; value : ValueType );
(* Stores value for key in dict, overwrites the previous value if key is already present in dict *)

PROCEDURE [-] removeValue ( dict : SampleCollection; key : KeyType );
(* Removes the entry whose key is key from dict *)


(* membership test *)

PROCEDURE [IN] keyExists ( dict : SampleCollection; key : KeyType ) : BOOLEAN;
(* Returns TRUE if key is present in dict, otherwise returns FALSE *)


(* counter *)

PROCEDURE [COUNT] entryCount ( dict : SampleCollection ) : LONGCARD;
(* Returns the number of entries stored in dict *)


(* iterator *)

PROCEDURE [FOR] nextEntry ( dict : SampleCollection; VAR key : KeyType; VAR value : ValueType );
(* Finds the successor key of key and passes its key/value pair back in key and value.
   If the null key is passed in then the first key/value pair is passed back in key and value.
   If no successor key is found then the null key is passed back in key and value is undefined. *)


(* destructor *)

PROCEDURE [DISPOSE] dispose ( VAR dict : SampleCollection );
(* Deallocates dict and passes NIL back in dict *)

END SampleCollection.