News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

String Heap

Started by kevin, February 26, 2015, 09:39:59 AM

Previous topic - Next topic

kevin

  String Heap - Bank Version

    This code lets the user store a bunch of the string data in bank with a reference table.  So it's kind of simulating an array, except all the data is in one place,  rather than separate strings.   So if your program needs a lot of static string data, you could dump it all into the heap,  then save/reload the banks.


PlayBASIC Code: [Select]
   // alloc the heap
Init_String_Heap()


// make a bunhc of dummy strings and store them on heap

t=timer()
for lp =0 to 1000
AddHeapString(make$("HelloWorld"+Str$(lp),(lp and 15)+1 ))
next
print "time:"+str$(timer()-t)

print "HeapSize:"+str$(heapsize)

; SHow the strings on screen
for lp =0 to heapsize
print ReadHeapString$(lp)
next


Sync
waitkey



; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


Type tStringHeapTable
offset
Size
EndTYpe

global HeapSize =0 ; number of strings in heap
global HeapMaxSize =0 ; Max number of string in heap before it needs a resize
global HeapCache =0 ; bank used for string cache
global HeapTable =0 ; bank used for reference table



Function Init_String_Heap(MinNumberOfStrings=4096)

HeapCache =NewBank(MinNumberOfStrings*256)
HeapTable =Newbank(MinNumberOfStrings*sizeof(tStringHeapTable))

HeapSize =0
HeapMaxSize =MinNumberOfStrings

EndFUnction





; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


Function ReadHeapString$(Index)
dim ThisString as tStringHeapTable pointer

if Index>=0 and index<HeapSize
ThisString = getbankptr(HeapTable)+((INdex)*sizeof(tStringHeapTable))
// write this into the table
s$=PeekBankString(HeapCache,ThisString.Offset,ThisString.Size)
endif

EndFunction S$


; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


Function AddHeapString(s$)

;
StringSize=Len(s$)


if (HeapSize+1) >=HeapMaxSize
HeapMaxSize*=2
ResizeBank HeapTable,HeapMaxSize*sizeof(tStringHeapTable)
endif

dim PreviousString as tStringHeapTable pointer
dim ThisString as tStringHeapTable pointer

tableptr=getbankptr(HeapTable)

if HeapSize>0
PreviousString = tableptr+((HeapSize-1)*sizeof(tStringHeapTable))
Offset =PreviousString.offset
Offset+=PreviousString.Size
endif

ThisString = tableptr+((HeapSize)*sizeof(tStringHeapTable))

// write this into the table
ThisString.Offset =Offset
ThisString.Size =StringSize

// check if the heap cache is big enough for this string ??
CacheSize=GetBankSize(HeapCache)
if (Offset+StringSize+100)>=CacheSize
CacheSize=(Offset+StringSize+100)*2
resizebank heapCache,CacheSize
endif

// poke the string into the cache
pokebankString HeapCache,offset,s$,0

HeapSize++

EndFunction








kevin


  String Heap - String Cache Version

     This is an alternative version of the previous code, where the only difference is this version holds the cached strings in a single string and lets PB manage the string memory for us.


PlayBASIC Code: [Select]
   explicit on

// alloc the heap
Init_String_Heap()

// make a bunhc of dummy strings and store them on heap

local lp,t=timer()
for lp =0 to 1000
AddHeapString(make$("HelloWorld"+Str$(lp),(lp and 15)+1 ))
next
print "time:"+str$(timer()-t)

print "Strings in Heap:"+str$(GetStringHeapSize())
print "Heap Cache Size:"+str$(GetStringCacheSize())

; SHow the strings on screen
for lp =0 to GetStringHeapSize()
print ReadHeapString$(lp)
next


Sync
waitkey



; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; [ STRING HEAP ]----------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------

Type tStringHeapTable
offset
Size
EndTYpe

global HeapSize =0 ; number of strings in heap
global HeapMaxSize =0 ; Max number of string in heap before it needs a resize
global HeapCacheString$ ="" ; global string used to house string fragments

global HeapTable =0 ; bank used for reference table



Function Init_String_Heap(MinNumberOfStrings=4096)


// fill cache with some emtpy charcters
HeapCacheString$=make$(" ",4096)
// trim the size to zero without releasing the buffer
HeapCacheString$=left$(HeapCacheString$,0)

// alloc table
HeapTable =Newbank(MinNumberOfStrings*sizeof(tStringHeapTable))

HeapSize =0
HeapMaxSize =MinNumberOfStrings

EndFUnction



Function GetStringHeapSize()
result=HeapSize
EndFunction Result


Function GetStringCacheSize()
result=len(HeapCacheString$)
EndFunction Result


; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


Function ReadHeapString$(Index)
dim ThisString as tStringHeapTable pointer

if Index>=0 and index<HeapSize
ThisString = getbankptr(HeapTable)+((INdex)*sizeof(tStringHeapTable))
// grab string fragment from the cache
s$=mid$(HeapCacheString$,ThisString.Offset,ThisString.Size)
endif

EndFunction S$


; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------
; --------------------------------------------------------------------------


Function AddHeapString(s$)

;
local StringSize=Len(s$)

if (HeapSize+1) >=HeapMaxSize
HeapMaxSize*=2
ResizeBank HeapTable,HeapMaxSize*sizeof(tStringHeapTable)
endif
dim ThisString as tStringHeapTable pointer

ThisString = getbankptr(HeapTable)+((HeapSize)*sizeof(tStringHeapTable))

// write this into the table
ThisString.Offset =len(HeapCacheString$)
ThisString.Size =StringSize

HeapCacheString$+=s$

HeapSize++

EndFunction