PSArrayImpl.mesa
Copyright Ó 1986, 1987 by Xerox Corporation. All rights reserved.
Doug Wyatt, May 13, 1987 11:49:52 am PDT
PostScript implementation: array operations.
DIRECTORY
PS;
PSArrayImpl: CEDAR PROGRAM
IMPORTS PS
~ BEGIN OPEN PS;
Basic Array operations
ArrayCreate: PUBLIC PROC [size: INT] RETURNS [Array] ~ {
IF size<0 THEN ERROR Error[rangecheck];
IF size IN ArrayIndex THEN {
ref: ArrayRef ~ NEW[ArrayRep[size]];
FOR i: ArrayIndex IN[0..ref.maxLength) DO ref[i] ← null ENDLOOP;
RETURN[[
val: [executable: FALSE, variant: array[access: unlimited, start: 0, length: size]],
ref: ref
]];
}
ELSE ERROR Error[limitcheck];
};
ArrayLength: PUBLIC PROC [array: Array] RETURNS [INT] ~ {
RETURN [array.val.length];
};
ArrayAccess: PUBLIC PROC [array: Array] RETURNS [Access] ~ {
RETURN [array.val.access];
};
ArraySetAccess: PUBLIC PROC [array: Array, access: Access] RETURNS [Array] ~ {
IF array.val.access<access THEN ERROR Error[invalidaccess];
array.val.access ← access;
RETURN [array];
};
ArrayGet: PUBLIC PROC [array: Array, index: INT] RETURNS [Any] ~ {
IF index NOT IN [0..array.val.length) THEN ERROR Error[rangecheck];
RETURN [array.ref[array.val.start+index]];
};
ArrayPut: PUBLIC PROC [array: Array, index: INT, x: Any] ~ {
IF index NOT IN[0..array.val.length) THEN ERROR Error[rangecheck];
array.ref[array.val.start+index] ← x;
};
ArrayGetInterval: PUBLIC PROC [array: Array, index, count: INT] RETURNS [Array] ~ {
IF index NOT IN [0..array.val.length] THEN ERROR Error[rangecheck];
IF count NOT IN [0..(array.val.length-index)] THEN ERROR Error[rangecheck];
RETURN[[
val: [executable: array.val.executable, variant: array[
access: array.val.access, start: array.val.start+index, length: count]],
ref: array.ref
]];
};
Other Array operations
ArrayPutInterval: PUBLIC PROC [array: Array, index: INT, interval: Array] ~ {
subarray: Array ~ ArrayGetInterval[array, index, ArrayLength[interval]];
FOR i: INT IN[0..ArrayLength[subarray]) DO
ArrayPut[subarray, i, ArrayGet[interval, i]];
ENDLOOP;
};
ArrayCopy: PUBLIC PROC [array1, array2: Array] RETURNS [Array] ~ {
subarray2: Array ~ ArrayGetInterval[array2, 0, ArrayLength[array1]];
ArrayPutInterval[subarray2, 0, array1];
RETURN [subarray2];
};
ArrayForAll: PUBLIC PROC [array: Array, action: PROC [Any]] ~ {
FOR index: INT IN [0..ArrayLength[array]) DO
action[ArrayGet[array, index]];
ENDLOOP;
};
END.