Searching/Using Class objects in lists

Kelevra

New Member
Joined
Mar 24, 2019
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
 

Phil7

Active Member
3rd Party Tool Dev
Joined
Jun 26, 2017
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 X:
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
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
I have used Lists in my pathfinding solution in fantomCX as well. Works great and is super fast.
 

Kelevra

New Member
Joined
Mar 24, 2019
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
 
Top Bottom