OEP

169

Title

RESIZE instrinsic PROC

Summary

Provide a simple way of resizing mobile arrays

Owner

Adam Sampson <ats@offog.org>

Status

Accepted

Date-Proposed

2008-02-05

Date-Accepted

2008-02-22

Keywords

intrinsic mobile

In code that uses mobile arrays (that is, most code I write), I often find myself wanting the following PROC:

--* Resize [@code array] to be [@code new.size] elements long.
PROC resize (MOBILE []T array, VAL INT new.size)
  INITIAL MOBILE []T new.array IS MOBILE [new.size]T:
  SEQ
    SEQ i = 0 FOR MIN (SIZE array, new.size)
      new.array[i] := array[i]
    array := new.array
:

(Typically I'm using it along with a counter variable to make an array long enough for an item I'm about to add to it, doubling the size of the array if it's not big enough; this is common enough that I'm half tempted to suggest it as an intrinsic too, with the signature (MOBILE []T array, INT used, VAL INT new.used).)

That PROC can't be written in a useful way in occam-pi at the moment, because we don't have type variables. In addition, it's not the most efficient way to implement the behaviour I want; since many memory allocators round up allocation sizes, the array may already be sitting in a chunk of memory that's big enough for the new size, in which case only the recorded size needs to be changed.

I'd suggest providing RESIZE as an intrinsic PROC, which would make it slightly easier to lift the type restriction. (This has now been implemented as RESIZE.MOBILE.ARRAY.1D.)

OEP/169 (last edited 2008-02-27 10:56:05 by ats)