windows linux osx
When will this go native Fred?
Code: Select all
;NumericMap hack For structured Numeric keyed Maps
;windows linux osx
CompilerIf #PB_Compiler_Version > 602
CompilerError "Attention - Check internal pb-function NumericMap!"
CompilerEndIf
NewMap dummy()
ClearMap(dummy())
ResetMap(dummy())
FreeMap(dummy())
Import ""
PB_NewNumericMap(ElementSize.i,*StructureMap,*Address,HashTabelSize.l);
PB_FreeMap(*map) ;
PB_ResetMap(*Map) ;
PB_ClearMap(*Map) ;
PB_MapSize(*Map) ;
PB_PushMapPosition(*map) ;
PB_PopMapPosition(*map) ;
PB_NextMapElement(*Map) ;
PB_FindNumericMapElement(*Map,Key.i) ;
PB_AddNumericMapElement(*Map,Key.i) ;
PB_AddNumericMapElement2(*Map,Key.i,ElementCheck=#PB_Map_ElementCheck);
PB_DeleteNumericMapElement2(*Map,Key.i) ;
PB_DeleteNumericMapElement(*Map) ;
PB_CopyMap(*Map,*DestinationMap) ;
PB_CopyMap2(*Map,*DestinationMap,Clear.l) ;
EndImport
Macro NewNumericMap(pmap,HashTableSize,StructureType,IsStructure=0)
Global structadr=0
EnableASM
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
CompilerIf IsStructure
lea rax,[s_#StructureType]
mov [v_structadr],rax
CompilerEndIf
CompilerElse
CompilerIf IsStructure
lea eax,[s_#StructureType]
mov [v_structadr],eax
CompilerEndIf
CompilerEndIf
DisableASM
PB_NewNumericMap(SizeOf(StructureType),structadr,@pmap,HashTableSize)
EndMacro
Procedure NumericMapkey(pMap)
Protected ele,key
ele = PeekI(pMap)
If ele
key = PeekI(ele+SizeOf(Integer))
EndIf
ProcedureReturn key
EndProcedure
Procedure FreeNumericMap(mp)
!MOV rcx,qword [p.v_mp]
!CALL PB_FreeMap
EndProcedure
Procedure ClearNumericMap(mp)
!MOV rcx,qword [p.v_mp]
!CALL PB_ClearMap
EndProcedure
Procedure ResetNumericMap(mp)
!MOV rcx,qword [p.v_mp]
!CALL PB_ResetMap
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
;Note you need to use a structured element pointer even for native types
Structure foo
List controls.i()
i.i
s.s
EndStructure
Procedure test()
Protected mp, *el.foo
NewNumericMap(mp,512,foo,#PB_Structure) ;create the map and pass map variable, number of elements, the name of the structure , if the structure contains a list map or array use the flag #PB_Structure
*el = PB_AddNumericMapElement(mp,123)
AddElement(*el\controls())
*el\controls() = 111
AddElement(*el\controls())
*el\controls() = 112
*el\i = 123
*el\s = "Hello"
;
*el = PB_AddNumericMapElement2(mp,345)
AddElement(*el\controls())
*el\controls() = 113
AddElement(*el\controls())
*el\controls() = 114
*el\i = 345
*el\s = "World"
;
*el = PB_FindNumericMapElement(mp,123)
ForEach *el\controls()
Debug "list Controls " + *el\controls()
Next
Debug "integer " + *el\i
Debug "string " + *el\s
*el = PB_FindNumericMapElement(mp,345)
ForEach *el\controls()
Debug "list Controls " + *el\controls()
Next
Debug "integer " + *el\i
Debug "string " + *el\s
Debug "+++++++++++"
Debug "walk the map"
Debug "+++++++++++"
ResetNumericmap(mp)
Repeat
*el = PB_NextMapElement(mp)
If *el
Debug "mapkey " + NumericMapkey(mp)
ForEach *el\controls()
Debug "list Controls " + *el\controls()
Next
Debug "integer " + *el\i
Debug "string " + *el\s
EndIf
Until *el=0
Debug "++++++++++++++"
Debug "clear map"
ClearNumericMap(mp);
ResetNumericMap(mp)
Repeat
*el = PB_NextMapElement(mp)
If *el
Debug "mapkey " + NumericMapkey(mp)
ForEach *el\controls()
Debug "list Controls " + *el\controls()
Next
Debug "integer " + *el\i
Debug "string " + *el\s
EndIf
Until *el=0
Debug "should be nothing"
FreeNumericmap(mp)
Debug "++++++++++++"
Debug "test with a native type via pointer"
Protected *elf.float
NewNumericMap(mp,512,float) ;create the map and pass map variable, number of elements, the name of the structure
*elf = PB_AddNumericMapElement(mp,123)
*elf\f = #PI
*elf = PB_AddNumericMapElement(mp,345)
*elf\f = 2*#PI
*elf = PB_FindNumericMapElement(mp,123)
Debug *elf\f
PB_DeleteNumericMapElement2(mp,345)
*elf = PB_FindNumericMapElement(mp,345)
If *elf
Debug *elf\f
Else
Debug "deleted key 345"
EndIf
Debug NumericMapkey(mp)
*elf = PB_FindNumericMapElement(mp,123)
If *elf
Debug *elf\f
EndIf
Debug NumericMapkey(mp)
FreeNumericmap(mp)
MessageRequester("test","ok")
EndProcedure
test()
CompilerEndIf