SetListPointer() in PB 4.1,2 or 3 or or or ;)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Franky
Enthusiast
Enthusiast
Posts: 213
Joined: Sat Apr 26, 2003 2:58 pm

SetListPointer() in PB 4.1,2 or 3 or or or ;)

Post by Franky »

Edit: Just saw, that I can pass Lists as Procedureparameters :oops:
So the example down there \|/ is opsolete. But the Idea as a feature is still a good one ;)

Hi people.

After downloading PB 4.0 I tried to get this little example (PseudoCode)
Working under PB.
It´s a sort-algorithm (MinSort it´s called, I think, don´t know.

Code: Select all

Procedure Sort(*pointer.Long)
Protected NewList Left.l()
Protected NewList Right.l()
Protected NewList List.l())
       SetListPointer(List(),*pointer)
       wert=List()
       k=DeleteElement(List())
        While k<>0
                 if List()>wert
                      AddElement(Right())
                              Right()=List()
                 else
                      AddElement(Left())
                              Left()=List()
                 endif
                 k=deleteelement(list())
         Wend 
             Sort(Left())
             Sort(Right())
         While CountList(Left())>0
                            AddElement(List())
                                 List()=Left()
                            DeleteElement(Left())
         Wend
         AddElement(List())
                    List()=wert
         While CountList(Right())>0
                            AddElement(List())
                                 List()=Right()
                            DeleteElement(Right())
         Wend
         ProcedureReturn @List() ;perhaps with a -8, don´t know, what´s better        
EndProcedure 

NewList MyList.l()
       for a=1 to 20
               AddElement(MyList())
                      MyList()=Random(200)
                      Debug mylist()
        next 
        Debug "__________"
        k=Sort(@MyList())
        SetListPointer(MyList(),k)
        FirstElement(MyList())
        for a=1 to 20
                       Debug mylist()
                       NextElement(MyList())
         next        

So, what would be good is having a function like

Code: Select all

       SetListPointer(MyList(),*AnyElement)

With this Function it would be possible to use PBs Linked-ListFunctions in Combination and it would make it possible to users, to write some Functions on their own instead of asking for

AddList(List(),ListToAdd()) ;<-without copying it, just set the pointers correctly

something like
SplitList(ListToSplit(),Split1(),Split2(),elementsin Split1)

or more of these, I can´t remember for now.

But, btw. PureBasic 4.0 is amazing :)


Btw, here is what I tried to do (hell of a Chaos, I know, an other reason for my wish ^^)

It´s
1.)Not working at all, there is some Bug in it.
2.)too long for what it should do and without comments (shouldn´t be to hard to understand, when understanding the pseudocode upstairs in this posting)

Code: Select all

Global NewList Werte.l()

Structure List
        Nexter.l
        laster.l
        wert.l
EndStructure         


Procedure P_NextElement(*pointer.list)
        If *Pointer<>0
              ProcedureReturn *pointer\Nexter
        EndIf 
        ProcedureReturn 0
EndProcedure

Procedure P_PreviousElement(*pointer.list)
        If *Pointer<>0
              ProcedureReturn *pointer\laster
        EndIf 
        ProcedureReturn 0 
EndProcedure


Procedure P_CountList(*pointer.list)
Protected k.l    
Protected number.l
          number=1
          k=*pointer
          While *pointer<>0
                 *pointer=*pointer\laster
                 INC number 
          Wend           
          *pointer=k
          While *pointer<>0
                 *pointer=*pointer\Nexter
                 INC number
          Wend 
          DEC NUMBER
          DEC number
          
          ProcedureReturn number
EndProcedure

Procedure P_FirstElement(*point.list)
      While *point\laster<>0
            *point=*point\laster
      Wend      
      ProcedureReturn *point       
EndProcedure 

Procedure P_LastElement(*point.list)
      While *point\Nexter<>0
            *point=*point\Nexter
      Wend             
      ProcedureReturn *point       
EndProcedure 

 
Procedure MySort(*List.List)
Static leer.l
Protected NewList   leftList.l()
Protected NewList   rightList.l()
Debug Space(leer)+"Aufruf"
          leer=leer+3
          mittwert.l=*list\wert
          t=P_NextElement(*list)              
          If t<>0
             *list=t          
              Repeat
                  If *list\wert<mittwert 
                        AddElement(LeftList.l())
                                 leftlist()=*list\Wert
                  Else 
                        AddElement(rightList.l())
                                 rightlist()=*list\Wert
                  EndIf
                  t=P_NextElement(*list)              
                  If t<>0
                     *list=t
                  Else
                      Break 
                  EndIf
              ForEver
              *list=P_FirstElement(*list)
              Debug Space(leer)+Str(*list)
              Debug Space(leer)+Str(CountList(leftlist()))
              Debug Space(leer)+Str(CountList(rightlist()))
              If CountList(leftlist())>1
                      FirstElement(leftList())
                      mysort(@leftList()-8)
                      ResetList(leftlist())                      
                      FirstElement(leftlist())          
                      Debug Space(a)+"leftlist: "+Str(ListIndex(leftlist()))
                      While CountList(leftlist())>0            
                            *list\wert=leftlist()
                            *list=P_NextElement(*list)
                            DeleteElement(leftlist())
                      Wend  
              EndIf              
              *list\wert=mittwert
    
              If CountList(rightlist())>1
                        FirstElement(rightlist())
                        Debug Space(leer)+"Rechts"
                        mysort(@rightlist()-8)
                        FirstElement(rightlist())
                        Debug Space(a)+"rightlist: "+Str(ListIndex(rightlist()))
                        While CountList(rightlist())>0            
                              *list=P_NextElement(*list)
                              *list\wert=3;rightlist()
                              DeleteElement(rightlist())
                        Wend                      
              EndIf 
        EndIf
        leer=leer-3
Debug Space(leer)+"Ende"        
EndProcedure 

If OpenWindow(1,200,200,400,500,#PB_Window_SystemMenu,"Sortierer")
          CreateGadgetList(WindowID(1))
                   ListViewGadget(1,0,0,400,440)
                   ButtonGadget(2,0,450,500,50,"Sort")
                   For a=0 To 500
                          AddGadgetItem(1,-1,Str(Random(600)))
                   Next 
                   Repeat 
                       event=WaitWindowEvent()
                       If event=#PB_Event_Gadget
                              Select EventGadget()
                                 Case 2 
                                    ClearList(Werte())
                                    For a=0 To 500
                                         AddElement(Werte())
                                         Werte()=Val(GetGadgetItemText(1,a,0))
                                    Next
                                    ClearGadgetItemList(1)
                                    FirstElement(Werte())
                                    NextElement(werte())
                                    PreviousElement(werte())
                                    mysort(@Werte()-8)
                                    ForEach Werte()
                                         AddGadgetItem(1,-1,Str(werte()))
                                         DeleteElement(Werte())
                                    Next                                     
                              EndSelect
                       EndIf
                   Until event=#WM_CLOSE
EndIf                   
Give Up everything but trying!