( hash("nuts") = 2*'n' + 3*'u' + 5*'t' + 7*'s' )
Code: Select all
Macro anotherMid(alpha, beta)
PeekS(alpha\wrd(3, beta), alpha\wrd(2, beta) )
EndMacro
#bpc = SizeOf(character) ; (b)ytes (p)er (c)haracter
#bpi = SizeOf(integer) ; (b)ytes (p)er (i)nteger
;longest word
;x32u; x32a; x64u; x64a
;114; 1469; ?(big); ?(big)
; (for beta reducing)
#greatestUnsignedCharacter = 1 << (8 * #bpc) - 1
#greatestSignedInteger = 1 << ((8 * #bpi) - 1) - 1
#cmLim = 1 << (8 * #bpc) - 1 ; (c)haracter (m)ask array (lim)it
#pvLim = 1 << 16 - 1 ; (p)rime (v)alue array (lim)it
Structure charMask
Array cm.a(#cmLim)
EndStructure
Structure wrd
Array wrd.i(3, 4095)
qty.i
EndStructure
Structure primeValue
Array pv.i(#pvLim)
EndStructure
Procedure cmCreate()
Define *this.charMask = AllocateMemory(SizeOf(charMask) )
InitializeStructure(*this, charMask)
ProcedureReturn *this
EndProcedure
Procedure pvCreate()
Define *this.primeValue = AllocateMemory(SizeOf(primeValue) )
Define i, j, sqrPvLim = Sqr(#pvLim)
InitializeStructure(*this, primeValue)
With *this
; *** 1/3 sieving ******************************************
i = 2
Repeat
If \pv(i) = 0
j = i * i
Repeat
\pv(j) = j
j + i
Until j > #pvLim
EndIf
i + 1
Until i > sqrPvLim
; *** 2/3 compacting ***************************************
j = 1
For i = 2 To #pvLim
If Not \pv(i)
\pv(j) = i
j + 1
EndIf
Next
j - 1
; *** 3/3 alpha reducing *****************************************
\pv(0) = j
ReDim \pv(j)
EndWith
ProcedureReturn *this
EndProcedure
Procedure hash(*a.character, *pv.primeValue)
While *a\c
i + 1
r + *pv\pv(i) * *a\c
*a + SizeOf(character)
Wend
ProcedureReturn r
EndProcedure
Procedure SplitFilterAndHash(*a.character, *cm.charMask, *pv.primeValue)
*c.wrd = AllocateMemory(SizeOf(wrd) ) ; resulting array
InitializeStructure(*c, wrd)
With *c
*a - #bpc
While *a\c
*a + #bpc
j + 1
If *cm\cm(*a\c)
If r
\wrd(0, k) = r
\wrd(2, k) = i
k + 1
i = 0
r = 0
EndIf
Else
If r = 0
\wrd(1, k) = j
\wrd(3, k) = *a
EndIf
i + 1
r + *pv\pv(i) * *a\c
EndIf
Wend
If r
\wrd(0, k) = r
\wrd(2, k) = i
EndIf
\qty = k
ProcedureReturn *c
EndWith
EndProcedure
; Here, we go !
Define *pv.primeValue = pvCreate()
Define *cm.charMask = cmCreate()
; WE EXCLUDE :
*cm\cm(9) = 1 ; tabulation char
*cm\cm(10) = 1 ; line feed char
*cm\cm(13) = 1 ; carriage return char
*cm\cm(32) = 1 ; space char
*cm\cm('(') = 1 ; 1st parenthesis char
*cm\cm('+') = 1 ; 'plus' char...
*cm\cm('e') = 1 ; and 'e' char...
*cm\cm('e') = 0 ; ...finally, nop : no 'e' char exclude...
*cm\cm('♞') = 0 ; We insure ourselves we keep the horse...
a$ = " monday (tuesday wednesday thursday+ friday"
weSearch = hash(@"wednesday", *pv)
Define *c.wrd = SplitFilterAndHash(@a$, *cm, *pv)
Debug a$
For i = 0 To *c\qty
Debug PeekS(*c\wrd(3, i), *c\wrd(2, i) )
If *c\wrd(0, i) = weSearch
Debug "before " + anotherMid(*c, i) + " there is " + anotherMid(*c, i - 1) + " and before again : " + anotherMid(*c, i - 2)
Debug "after " + anotherMid(*c, i) + " there is " + anotherMid(*c, i + 1) + " and after again : " + anotherMid(*c, i + 2)
EndIf
Next