3

This question was posted here (https://stackoverflow.com/questions/15881110/java-to-c-sharp-conversion) by a team member but was closed due to the community not having enough information.

Here's my attempt to revive such a question being, How would I go about converting this java extract into C#?

Java Extract:

PriorityQueue<PuzzleNode> openList = new PriorityQueue<PuzzleNode>
           (1,
            new Comparator<PuzzleNode>(){
                public int compare(PuzzleNode a, PuzzleNode b){
                    if (a.getPathCost() > b.getPathCost())
                        return 1;
                    else if (a.getPathCost() < b.getPathCost())
                        return -1;
                    else
                        return 0;
                    }
                }
            );

A sortedList has been thought about but to no avail as I'm unsure how to code it.

I've also tried creating a standard list with a method:

List<PuzzleNode> openList = new List<PuzzleNode>();

//Method to sort the list
public int CompareFCost(PuzzleNode a, PuzzleNode b)
        {
            if (a.getPathCost() > b.getPathCost())
            {
                return 1;
            }
            else if (a.getPathCost() > b.getPathCost())
            {
                return -1;
            }
            else
                return 0;
        }//end CompareFCost

and then calling: openList.Sort(CompareFCost); at appropriate locations, however this doesn't work.

What the code is used for? It orders the objects 'PuzzleNode' depending on a score (pathCost) I have set else where in the program. A while loop then operates and pulls the first object from the list. The list needs to be ordered otherwise an object with a higher pathCost could be chosen and the while loop will run for longer. The objective is to pull the lower pathCost from the list.

I ask for a conversion because it works in Java & the rest of the code has pretty much originated from Java.

Any takers? If you need further info I'm happy to discuss it further.

Community
  • 1
  • 1
Glitchezz
  • 333
  • 2
  • 6
  • 20
  • Perhaps this answer can help: http://stackoverflow.com/questions/102398/priority-queue-in-net I think the only problem there is an alternative to a PriorityQueue, which is quite different from a simple ordered list. – pcalcao Apr 08 '13 at 14:47
  • What's the final intent of this? Did you know you can use `LinQ` to do all sorts of `IEnumerable` related operations? Also, what's the difference between the java's `PriorityQueue` and a regular `List`? – Federico Berasategui Apr 08 '13 at 14:54
  • @HighCore - Priority queues allow efficient access to and removal of the minimum element. – Lee Apr 08 '13 at 14:57
  • @Lee and by "minimum element" you mean...? – Federico Berasategui Apr 08 '13 at 14:59
  • Please check edited question which states what it is needed for. I didn't know that, nor do I know how to go about using them at the moment, I'll look into it. – Glitchezz Apr 08 '13 at 15:00
  • X to Y conversion questions are almost always closed – Sam I am says Reinstate Monica Apr 08 '13 at 15:00
  • The minimum element is the element with the lowest/highest priority, in this case. Most priority queues achieve this in O(logN) or O(1) time, while guaranteeing interesting orders of complexity for the rest of the operations (insertion, deletion, etc). – pcalcao Apr 08 '13 at 15:01
  • @HighCore - In this case it appears to be the node with the minimum path cost. If the elements in the queue don't have a natural ordering then a custom comparator is required to compare elements. – Lee Apr 08 '13 at 15:03
  • 3
    Assuming you use a List and are just ordering based on the PathCost, you could use linq: `var orderedList = openList.OrderBy(node => node.getPathCost())` (`.OrderByDescending` if it needs to go the other way). Or, if you want more control over the comparison, look here at the "order by comparer" examples: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b – valverij Apr 08 '13 at 15:03

2 Answers2

0

I suppose you could misappropriate a SortedList something like this:

var openList=new SortedList<PuzzleNode,PuzzleNode>(
    //assumes .Net4.5 for Comparer.Create
    Comparer<PuzzleNode>.Create((a,b)=>{
        if (a.getPathCost() > b.getPathCost())
                    return 1;
                else if (a.getPathCost() < b.getPathCost())
                    return -1;
                else
                    return 0;

    }));
openList.Add(new PuzzleNode());
foreach(var x in openList.Keys)
{
    //ordered enumeration
}
var firstItem = openList.Dequeue();

by creating some extension methods to make things a little more queue-like

static class SortedListExtensions
{
    public static void Add<T>(this SortedList<T,T> list,T item)
    {
        list.Add(item,item);
    }
    public static T Dequeue<T>(this SortedList<T,T> list)
    {
        var item=list.Keys.First();
        list.Remove(item);
        return item;
    }
    //and so on...
}

TBH, I'd probably go for @valverij's answer in the comment to your original question, but if the cost of repeated sorting is prohibitive, this may be what you need.

spender
  • 106,080
  • 28
  • 202
  • 324
0

What the code is used for? It orders the objects 'PuzzleNode' depending on a score (pathCost) I have set else where in the program. A while loop then operates and pulls the first object from the list. The list needs to be ordered otherwise an object with a higher pathCost could be chosen and the while loop will run for longer. The objective is to pull the lower pathCost from the list.

  • 1: There's LinQ for that. You don't usually do any of these things in C#, because LinQ does it for you.

    • It orders the objects 'PuzzleNode' depending on a score (pathCost)

      That's Achieved with LinQ's Enumerable.OrderBy() Extension:

      //Assuming PathCost is a property of a primitive type (int, double, string, etc)
      var orderedlist = list.OrderBy(x => x.PathCost);
      
    • The objective is to pull the lower pathCost from the list.

      That's achieved using LinQ's Enumerable.Min() or Enumerable.Max() extensions.

      //Same assumption as above.
      var puzzlewithlowestpath = list.Min(x => x.PathCost);
      

here goes my rant about java being incomplete compared to C# because it lacks something like LinQ, but I will not do any more ranting in StackOverflow by now.


Another thing I wanted to mention is that if you are coding in C#, you'd better use C# Naming Conventions, where Properties are ProperCased:

public int PathCost {get;set;}
//or double or whatever

instead of:

public int getPathCost()
public int setPathCost()
Federico Berasategui
  • 41,627
  • 10
  • 91
  • 144