[PB4] Dictionnaires / Tableaux associatifs

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
Flype
Messages : 2431
Inscription : jeu. 29/janv./2004 0:26
Localisation : Nantes

[PB4] Dictionnaires / Tableaux associatifs

Message par Flype »

En PHP, ou dans beaucoup de langages on trouve les dictionnaires.
c'est un type de données bien pratique et voici une des implémentations possible :

Code : Tout sélectionner

; Object:  Dictionary (Associative Array)
; Version: 0.1
; Author:  flype
; Requir.  Purebasic 4.0+

EnableExplicit

; AssociativeArray.pbi

Structure DICTIONARY
  key.s
  value.s
EndStructure

Macro dict_dim(dict)
  NewList dict.DICTIONARY()
EndMacro

Procedure.l dict_is(dict.DICTIONARY(), key.s = "")
  If key
    ForEach dict()
      If dict()\key = key
        ProcedureReturn #True
      EndIf
    Next
  Else
    If CountList(dict())
      ProcedureReturn #True
    EndIf
  EndIf
EndProcedure
Procedure.l dict_del(dict.DICTIONARY(), key.s = "")
  If key
    ForEach dict()
      If dict()\key = key
        DeleteElement(dict())
        ProcedureReturn #True
      EndIf
    Next
  Else
    ClearList(dict())
  EndIf
EndProcedure
Procedure.l dict_new(dict.DICTIONARY(), key.s, Value.s = "")
  If key
    ForEach dict()
      If dict()\key = key
        ProcedureReturn #False
      EndIf
    Next
    If AddElement(dict())
      dict()\key = key
      dict()\Value = Value
      ProcedureReturn #True
    EndIf
  EndIf
EndProcedure
Procedure.l dict_set(dict.DICTIONARY(), key.s, Value.s = "")
  ForEach dict()
    If dict()\key = key
      dict()\Value = Value
      ProcedureReturn #True
    EndIf
  Next
EndProcedure
Procedure.l dict_index(dict.DICTIONARY(), key.s)
  Protected value.s
  ForEach dict()
    If dict()\key = key
      ProcedureReturn ListIndex(dict())
    EndIf
  Next
EndProcedure
Procedure.s dict_value(dict.DICTIONARY(), key.s = "")
  Protected value.s
  ForEach dict()
    If key = "" Or dict()\key = key
      value + dict()\value + ", "
    EndIf
  Next
  ProcedureReturn value
EndProcedure
Procedure.l dict_values(dict.DICTIONARY(), values.s(), key.s = "")
  ForEach dict()
    If key = "" Or dict()\key = key
      If AddElement(values())
        values() = dict()\value
      EndIf
    EndIf
  Next
EndProcedure
Procedure.s dict_key(dict.DICTIONARY(), value.s = "")
  Protected key.s
  ForEach dict()
    If value = "" Or dict()\value = value
      key + dict()\key + ", "
    EndIf
  Next
  ProcedureReturn key
EndProcedure
Procedure.l dict_keys(dict.DICTIONARY(), keys.s(), value.s = "")
  ForEach dict()
    If value = "" Or dict()\value = value
      If AddElement(keys())
        keys() = dict()\key
      EndIf
    EndIf
  Next
EndProcedure
Procedure.l dict_count(dict.DICTIONARY(), value.s = "")
  Protected result.l
  If value
    ForEach dict()
      If dict()\value = value
        result + 1
      EndIf
    Next
  Else
    result = CountList(dict())
  EndIf
  ProcedureReturn result
EndProcedure
Procedure.l dict_sort(dict.DICTIONARY(), options.l, member.l, first.l = 0, last.l = 0)
  If last = 0
    last = CountList(dict())
  ElseIf last < 0
    last = CountList(dict()) + last
  EndIf
  Select member
    Case 0: SortStructuredList(dict(), options, OffsetOf(DICTIONARY\key), #PB_Sort_String, first.l, last.l)
    Case 1: SortStructuredList(dict(), options, OffsetOf(DICTIONARY\value), #PB_Sort_String, first.l, last.l)
  EndSelect
EndProcedure
Procedure.s dict_implode(dict.DICTIONARY(), op.s, sep.s)
  Protected result.s
  ForEach dict()
    result + dict()\key + op
    result + dict()\value + sep
  Next
  ProcedureReturn result
EndProcedure
Procedure.l dict_explode(dict.DICTIONARY(), string.s, op.s, sep.s)
  Protected i.l, field.s, n = CountString(string, sep) + 1
  For i = 1 To n
    field = StringField(string, i, sep)
    dict_new(dict(), Trim(StringField(field, 1, op)), StringField(field, 2, op))
  Next
  ProcedureReturn CountList(dict())
EndProcedure

; AssociativeArray_TEST.pb

Procedure Main(test.s)
  
  dict_dim(a)
  
  Debug dict_explode(a(), test, "=", " ")
  
  Debug dict_count(a())
  Debug dict_count(a(), "yes")
  
  Debug dict_index(a(), "revision")
  
  Debug dict_key(a(), "11")
  Debug dict_value(a(), "os")
  
  Debug dict_key(a())
  Debug dict_value(a())
  
  Debug dict_implode(a(), "-->", "  ///  ")
  
  dict_sort(a(), 0, 0)
  
  Debug dict_implode(a(), "-->", "  ///  ")
  
  dict_sort(a(), 0, 1)
  
  Debug dict_implode(a(), "-->", "  ///  ")
  
EndProcedure

Main("name=purebasic version=4.0 revision=11 os=windows registered=yes uptodate=yes")
Image