• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Searching/Using Class objects in lists

Kelevra

New member
Joined
Mar 24, 2019
Messages
4
First off - apologies if this has been answered before or if it's obvious. I'm a programmer who codes in IBM RPG/iSeries so the class/method/oop syntax is all very new to me (and it's hard to teach an old dog new tricks).

I'm trying to reproduce the A*/star algorithm in Cerberus. I've just about succeeded (in an extremely inefficient and clunky way) using object arrays for my open & closed lists but feel that this is perhaps the wrong tool for the job, especially as it's difficult to delete an array entry so there are less rows to read when trying to find the record I want.

So instead I thought about about using object lists and making use of the various Sort/Contains/Remove methods that lists have. I've been able to manipulate lists in this way when they are just straight forward Int or String lists, but can't figure out the syntax for searching or sorting by a particular field within my object/class.

For example, my object is something like this:

Class workList
Field x:Int = 0
Field y:Int = 0
Field g:Int = 0
Field h:Int = 0
Field f:Int = 0
End

Global openList:List<workList> = New List<workList>

openList.AddLast(New workList(1,3,1,2,3))
openList.AddLast(New workList(3,7,1,1,7))
openList.AddLast(New workList(8,3,2,6,3))
openList.AddLast(New workList(0,3,1,9,1))
openList.AddLast(New workList(4,9,0,8,6))
openList.AddLast(New workList(99,1,0,2,3))
openList.AddLast(New workList(2,3,2,3,3))


And I want to be able to check if the "x" field in openList.Contains(99), or to openList.Sort() by the "f" field. Is this possible or do I have to manually do these tests by reading through the lists using an Eachin For loop?

Any help would be gratefully received.

Thanks
 
Welcome to the forums!

I am no expert on this and maybe my solution is producing to much overhead, but I use it if I want to sort Lists or Stacks of Objects that are no standard type:
1. Make a custom List Class i. e. a Class that extends the List Class and implement the Compare() Method for it.
2. Create your List of Objects with this custom List Class and use the inbuild Sort() Method.

BTW 1. Your naming of Class workList gave me a little headache, so I changed it to Work. Then I used WorkList as the name of the custom List Class. 2. You should use 'insert code' for correct display of code snippets in the forum.

Cerberus:
Strict

Global openList:WorkList = New WorkList

Function Main:Int()
  
    openList.AddLast(New Work(1, 3, 1, 2, 3))
    openList.AddLast(New Work(3, 7, 1, 1, 7)) 'x of Object with highest f is 3.
    openList.AddLast(New Work(8, 3, 2, 6, 3))
    openList.AddLast(New Work(0, 3, 1, 9, 1)) 'x of Object with lowest f is 0.
    openList.AddLast(New Work(4, 9, 0, 8, 6))
    openList.AddLast(New Work(99, 1, 0, 2, 3))
    openList.AddLast(New Work(2, 3, 2, 3, 3))
  
    openList.Sort(True) 'Sorting ascending.
    Print openList.First().x
  
    openList.Sort(False) 'Sorting descending.
    Print openList.First().x
  
  
    Return 0
End



Class Work
    Field x:Int = 0
    Field y:Int = 0
    Field g:Int = 0
    Field h:Int = 0
    Field f:Int = 0
  
    Method New(x:Int, y:Int, g:Int, h:Int, f:Int)
        Self.x = x
        Self.y = y
        Self.g = g
        Self.h = h
        Self.f = f
    End
  
End


Class WorkList Extends List<Work> 'Custom List Class to use the Sort() Method.
  
    Method New(data:Work[])
        Super.New( data )
    End
  
    Method Compare:Int(lhs:Work, rhs:Work)
        Return lhs.f - rhs.f
    End

End
 
I have used Lists in my pathfinding solution in fantomCX as well. Works great and is super fast.
 
Thank you for this - I'll have a look later on tonight (once the kid is in bed) and see what I can absorb.

Cheers
 
Back
Top Bottom