-- BcdTab.Mesa -- last edited by Satterthwaite on November 23, 1982 10:09 am -- last edited by Lewis on 10-Dec-80 20:11:41 DIRECTORY Alloc: TYPE USING [AddNotify, DropNotify, Handle, Notifier, Trim, Words], BcdDefs: TYPE USING [httype, PackedString, sstype], Environment: TYPE USING [charsPerWord], Inline: TYPE USING [BITAND, BITXOR], Strings: TYPE USING [ AppendChar, AppendSubString, EqualSubStrings, EquivalentSubStrings, String, SubString, SubStringDescriptor], Symbols: TYPE USING [HTIndex, HTNull, HTRecord, HVIndex, hvLength], SymbolOps: TYPE USING [], Table: TYPE USING [Base, Index]; BcdTab: PROGRAM IMPORTS Alloc, Inline, Strings EXPORTS SymbolOps={ OPEN Symbols; SubString: TYPE~Strings.SubString; SubStringDescriptor: TYPE~Strings.SubStringDescriptor; -- tables defining the current symbol table table: Alloc.Handle; hashVec: LONG POINTER TO ARRAY HVIndex OF HTIndex; ht: LONG DESCRIPTOR FOR ARRAY --HTIndex-- OF HTRecord; htb: Table.Base; -- hash table ssb: LONG POINTER TO BcdDefs.PackedString; -- id string UpdateBases: Alloc.Notifier~{ OPEN BcdDefs; htb _ base[httype]; ssb _ LOOPHOLE[base[sstype]]; hashVec _ htb; ht _ DESCRIPTOR[htb + hashVec^.LENGTH*HTIndex.SIZE, ht.LENGTH]}; AllocateHash: PROC RETURNS [hti: HTIndex]~{ next: Table.Index~table.Words[BcdDefs.httype, HTRecord.SIZE]; hti _ ht.LENGTH; ht _ DESCRIPTOR[ht.BASE, ht.LENGTH+1]; ht[hti] _ HTRecord[link~HTNull, offset~ssb.string.length+1]; RETURN [hti-1]}; -- variables for building the symbol string ssw: Table.Index; initialized: BOOL_FALSE; Initialize: PUBLIC PROC [ownTable: Alloc.Handle]~{ IF initialized THEN Finalize[]; hashVec _ NIL; table _ ownTable; table.AddNotify[UpdateBases]; Reset[]; initialized _ TRUE}; Finalize: PUBLIC PROC~{ table.DropNotify[UpdateBases]; table _ NIL; initialized _ FALSE}; Reset: PUBLIC PROC~{ nullSS: SubStringDescriptor_[base~"null"L, offset~0, length~0]; table.Trim[BcdDefs.sstype, 0]; table.Trim[BcdDefs.httype, 0]; [] _ table.Words[BcdDefs.httype, hvLength*HTIndex.SIZE]; hashVec _ htb; hashVec^ _ ALL[HTNull]; ht _ DESCRIPTOR[htb + hashVec^.LENGTH*HTIndex.SIZE, 0]; ssw _ table.Words[BcdDefs.sstype, StringBody.SIZE] + StringBody.SIZE; ssb.string _ [length~0, maxlength~0, text~]; [] _ AllocateHash[]; IF EnterString[@nullSS] # HTNull THEN ERROR}; -- hash entry creation EnterString: PUBLIC PROC [s: SubString] RETURNS [hti: HTIndex]~{ hvi: HVIndex~HashValue[s]; desc: SubStringDescriptor_[base~@ssb.string, offset~, length~]; charsPerWord: CARDINAL~Environment.charsPerWord; offset, length, nw: CARDINAL; ssi: Table.Index; FOR hti _ hashVec[hvi], ht[hti].link UNTIL hti = HTNull DO SubStringForHash[@desc, hti]; IF Strings.EqualSubStrings[s, @desc] THEN RETURN [hti]; ENDLOOP; offset _ ssb.string.length; length _ s.length + 1; nw _ (offset + length + (charsPerWord-1) - ssb.string.maxlength)/charsPerWord; IF nw # 0 THEN { IF (ssi _ table.Words[BcdDefs.sstype, nw]) # ssw THEN ERROR; ssw _ ssw + nw; ssb.string _ [ text~, length~ssb.string.length, maxlength~ssb.string.maxlength+nw*charsPerWord]}; Strings.AppendChar[@ssb.string, LOOPHOLE[s.length, CHAR]]; Strings.AppendSubString[@ssb.string, s]; hti _ AllocateHash[]; ht[hti].link _ hashVec[hvi]; hashVec[hvi] _ hti; RETURN}; -- the following copied from SymbolPack.mesa ignoreCases: BOOL_FALSE; HashValue: PROC [s: SubString] RETURNS [HVIndex]~{ CharBits: PROC [CHAR, WORD] RETURNS [WORD]~LOOPHOLE[Inline.BITAND]; mask: WORD~0DFh; -- masks out ASCII case shifts n: CARDINAL~s.length; b: Strings.String~s.base; v: WORD~CharBits[b[s.offset], mask]*7fh + CharBits[b[s.offset + (n-1)], mask]; RETURN [Inline.BITXOR[v, n*0fh] MOD hashVec^.LENGTH]}; FindString: PUBLIC PROC [s: SubString] RETURNS [found: BOOL, hti: HTIndex]~{ desc: SubStringDescriptor; ss: SubString~@desc; FOR hti _ hashVec[HashValue[s]], ht[hti].link UNTIL hti = HTNull DO SubStringForHash[ss, hti]; found _ IF ignoreCases THEN Strings.EquivalentSubStrings[s, ss] ELSE Strings.EqualSubStrings[s, ss]; IF found THEN RETURN; ENDLOOP; RETURN [FALSE, HTNull]}; FindEquivalentString: PUBLIC PROC [s: SubString] RETURNS [found: BOOL, hti: HTIndex]~{ oldcase: BOOL~ignoreCases; ignoreCases _ TRUE; [found, hti] _ FindString[s]; ignoreCases _ oldcase; RETURN}; SubStringForHash: PUBLIC PROC [s: SubString, hti: HTIndex]~{ s.base _ @ssb.string; IF hti = HTNull THEN s.offset _ s.length _ 0 ELSE {s.offset _ ht[hti].offset; s.length _ ssb.size[ht[hti].offset]}}; }.