12 Streams or Infinite Arrays

 12.1 The Representation of DataStream
 12.2 Constructor Functions of DataStream
 12.3 Selector Functions
 12.4 Access Functions
 12.5 Destructive Functions
 12.6 DataStream Modification

This file implements a stream data structure which is tested in the file test/stream.as.nw

A stream models a function T for some type T where it caches all the elements up to the index of the last element that has ever been accessed. In fact, a DataStream can be seen as an infinite array.

The domain DataStream resembles in many aspects the functionality of the Stream domain from the Aldor library, but we had to implement our own stream type since the functionality of the Aldor stream was not sufficient and could not be extended without knowing the internal representation of Stream.

Since we are going to document our DataStream more extensively, it might even become a replacement for the Aldor Stream.

In order to make the description of a DataStream finite, one can think of a DataStream as a pair (a,g) where a is a finite array and g is a function that computes the next elements.

Like the domain Stream from the Aldor library, DataStream handles efficiently infinite streams that are constant from some point on by storing the constant value only once.

384* 13+   365  429
-------------------------------------------------------------------
----
---- Combinat
---- Copyright (C) Ralf Hemmecke <ralf@hemmecke.de>
---- svn co svn://svn.risc.uni-linz.ac.at/hemmecke/combinat/
----
-------------------------------------------------------------------

#include "combinat"
dom: DataStream 386

Type Constructor

DataStream

Usage

T == Integer
s: DataStream T := stream 0;
s := stream(generator [1, 3]);
g: Generator T == generate for i in 2.. repeat yield i;
s := stream g;

Description

Implements an infinite array.

The domain DataStream can be abstractly seen as an infinite array. However, to make this data structure finite, it is more appropriate to view it as a pair (a,g) of an Array(T) and a Generator(T).

Like Array, DataStream is 0-based.

386dom: DataStream 386  (384)
DataStream(T: Type): with {
        exports: DataStream 392a
} == add {
        representation: DataStream 388
        import from Rep, I, T;
        macro X == rep x;
        implementation: DataStream 392b
}

Defines:
DataStream, used in chunks 86b, 93, 111b, 124, 125a, 132, 133, 142, 143, 160, 162, 163a, 220, 221, 245, 249a, 253a, 262, 267a, 271b, 273a, 276, 278b, 318, 319b, 337, 341b, 346–48, 354a, 361, 363, 657, 676, 685, 695–98, 700, 703–7, 710–17, 719, 721, 723, and 725.

Uses I 47.

Exports of DataStream

stream: T -> % Construct a constant stream.

stream: Generator T -> % Construct a stream.

stream: (Generator T, T) -> % Construct a stream.

stream: ((MachineInteger, %) -> T) -> % Construct a stream.

new: () -> % Construct an uninitialized stream.

#: % -> MachineInteger Number of computed elements.

data: % -> PrimitiveArray T Returns the array of computed elements.

constant?: % -> Boolean Returns true if the stream is known to be eventually constant.

generator: % -> Generator T Generate all elements of the stream.

elements: (%, startFromIndex: MachineInteger == 0) -> Generator T Generate all elements of the stream until the last elements repeats.

apply: (%, MachineInteger) -> T Access an entry of the stream.

set!: (%, MachineInteger, T) -> T Destructively modify an entry of a stream.

set!: (%, Generator T) -> () Construct a stream using an old structure.

map: (T -> T) -> % -> % Maps a function on a stream.