RopeEditingAllocImpl.Mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
written by Bill Paxton, March 1981
last edit by Bill Paxton, December 22, 1981 9:56 am
Doug Wyatt, January 17, 1984 4:59:57 pm PST
Michael Plass, February 15, 1985 9:34:05 am PST
DIRECTORY
Rope USING [ROPE],
RopeEditingAlloc USING [],
RopeReader USING [CharsArray, charsPerArray, CharsRope];
RopeEditingAllocImpl:
CEDAR
MONITOR
IMPORTS RopeReader
EXPORTS RopeEditingAlloc
= {
ROPE: TYPE = Rope.ROPE;
Chars: TYPE = REF CharsArray;
CharsArray: TYPE = RopeReader.CharsArray;
charsPerArray: NAT = RopeReader.charsPerArray;
wordsPerArray: NAT = charsPerArray/2;
charsArray: Chars ← NIL; -- current shared array of chars
charsRemaining: NAT ← 0; -- amount of room remaining in current chars array
charsRope: ROPE; -- rope pointing to current chars array
AllocChars: PUBLIC ENTRY PROC [len: NAT]
RETURNS [chars: Chars, start:
NAT, base:
ROPE] = {
returns chars array & starting index to fill with characters
and a rope that points to that substring of the chars array
IF len > charsRemaining
THEN {
IF len > charsPerArray THEN ERROR;
IF charsPerArray-len < charsRemaining
THEN {
-- keep the current one
chars ← NEW[CharsArray]; start ← 0;
base ← RopeReader.CharsRope[chars];
RETURN
};
charsArray ← NEW[CharsArray]; charsRemaining ← charsPerArray;
charsRope ← RopeReader.CharsRope[charsArray]
};
chars ← charsArray;
start ← charsPerArray-charsRemaining;
charsRemaining ← charsRemaining-len;
base ← charsRope;
AllocWords: PUBLIC ENTRY PROC [nwords: NAT]
RETURNS [chars: Chars, start:
NAT, base:
ROPE] = {
ensures that start is even, i.e., start on word boundary
wordsRemaining: NAT;
IF nwords > (wordsRemaining ← charsRemaining/2)
THEN {
IF nwords > wordsPerArray THEN ERROR;
IF wordsPerArray-nwords < wordsRemaining
THEN {
chars ← NEW[CharsArray]; start ← 0;
base ← RopeReader.CharsRope[chars];
RETURN
};
charsArray ← NEW[CharsArray];
wordsRemaining ← wordsPerArray;
charsRope ← RopeReader.CharsRope[charsArray]
};
chars ← charsArray;
start ← (wordsPerArray-wordsRemaining)*2;
charsRemaining ← (wordsRemaining-nwords)*2;
base ← charsRope;
TryAllocAdjacent: PUBLIC ENTRY PROC [base: ROPE, start: NAT, len: NAT]
RETURNS [ok:
BOOLEAN, chars: Chars] = {
IF base=charsRope AND
start=charsPerArray-charsRemaining AND
charsRemaining >= len
THEN {
charsRemaining ← charsRemaining-len; RETURN [TRUE, charsArray]
};
RETURN [FALSE, charsArray]
};