-- CardAddrs.Mesa
-- written by Paxton.  March 1981
-- last written by Paxton.  April 5, 1981  2:36 PM

-- **** Persistent addressing ****

CardAddrs: DEFINITIONS =
BEGIN

Ref: TYPE = REF Body;
Body: TYPE; -- contains the map from REFs to Cards

Card: TYPE = LONG CARDINAL;

Create: PROC RETURNS [Ref];

PutAddr: PROC [ref: Ref, addr: REF, location: Card]; 
	-- assigns addr to location in ref
	-- ok if addr was previously assigned elsewhere

RemAddr: PROC [ref: Ref, addr: REF];
	-- removes the given addr

GetAddr: PROC [ref: Ref, addr: REF] RETURNS [location: Card];
	-- generates CardAddrs.AddrNotFound if the addr is not in the mapping

AddrNotFound: ERROR;

TryGetAddr: PROC [ref: Ref, addr: REF]
	RETURNS [found: BOOLEAN, location: Card];

MapAddrs: PROC [ref: Ref, action: MapAddrsAction]
	RETURNS [BOOLEAN];
	-- apply the action to each addr&location pair for the ref
	-- returns true if&when an action returns true

MapAddrsAction: TYPE = PROC [addr: REF, location: Card] RETURNS [BOOLEAN];

-- **** Editing Operations for persistent addrs ****

Replace: PROC [ref: Ref, start, len, newlen: Card];
	-- replace chars in [start..start+len) by newlen chars
	-- addrs that are in the replaced section move to start
	-- add (newlen-len) to addrs that are after the replaced section

Move: PROC [ref: Ref, dest, start, len: Card];
	-- dest must not be in (start..start+len)
		-- or get ERROR BadMove

Transpose: PROC [ref: Ref, astart, alen, bstart, blen: Card];
	-- [astart..astart+alen) must not intersect [bstart..bstart+blen)
		-- or get ERROR BadTranspose

-- **** Update Functions for persistent addrs ****

AfterReplace: PROC [initLoc, start, len, newlen: Card] RETURNS [newLoc: Card];

AfterMove: PROC [initLoc, dest, start, len: Card] RETURNS [newLoc: Card];
	-- dest must not be in [start..start+len) or get ERROR BadMove

AfterTranspose: PROC [initLoc, astart, alen, bstart, blen: Card]
	RETURNS [newLoc: Card];
	-- [astart..astart+alen) must not intersect [bstart..bstart+blen)
		-- or get ERROR BadTranspose

BadMove, BadTranspose: ERROR;

Start: PROC; -- for initialization only

END.