Help Needed.

Everything else that doesn't fall into one of the other PB categories.
ShDancer
User
User
Posts: 51
Joined: Sat Apr 26, 2003 5:49 am

Help Needed.

Post by ShDancer »

Hello there, im trying to implement a kind of indexed linked list, known as an Hashtable in Java.

But i have a little problem with this.

I am trying to do this for several Hashtables instead of only one, well the problem is that i can't pass by parameter a linked list, to every procedure

Can some one help me?

Here's the code:

Code: Select all

; Implementation of a HashTable from Java.
; A kind of indexed List

Global HashTable_Sort_Thread.b
Global HashTable_Init.b
Global AutomaticSort.b

HashTable_Init        = #False
AutomaticSort         = #False
HashTable_Sort_Thread = #False
AutomaticSort         = #True

Structure Key_Value_Pair
  Key.s
  Value.s
EndStructure

;{ HashTable Declares
Declare.b HashTable_Init      ( Hash.Key_Value_Pair() )
Declare.b HashTable_Stop      ( Hash.Key_Value_Pair() )
Declare HashTable_Sort_Thread ( Dummy.l )
Declare PutValue              ( Hash.Key_Value_Pair(), Key.s, Value.s )
Declare AddValue              ( Hash.Key_Value_Pair(), Key.s, Value.s )
Declare GetValue              ( Hash.Key_Value_Pair(), Key.s )
Declare DelValue              ( Hash.Key_Value_Pair(), Key.s )
Declare RemoveValue           ( Hash.Key_Value_Pair(), Key.s )
Declare HashSize              ( Hash.Key_Value_Pair() )
Declare ContainsKey           ( Hash.Key_Value_Pair(), Key.s )
;}

Procedure.b HashTable_Init( Hash.Key_Value_Pair() )
  ClearList( Hash() )
  ResetList( Hash() )
  HashTable_Init = #True
EndProcedure

Procedure.b HashTable_Stop( Hash.Key_Value_Pair() )
  ResetList( Hash() )
  ClearList( Hash() )
  HashTable_Init = #False
EndProcedure

Procedure HashTable_Sort_Thread( Dummy.l )
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b
  Shared AutomaticSort.b
  If ( HashTable_Init = #True And AutomaticSort = #True)
    If ( HashTable_Sort_Thread = #True )
      HashTable_Sort_Thread = #True
      ; Place Sort Here
      HashTable_Sort_Thread = #False
    EndIf
  EndIf
EndProcedure

Procedure PutValue( Hash.Key_Value_Pair(), Key.s, Value.s )
  AddValue( Hash(), Key.s, Value.s )
EndProcedure

Procedure AddValue( Hash.Key_Value_Pair(), Key.s, Value.s )
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b
  Shared AutomaticSort.b
  If ( HashTable_Init = #True )
    If ( HashTable_Sort_Thread = #True )
      WaitThread( #HashTable_Sort_ThreadID )
      ResetList( Hash() )
      AddElement( Hash() )
      HashTable()\Key = Key
      HashTable()\Value = Value
      If ( AutomaticSort = #True )
        CreateThread( @HashTable_Sort_Thread(), "" )
      EndIf
    EndIf
  EndIf
EndProcedure

Procedure GetValue( Hash.Key_Value_Pair(), Key.s )
  Protected ReturnValue.s
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b

  ReturnValue = "Null"
  If ( HashTable_Init = #True )
    If ( HashTable_Sort_Thread = #True )
      WaitThread( #HashTable_Sort_ThreadID )
      ForEach ( Hash() )
        If ( Hash()\Key = Key )
          ReturnValue = Hash()\Value
        EndIf
      Next
    EndIf
  EndIf
  ProcedureReturn ReturnValue
EndProcedure

Procedure DelValue( Hash.Key_Value_Pair(), Key.s )
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b
  Shared AutomaticSort.b
  If ( HashTable_Init = #True )
    If ( HashTable_Sort_Thread = #True )
      WaitThread( #HashTable_Sort_ThreadID )
      ResetList( Hash() )
      ForEach Hash()
        If ( Hash()\Key = Key )
          DeleteElement( Hash() )
        EndIf
      Next
      If ( AutomaticSort = #True )
        CreateThread( @HashTable_Sort_Thread(), "" )
      EndIf
    EndIf
  EndIf
EndProcedure

Procedure RemoveValue( Hash.Key_Value_Pair(), Key.s )
  DelValue( Hash(), Key )
EndProcedure

Procedure HashSize( Hash.Key_Value_Pair() )
  Protected ReturnValue.l
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b
  ReturnValue = 0
  If ( HashTable_Init = #True )
    If ( HashTable_Sort_Thread = #True )
      WaitThread( #HashTable_Sort_ThreadID )
    EndIf
    ReturnValue = CountList( Hash() ) 

  EndIf
  ProcedureReturn ReturnValue
EndProcedure

Procedure ContainsKey( Hash.Key_Value_Pair(), Key.s )
  Protected ReturnValue.l
  Shared HashTable_Sort_Thread.b
  Shared HashTable_Init.b
  ReturnValue = #False
  If ( HashTable_Init = #True )
    If ( HashTable_Sort_Thread = #True )
      WaitThread( #HashTable_Sort_ThreadID )
    EndIf
    ForEach ( Hash() )
      If ( Hash()\Key = Key )
        ReturnValue = #True
      EndIf
    Next
  EndIf
  ProcedureReturn ReturnValue
EndProcedure
For one indexed list its simple, and it worked at first time, but for more than one its dificult or i can't see the solution.

Thanks in advance.
User avatar
Derlidio
User
User
Posts: 77
Joined: Fri Feb 27, 2004 9:19 pm
Location: SP - Brazil

Post by Derlidio »

Yippe! You really have a problem here.

Lists are allways global in PB. So, no matter where you create the list, it will be accessible inside your procedures by default (and yes, there is no way to get rid of this). So, arising from this behavior, there is no sense in passing a list as a parameter to a procedure. You can pass a pointer to a list element, and deal with it inside the procedure, but you can't pass the entire list as a parameter.

So, I'm sorry having to tell you that, but I think there is not easy solution for your problem. Maybe someone else can give us a clue.

Best wishes...

- PJoe
Derlidio Siqueira
Post Reply