Voici une petit exemple avec une liste d'index (clé composée) liée à la liste, en gros on ne trie pas la liste principal mais uniquement la liste indexée, quand on ajoute un record on ajoute la clé à l'index par insertion triée.
Désolé mais le code à un peux été fait à l'arrache , je le replacerais au propre quand j'aurais le temps.
Voici la liste des fonction pour la gestion des index
- StringCompare(FistChaine.s,TwoChaine.s) -> Compare deux chaines de caractère retourne -1 si la chaîne est plus petite 1 si plus grande 0 équivalente
- IndexSort() ->Création de la liste d'index d'index
- AddToIndex(Key.s,*Pos) -> inséré un nouvelle index (conserve le tri)
- Looking(chaine.s) -> Recherche un élément si trouvé (même si la chaîne comparée est plus petite mais correspond) pointe sur la structure principale
Code : Tout sélectionner
Structure People
Name.s
FirstName.s
Adress.s
ZipCode.s
Cyti.s
Telephone.s
Celular.s
EndStructure
Structure Index
*Position
Key.s
EndStructure
Global NewList myPeople.People()
Global NewList myIndex.Index()
Declare StringCompare(FistChaine.s,TwoChaine.s)
Declare AddData()
Declare IndexSort()
Declare DisplayIndex()
Declare Looking(chaine.s)
Declare AddToIndex(Key.s,*Pos)
Procedure StringCompare(FistChaine.s,TwoChaine.s)
Protected i,max
max=Len(FistChaine)
For i=i To max
If Asc(Mid(FistChaine,i,1))>Asc(Mid(TwoChaine,i,1))
ProcedureReturn 1
Else
If Asc(Mid(FistChaine,i,1))<Asc(Mid(TwoChaine,i,1))
ProcedureReturn -1
EndIf
EndIf
Next
ProcedureReturn 0
EndProcedure
Procedure AddData()
With myPeople()
AddElement(myPeople())
\Name="Zoala"
\FirstName="Alain"
AddElement(myPeople())
\Name="Yatola"
\FirstName="Halonin"
AddElement(myPeople())
\Name="Albinos"
\FirstName="Bernard"
AddElement(myPeople())
\Name="Catorin"
\FirstName="Brunot"
AddElement(myPeople())
\Name="Albinos"
\FirstName="Pierre"
EndWith
EndProcedure
Procedure IndexSort()
Protected i,comp
ClearList(myIndex())
ForEach myPeople()
With myIndex()
If ListSize(myIndex())=0
AddElement(myIndex())
Else
i=0
ForEach myIndex()
i+1
comp=StringCompare(myPeople()\Name+" "+myPeople()\FirstName,\Key)
If comp<0
InsertElement(myIndex())
Break
Else
If i=ListSize(myIndex())
AddElement(myIndex())
EndIf
EndIf
Next
EndIf
\Key=myPeople()\Name+" "+myPeople()\FirstName
\Position=@myPeople()
EndWith
Next
EndProcedure
Procedure DisplayIndex()
ForEach myIndex()
With myIndex()
Debug \Key
EndWith
Next
EndProcedure
Procedure Looking(chaine.s)
Protected *adress=-1
With myIndex()
ForEach myIndex()
If StringCompare(chaine,\Key)=0
*adress=\Position
Break
EndIf
Next
EndWith
If *adress>-1
With myPeople()
ChangeCurrentElement(myPeople(),*adress)
Debug ""
Debug "Element finding"
Debug \Name+" "+\FirstName
EndWith
EndIf
EndProcedure
Procedure AddToIndex(Key.s,*Pos)
With myIndex()
If ListSize(myIndex())=0
AddElement(myIndex())
Else
i=0
ForEach myIndex()
i+1
comp=StringCompare(Key,\Key)
If comp<0
InsertElement(myIndex())
Break
Else
If i=ListSize(myIndex())
AddElement(myIndex())
EndIf
EndIf
Next
EndIf
\Key=Key
\Position=*Pos
EndWith
EndProcedure
AddData()
IndexSort()
Debug "Index list"
DisplayIndex()
Looking("Yato")
; Ici j'ajoute un élément
With myPeople()
AddElement(myPeople())
\Name="Gradon"
\FirstName="Gérard"
AddToIndex(\Name+" "+\FirstName,@myPeople())
EndWith
; J" affiche l'index pour vérifié
Debug "Index list"
DisplayIndex()
; Ici j'ajoute un élément
With myPeople()
AddElement(myPeople())
\Name="Gradon"
\FirstName="Alan"
AddToIndex(\Name+" "+\FirstName,@myPeople())
EndWith
; J' affiche l'index pour vérifié
Debug "Index list"
DisplayIndex()