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.
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.
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()
Related
So in my attempt to start learning c# one challenge I've come across is to create a recursive function that will calculate the sum of a list. I'm wondering if it's possible to do this using a list as the only argument of the function? Or would I need to apply an index size as well to work through the list?
int addRecursively(List<int> numList)
{
int total = numList[0];
if (numList.Count > 1)
{
numList.RemoveAt(0);
return total += addRecursively(numList);
}
Console.WriteLine(total);
return total;
}
List<int> numbers = new<List<int> {1,2,3,4,5,6,7,8};
addRecursively(numbers); //returns only the last element of whichever list I enter.
I was hoping by assigning the total to the first index of the list before deleting the first index of the list that when passed into the next instance of the function the index of each element in the list would move down one, allowing me to get each value in the list and totalling them up. However using the function will only ever return the last element of whichever list of integers I enter.
My thought process came from arrays and the idea of the shift method on an array in JS, removing the first element and bringing the whole thing down.
Am I attempting something stupid here? Is there another similar method I should be using or would I be better off simply including a list size as another parameter?
Thanks for your time
So in my attempt to start learning c# one challenge I've come across is to create a recursive function that will calculate the sum of a list. I'm wondering if it's possible to do this using a list as the only argument of the function? Or would I need to apply an index size as well to work through the list?
That's a great exercise for a beginner. However, you would never, ever do this with a List<int> in a realistic program. First, because you'd simply call .Sum() on it. But that's a cop-out; someone had to write Sum, and that person could be you.
The reason you would never do this recursively is List<T> is not a recursive data structure. As you note, every time you recurse there has to be something different. If there is not something different then you have an unbounded recursion!
That means you have to change one of the arguments, either by mutating it, if it is a reference type, or passing a different argument. Neither is correct in this case where the argument is a list.
For a list, you never want to mutate the list, by removing items, say. You don't own that list. The caller owns the list and it is rude to mutate it on them. When I call your method to sum a list, I don't want the list to be emptied; I might want to use it for something else.
And for a list, you never want to pass a different list in a recursion because constructing the new list from the old list is very expensive.
(There is also the issue of deep recursion; presumably we wish to sum lists of more than a thousand numbers, but that will eat up all the stack space if you go with a recursive solution; C# is not a guaranteed-tail-recursive language like F# is. However, for learning purposes let's ignore this issue and assume we are dealing with only small lists.)
Since both of the techniques for avoiding unbounded recursions are inapplicable, you must not write recursive algorithms on List<T> (or, as you note, you must pass an auxiliary parameter such as an index, and that's the thing you change). But your exercise is still valid; we just have to make it a better exercise by asking "what would we have to change to make a list that is amenable to recursion?"
We need to change two things: (1) make the list immutable, and (2) make it a recursively defined data structure. If it is immutable then you cannot change the caller's data by accident; it's unchangeable. And if it is a recursively defined data structure then there is a natural way to do recursion on it that is cheap.
So this is your new exercise:
An ImmutableList is either (1) empty, or (2) a single integer, called the "head", and an immutable list, called the "tail". Implement these in the manner of your choosing. (Abstract base class, interface implemented by multiple classes, single class that does the whole thing, whatever you think is best. Pay particular attention to the constructors.)
ImmutableList has three public read-only properties: bool IsEmpty, int Head and ImmutableList Tail. Implement them.
Now we can define int Sum(ImmutableList) as a recursive method: the base case is the sum of an empty list is zero; the inductive case is the sum of a non-empty list is the head plus the sum of the tail. Implement it; can you do it as a single line of code?
You will learn much more about C# and programming in a functional style with this exercise. Use iterative algorithms on List<T>, always; that is what it was designed for. Use recursion on data structures that are designed for recursion.
Bonus exercises:
Write Sum as an extension method, so that you can call myImmutableList.Sum().
Sum is a special case of an operation called Aggregate. It returns an integer, and takes three parameters: an immutable list, an integer called the accumulator, and a Func<int, int, int>. If the list is empty, the result is the accumulator. Otherwise, the result is the recursion on the tail and calling the function on the head and the accumulator. Write a recursive Aggregate; if you've done it correctly then int Sum(ImmutableList items) => Aggregate(items, 0, (acc, item) => acc + item); should be a correct implementation of Sum.
Genericize ImmutableList to ImmutableList<T>; genericize Aggregate to Aggregate<T, R> where T is the list element type and R is the accumulator type.
Try this way:
int addRecursively(List<int> lst)
{
if(lst.Count() == 0) return 0;
return lst.Take(1).First() + addRecursively(lst.Skip(1).ToList());
}
one more example:
static public int RecursiveSum(List<int> ints)
{
int nextIndex = 0;
if(ints.Count == 0)
return 0;
return ints[0] + RecursiveSum(ints.GetRange(++nextIndex, ints.Count - 1));
}
These are some ways to get the sum of integers in a list.
You don't need a recursive method, it spends more system resources when it isn't needed.
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
int sum1 = numbers.Sum();
int sum2 = GetSum2(numbers);
int sum3 = GetSum3(numbers);
int sum4 = GetSum4(numbers);
}
private static int GetSum2(List<int> numbers)
{
int total = 0;
foreach (int number in numbers)
{
total += number;
}
return total;
}
private static int GetSum3(List<int> numbers)
{
int total = 0;
for (int i = 0; i < numbers.Count; i++)
{
total += numbers[i];
}
return total;
}
private static int GetSum4(List<int> numbers)
{
int total = 0;
numbers.ForEach((number) =>
{
total += number;
});
return total;
}
}
Lets assume you have a function that returns a lazily-enumerated object:
struct AnimalCount
{
int Chickens;
int Goats;
}
IEnumerable<AnimalCount> FarmsInEachPen()
{
....
yield new AnimalCount(x, y);
....
}
You also have two functions that consume two separate IEnumerables, for example:
ConsumeChicken(IEnumerable<int>);
ConsumeGoat(IEnumerable<int>);
How can you call ConsumeChicken and ConsumeGoat without a) converting FarmsInEachPen() ToList() beforehand because it might have two zillion records, b) no multi-threading.
Basically:
ConsumeChicken(FarmsInEachPen().Select(x => x.Chickens));
ConsumeGoats(FarmsInEachPen().Select(x => x.Goats));
But without forcing the double enumeration.
I can solve it with multithread, but it gets unnecessarily complicated with a buffer queue for each list.
So I'm looking for a way to split the AnimalCount enumerator into two int enumerators without fully evaluating AnimalCount. There is no problem running ConsumeGoat and ConsumeChicken together in lock-step.
I can feel the solution just out of my grasp but I'm not quite there. I'm thinking along the lines of a helper function that returns an IEnumerable being fed into ConsumeChicken and each time the iterator is used, it internally calls ConsumeGoat, thus executing the two functions in lock-step. Except, of course, I don't want to call ConsumeGoat more than once..
I don't think there is a way to do what you want, since ConsumeChickens(IEnumerable<int>) and ConsumeGoats(IEnumerable<int>) are being called sequentially, each of them enumerating a list separately - how do you expect that to work without two separate enumerations of the list?
Depending on the situation, a better solution is to have ConsumeChicken(int) and ConsumeGoat(int) methods (which each consume a single item), and call them in alternation. Like this:
foreach(var animal in animals)
{
ConsomeChicken(animal.Chickens);
ConsomeGoat(animal.Goats);
}
This will enumerate the animals collection only once.
Also, a note: depending on your LINQ-provider and what exactly it is you're trying to do, there may be better options. For example, if you're trying to get the total sum of both chickens and goats from a database using linq-to-sql or linq-to-entities, the following query..
from a in animals
group a by 0 into g
select new
{
TotalChickens = g.Sum(x => x.Chickens),
TotalGoats = g.Sum(x => x.Goats)
}
will result in a single query, and do the summation on the database-end, which is greatly preferable to pulling the entire table over and doing the summation on the client end.
The way you have posed your problem, there is no way to do this. IEnumerable<T> is a pull enumerable - that is, you can GetEnumerator to the front of the sequence and then repeatedly ask "Give me the next item" (MoveNext/Current). You can't, on one thread, have two different things pulling from the animals.Select(a => a.Chickens) and animals.Select(a => a.Goats) at the same time. You would have to do one then the other (which would require materializing the second).
The suggestion BlueRaja made is one way to change the problem slightly. I would suggest going that route.
The other alternative is to utilize IObservable<T> from Microsoft's reactive extensions (Rx), a push enumerable. I won't go into the details of how you would do that, but it's something you could look into.
Edit:
The above is assuming that ConsumeChickens and ConsumeGoats are both returning void or are at least not returning IEnumerable<T> themselves - which seems like an obvious assumption. I'd appreciate it if the lame downvoter would actually comment.
Actually simples way to achieve what you what is convert FarmsInEachPen return value to push collection or IObservable and use ReactiveExtensions for working with it
var observable = new Subject<Animals>()
observable.Do(x=> DoSomethingWithChicken(x. Chickens))
observable.Do(x=> DoSomethingWithGoat(x.Goats))
foreach(var item in FarmsInEachPen())
{
observable.OnNext(item)
}
I figured it out, thanks in large part due to the path that #Lee put me on.
You need to share a single enumerator between the two zips, and use an adapter function to project the correct element into the sequence.
private static IEnumerable<object> ConsumeChickens(IEnumerable<int> xList)
{
foreach (var x in xList)
{
Console.WriteLine("X: " + x);
yield return null;
}
}
private static IEnumerable<object> ConsumeGoats(IEnumerable<int> yList)
{
foreach (var y in yList)
{
Console.WriteLine("Y: " + y);
yield return null;
}
}
private static IEnumerable<int> SelectHelper(IEnumerator<AnimalCount> enumerator, int i)
{
bool c = i != 0 || enumerator.MoveNext();
while (c)
{
if (i == 0)
{
yield return enumerator.Current.Chickens;
c = enumerator.MoveNext();
}
else
{
yield return enumerator.Current.Goats;
}
}
}
private static void Main(string[] args)
{
var enumerator = GetAnimals().GetEnumerator();
var chickensList = ConsumeChickens(SelectHelper(enumerator, 0));
var goatsList = ConsumeGoats(SelectHelper(enumerator, 1));
var temp = chickensList.Zip(goatsList, (i, i1) => (object) null);
temp.ToList();
Console.WriteLine("Total iterations: " + iterations);
}
Approach 1:
class myClass
{
List<SomeType> _list;
IENumerator<SomeType> GetEnumerator()
{
foreach(SomeType t in _list)
yield return t;
}
}
myClass m = new myClass();
List<SomeType> list;
...
foreach(SomeType t in m)
list.Add(t);
Approach 2:
class myClass
{
public List<SomeType> _list {get; private set;}
}
myClass m = new myClass();
...
List<SomeType> list = m.list;
Which approach is better? If second then could you please show me real-proof usage of yield return?
A class with a collection-property is usually best done as a list, so that it can be iterated multiple times, and mutated. That is not possible for an enumerator, which represents just a sequence. Typical uses of enumerators might be:
filtering data on the fly (such as Enumerable.Where)
reading individual items sequentially from a file that contains multiple records, without loading them all at once
or network socket
or database server
providing a forwards-only, read-only wrapper over a sequence of data
etc
Neither.
If you want an enumerator, and already have an underlying IEnumerable type (List implements IList which extends IEnumerable), then just return it like that:
public IEnumerator<SomeType> GetEnumerator ()
{
return _list.GetEnumerator();
}
Otherwise, if you actually need a list, i.e. random access using indexes, then return an IList; and if you actually want to return its internal implementation type, i.e. List, then you can just make it an accessible property. Note though, that a private setter does not prevent modifying the list (adding or removing items etc.). If you want that, return a read-only list instead:
public IList<SomeType> List
{
get { return _list.AsReadOnly(); }
}
Regarding yield
could you please show me real-proof usage of yield return?
yield return is useful, when you actually have a generator, when you actually need to generate the next item. A simple example would be a random number generator which provides you with another random number, as long as you keep asking it. There is not necessarily an end to it, but you might not know the amount of numbers before starting.
Another common usage would be anything that retrieves the data from some external source. For example a list of items from a webservice. Before you don’t know how many items there are, and you don’t necessarily know how many items you actually want (as you might want to display it in an endless display, showing one at a time). In that case, you could do it like that:
IEnumerable<Item> GetItems()
{
while (Service.HasMorePages())
{
foreach (Item item in Service.GetNextPage())
{
yield return item;
}
}
yield break;
}
GetNextPage would always return a list of N items at a time, and you would get the next one whenever you want more items than you have already received.
The options are without bound,
public IEnumerator<int> FibonnaciSeries()
{
int a = 1;
int b = 1;
yield return 1;
yield return 1;
while (true)
{
var c = a + b;
a = b;
b = c;
yield return c;
}
}
Is one trivial example that comes to mind, is the Fibonnaci Series real world?
This is not the most efficient implementation possible.
Which is the best way to loop thru an list? is for loop better than numerous List class's find method? Also if i use its find methed as i mentioned below which is anonymous delegate an instance of an predicate delegate, does it better than using lambda expression? Which one will execute faster?
var result = Books.FindLast(
delegate(Book bk)
{
DateTime year2001 = new DateTime(2001,01,01);
return bk.Publish_date < year2001;
});
It's a complex question because it involves a lot of different topics.
In general, delegates are many times slower than a simple function call but to enumerate a list (via foreach) is terribly slow too.
If you really care about performance (but do not do it a-priori, profile!) you should avoid delegates and enumerations. First big step (whenever possible) could be to use a Hashtable instead of a simple list.
Examples
Now some examples, I'll write the same function in different ways, from the more readable (but slower) to the less readable (but faster). I omit every error checking but a real world function shouldn't (at least some asserts are required).
This function uses LINQ, it's the more easy to understand but the slowest.
Note the books can be a generic enumeration (it's not required to be a List<T>)
public static Book FindLastBookPublishedBefore(IEnumerable<Book> books,
DateTime date)
{
return books.FindLast(x => x.Publish_date < date);
}
Same as before but without LINQ. Note that this function
handles a special case: the list doesn't contains any eligible book.
public static Book FindLastBookPublishedBefore(IEnumerable<Book> books,
DateTime date)
{
Book candidate = null;
foreach (Book book in books)
{
if (candidate == null || candidate.Publish_date > book.Publish_date)
candidate = book;
}
return candidate;
}
Same as before but without enumeration, note that this function
handles a special case: the list doesn't contains any eligible book.
public static Book FindLastBookPublishedBefore(List<Book> books,
DateTime date)
{
Book candidate = null;
for (int i=0; i < books.Count; ++i)
{
if (candidate == null || candidate.Publish_date > books[i].Publish_date)
candidate = books[i];
}
return candidate;
}
Same as before but with a SortedList<T> as suggested by #MaratKhasanov. Please note that with this container you'll have the good performances during the search but insertion of a new element can be more slow than a normal unsorted list (because list itself must be kept sorted). If the number of elements in the list is very high you may think to write your own sorted list using a Hashtable (using, for example, the year as key for the first level).
public static Book FindLastBookPublishedBefore(SortedList<Book> books,
DateTime date)
{
Book candidate = null;
for (int i=0; i < books.Count; ++i)
{
DateTime publishDate = books[i].Publish_date;
if (publishDate > date)
return candidate;
if (candidate == null || candidate.Publish_date > publishDate)
candidate = books[i];
}
return candidate;
}
Now an example a little bit more complex but with best search performance. Algorithm is derived from an ordinary binary search (note that if you want to match the first element that matches the predicate you may use the List.BinarySearch method directly).
Note that code is untested and can be optimized too, please consider it just an example.
public static Book FindLastBookPublishedBefore(List<Book> books,
DateTime date)
{
int min = 0, max = books.Count;
Book candidate = null;
while (min < max)
{
int mid = (min + max) / 2;
Book book = books[mid];
if (book.Publish_date > date)
max = mid - 1;
else
{
candidate = book;
++min;
}
if (min >= max)
break;
}
return candidate;
}
Before moving to a more complex container you may think to keep your SortedList<T> unsorted until the first search. It'll be really slow (because it will sort the list too) but inserts will be as fast as a normal list (but you have to try with real world data). Anyway last algorithm can be optimized a lot.
Maybe if you have so many items in your collection that you can't manage them with a normal collection you may think to move everything to a database...lol
Use whatever makes your code more readable. You can simplify the code above by using lambda expressions; they are just a simplified syntax for anonymous delegates. Wherever you can use delegates you can use lambda expressions or normal methods. You would pass a normal method as argument without parentheses.
The C# compiler really just makes a hidden method for anonymous delegates and lambda expressions. You will probably not experience any difference in speed.
var result = Books.FindLast(bk => bk.Publish_date < new DateTime(2001,01,01));
Somebody please shed some light on how Add method is implemented for
(how Add method is implemented for List in c#)
listobject.Add();
where List<User> listobject= new List<User>() is the declaration for the object.
I know that using List we can perform many operations on a fly and that too with type safety, but what I wonder is how id add method implemented so that it takes care of all that at run time.
Hope it doesnot copy the object and make adjustment on each add but I will keep my fingers crossed and wait for your reply :)
Using Reflector you can see exactly how its implemented.
public void Add(T item)
{
if (this._size == this._items.Length)
{
this.EnsureCapacity(this._size + 1);
}
this._items[this._size++] = item;
this._version++;
}
Following 'EnsureCapacity' ...
private void EnsureCapacity(int min)
{
if (this._items.Length < min)
{
int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
if (num < min)
{
num = min;
}
this.Capacity = num;
}
}
And finally the setter for 'Capacity'
public int Capacity
{
get
{
return this._items.Length;
}
set
{
if (value != this._items.Length)
{
if (value < this._size)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
}
if (value > 0)
{
T[] destinationArray = new T[value];
if (this._size > 0)
{
Array.Copy(this._items, 0, destinationArray, 0, this._size);
}
this._items = destinationArray;
}
else
{
this._items = List<T>._emptyArray;
}
}
}
}
Internally the List<T> holds the items in an array. The actual implementation (List<string>) is created at compile-time runtime (thanks #Jason for the correction), so internally there will be a string array that holds the items.
For reference types the list will hold a reference to the same object instance that you added. This is true for strings as well. However, note that the string class is immutable, so any time you modify a string, it actually results in a new instance.
string a = "a";
List<string> list = new List<string>();
list.Add(a); // now the item in the list and a refer to the same string instance
a = "b"; // a is now a completely new instance, the list
// is still referring the old one
No it will work on the string reference, otherwise there wouldn't be much point in using it if it always cloned you're objects.
You can actually check this sort of thing within Visual Studio by using the memory screens, it's possible to compare the address of the added item, and the original and you'll see that they point to the same memory location.
As Frederik notes, List uses an array internally. It creates the array with an initial size and as more items are added, if the array is filled to capacity, it is copied into a larger array. That is why if you know ahead of time that a List will contain many strings, it can help to specify its initial capacity in the constructor.
When you remove items from the list, or insert in the middle, it must shift all the elements in the internal array so a List is not particularly optimized for adding/removing many items. You may be better off using a LinkedList which is much more efficient at add/remove operations but gives up the ability to efficiently access elements in the list by position.
Different collections have different implementations that are best suited for certain scenarios. For a good example of how various collections are implemented, I suggest checking out PowerCollections library that was released by Wintellect. Many of the collections are no longer relevant in .NET 3.5/4.0 but they provide some great insight on how to go about implementing collections.
For a List, the Add method will be of generic type T, in this case a string. The content of the list will be a reference to the original variable, so if you modify the string in the list it will modify the original and vice versa.