I'm trying to get an element at a specific index with the method :
public T GetElem(int index)
{
var it = outerInstance.iterator();
var i = 0;
while (it.MoveNext() && i < index)
{
i++;
}
return it.Current;
}
I have created my own iterator which is an internal class of the outerInstance,during debug the current element is decreasing at the end it becomes null.
My test it's inside console app and looks like this :
Storage<int?> storage = new DynSLinkedList<int?>();
var ranked = new Ranked<int?>(storage);
if (ranked.IsEmpty())
{
Console.WriteLine("is empty \n");
}
for (var i = 1; i <= 10; i++)
ranked.Add(i);
if (!ranked.IsEmpty())
{
Console.WriteLine("is not empty \n");
}
if (ranked.Size() == 10)
{
Console.WriteLine("Size ok \n");
}
for (var i = 0; i <= 9; i++)
{
var element = ranked.GetElem(i);
if (element == 10 - i)
{
Console.WriteLine("get " + i + " elem ok \n");
}
}
Only for i=0 it gets right.
I have tried to write the equivalent for the java method:
#Override
public T getElem(int index) throws IndexOutOfBoundsException {
RWIterator<T> it=support.iterator();
int i=0;
while (it.hasNext() && i<index){
it.next();
i++;
}
return it.next();
}
Your problem is that you are using a single instance to iterate over in the Ranked.GetElem method. The first time you call ranked.GetElem, passing 0, the iterator will be moved one step (it.MoveNext).
At this point the iterator is already pointing to the second element in your list. The next time you call ranked.GetElem, passing 1, the iterator will be moved two spaced further, ending up returning the third element as opposed to the one you are expecting (the second). So on and so forth.
You need to either change the overall approach you are taking or reset the iterator inside the GetElem method so it always starts from the first element.
Try this (assuming you are implementing the Reset() method correctly):
public T GetElem(int index)
{
var it = outerInstance.iterator();
it.Reset();
var i = 0;
while (it.MoveNext() && i < index)
{
i++;
}
return it.Current;
}
Related
I'm currently working in a program which contains a class named LinkedList. It uses nodes to create the functions Add at beginning, Insert At Position, Get Info at position and Eliminate at position. I'm having trouble with the Get function because it's not getting the correct value from the list and I don't know exaclty why.
The definition for Node in the program is:
public class Node<T>
{
public T value;
public Node<T> next;
}
The code for the Get function is:
public T Get(int index)
{
if (start == null)
{
throw new Exception("Nothing to show");
}
if (index == 0)
{
return start.value;
}
Node<T> previous = start;
for (int i = 0; i < index; i++)
{
previous = previous.next;
}
return previous.value;
}
Also, I have these tests where my code is failing when the Get is used. The lists are created when the class LinkedList is used an then, it uses his attributes to invoke the Add, Get, AddAt and Eliminate functions in the tests.
[Test]
public void AddMultipleThenGetAll ()
{
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.Add(42);
linkedList.Add(43);
linkedList.Add(44);
int[] results = new int[] { 42, 43, 44 };
for (int i = 0; i < results.Length; i++)
{
if (linkedList.Get(i) != results[i])
Assert.Fail();
}
Assert.Pass();
}
[Test]
public void AddMultipleAndRemoveInTheMiddle()
{
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.Add(42);
linkedList.Add(43);
linkedList.Add(44);
linkedList.Remove(1);
Console.WriteLine(linkedList);
Assert.AreEqual(44, linkedList.Get(1));
}
The rest of functions are working properly.
Your for loop in your get method has a bug. You are looping all the way to your desired index then setting previous to the next item, which doesn't exist or it isn't the index / item you want. Try this instead.
Node<T> previous = start;
for (int i = 0; i < index - 1; i++)
{
previous = previous.next;
}
return previous.value;
I've been trying to do the following:
public List<List<object[]>> Queue = new List<List<object[]>>();
Queue = InitList(MaxLayerCapability, new List<object[]>());
Having
public List<T> InitList<T>(int count, T initValue)
{
return Enumerable.Repeat(initValue, count).ToList();
}
So here is where the issue resides:
Queue[2].Add(new object[] { "Draw", "Test" });
for ( int i = 0; i < MaxLayerCapability; i++)
{
Console.WriteLine(i + ">" + Queue[i].Count);
//Operate(Queue[i], i);
}
For some reason, I want that Queue[2] to contains elements, and all other lists (for example, Queue[0]) should have a count of 0.
It is at some point pushing all the elements into Queue, any ideas?
Here's what I'm getting:
0>1
1>1
2>1
3>1
4>1
5>1
Thanks in advance.
The problem is, as i understand it, that Repeat just repeats reference to the same instance of List.
I would refactor it this way:
public IEnumerable<T> InitList<T>(int count)
{
for (int i=0;i<count; i++)
{
yield return Activator.CreateInstance<T>();
}
}
I see correct results if i use fucntion this way:
Queue = InitList<List<object[]>>(3).ToList();
this is my very first post here on StackOverflow so please tell me if I did anything wrong, also english is not my native language, forgive me if there is any gramatical errors.
My question is how can I permutate the items of an array of type "Location", I need to get all possible permutations of waypoints given by the user to then calculate the best route based on time or distance. (I don't want to use the normal route calculation)
I've searched for algorithms but all of them when I put the array of type "Location[]" in the function's parameter I get the error that the object needs to be IEnumerable and I don't know how to convert to that if is even possible, I never worked with IEnumerable.
If it is of any help this is my code for calculating the route:
//Gets the waypoints from a listBox provided by the user, "mode" selects between best time and best distance
//backgroundworker so the UI dont freezes, and return the optimal waypoint order
public Location[] CalcularRota(Location[] waypoints, int mode, BackgroundWorker work, DoWorkEventArgs e)
{
//Declarations
string origem = "";
string destino = "";
Rota[] prop = new Rota[100]; //this index is the number of times the algorithm will be executed, more equals accuracy but much more time to complete
Rota bestDist = new Rota();
Rota bestTime = new Rota();
DirectionService serv = new DirectionService();
DirectionRequest reqs = new DirectionRequest();
DirectionResponse resp;
Random rnd = new Random();
Location[] rndWays;
int dist = 0;
int ti = 0;
bestDist.Distance = 1000000000; //put higher values for the first comparation to be true (end of code)
bestTime.Time = 1000000000;
if (waypoints != null)
{
reqs.Sensor = false;
reqs.Mode = TravelMode.driving;
for (int i = 0; i < prop.Length; i++) //initializes prop
prop[i] = new Rota();
for (int i = 0; i < prop.Length; i++)
{
rndWays = waypoints.OrderBy(x => rnd.Next()).ToArray(); //randomizes the order, I want to get all permutations and then test
//but I dont know how so I've been using randomized
dist = ti = 0;
origem = prop[0].ToString(); //save this particular waypoint's origin and destination
destino = prop[1].ToString();
reqs.Origin = origem;
reqs.Destination = destino;
if (waypoints.Length > 0)
reqs.Waypoints = rndWays;
resp = serv.GetResponse(reqs); //request the route with X order of waypoints to google
if (resp.Status == ServiceResponseStatus.Ok) //wait the response otherwise the program crashes
{
for (int j = 0; j < resp.Routes[0].Legs.Length; j++) //gets the distance and time of this particular order
{
ti += int.Parse(resp.Routes[0].Legs[j].Duration.Value);
dist += int.Parse(resp.Routes[0].Legs[j].Distance.Value);
}
}
prop[i].Origem = origem; //saves this waypoints order details for further comparison
prop[i].Destino = destino;
prop[i].Distance = dist;
prop[i].Time = ti;
prop[i].Order = rndWays;
work.ReportProgress(i); //report the progress
}
for (int i = 0; i < prop.Length; i++) //gets the best distance and time
{
if (bestDist.Distance > prop[i].Distance)
{
bestDist.Distance = prop[i].Distance;
bestDist.Time = prop[i].Time;
bestDist.Order = prop[i].Order;
bestDist.Origem = prop[i].Origem;
bestDist.Destino = prop[i].Destino;
}
if (bestTime.Time > prop[i].Time)
{
bestTime.Distance = prop[i].Distance;
bestTime.Time = prop[i].Time;
bestTime.Order = prop[i].Order;
bestTime.Origem = prop[i].Origem;
bestTime.Destino = prop[i].Destino;
}
}
if (bestDist.Order == bestTime.Order) //if the same waypoint order has the same time and distance
return bestDist.Order; // returns whatever bestDist.Order or bestTime.Order
else if (bestDist.Order != bestTime.Order) //if different returns corresponding to the mode selected
{
if (mode == 1) return bestDist.Order;
if (mode == 2) return bestTime.Order;
}
}
return null;
}
What I want is to permutate the waypoints given and test each permutation, I've been struggling with this for a time, if u guys could help me with any way possible would be great.
Ty.
EDIT.
I found this function here on StackOverflow:
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
{
var count = elements.Length;
var done = true;
for (var i = count - 1; i > 0; i--)
{
var curr = elements[i];
// Check if the current element is less than the one before it
if (curr.CompareTo(elements[i - 1]) < 0)
{
continue;
}
// An element bigger than the one before it has been found,
// so this isn't the last lexicographic permutation.
done = false;
// Save the previous (bigger) element in a variable for more efficiency.
var prev = elements[i - 1];
// Have a variable to hold the index of the element to swap
// with the previous element (the to-swap element would be
// the smallest element that comes after the previous element
// and is bigger than the previous element), initializing it
// as the current index of the current item (curr).
var currIndex = i;
// Go through the array from the element after the current one to last
for (var j = i + 1; j < count; j++)
{
// Save into variable for more efficiency
var tmp = elements[j];
// Check if tmp suits the "next swap" conditions:
// Smallest, but bigger than the "prev" element
if (tmp.CompareTo(curr) < 0 && tmp.CompareTo(prev) > 0)
{
curr = tmp;
currIndex = j;
}
}
// Swap the "prev" with the new "curr" (the swap-with element)
elements[currIndex] = prev;
elements[i - 1] = curr;
// Reverse the order of the tail, in order to reset it's lexicographic order
for (var j = count - 1; j > i; j--, i++)
{
var tmp = elements[j];
elements[j] = elements[i];
elements[i] = tmp;
}
// Break since we have got the next permutation
// The reason to have all the logic inside the loop is
// to prevent the need of an extra variable indicating "i" when
// the next needed swap is found (moving "i" outside the loop is a
// bad practice, and isn't very readable, so I preferred not doing
// that as well).
break;
}
// Return whether this has been the last lexicographic permutation.
return done;
}
The usage is:
NextPermutation(array);
Doing this and putting my array (rndWays) as overload I get the following error:
The type 'Google.Maps.Location' cannot be used as type parameter 'T' in the generic type or method 'Form1.NextPermutation< T >(T[])'. There is no implicit reference conversion from 'Google.Maps.Location' to 'System.IComparable< Google.Maps.Location >'.
The problem is that Location does not implement the IComparable interface.
Change:
public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>
to:
public static bool NextPermutation(Location[] elements)
And replace each CompareTo() with your own comparison function.
I have the following code that compares the node.Texto each both given node sets and then return one if they are equal otherwise zero. But my problem is that it just compare the first children because of nodes2.Nodes[ii] hence I know that it will not go forward more.
As I know if it was TreeNodeCollection it was easy to do recursive for each node and sub-node using foreach loop.
But here how I could change the code to the recursive version?
thanks in advance!
public int Compare_ChildNodes(TreeNode nodes1, TreeNode nodes2)
{
int length_children1 = nodes1.Nodes.Count;
int length_children2 = nodes2.Nodes.Count;
int result_int = 1;
if (length_children1 != length_children2)
{
result_int = 0;
}
else
{
for (int ii = 0; ii < length_children1; ii++)
{
if (nodes1.Nodes[ii].Text.Equals(nodes2.Nodes[ii].Text))
{
int ret = Compare_ChildNodes(nodes1.Nodes[ii], nodes2.Nodes[ii]);
result_int = ret;
}
else
{
result_int = 0;
}
}
}
return result_int;
}
I can't see any problems here. Calling Compare_ChildNodes with nodes1.Nodes[ii] and nodes2.Nodes[ii] does exactly the recursion you want.
I'd just suggest a little optimization ("early out") for your code:
public int Compare_ChildNodes(TreeNode nodes1, TreeNode nodes2)
{
int length_children1 = nodes1.Nodes.Count;
int length_children2 = nodes2.Nodes.Count;
int result_int = 1;
if (!nodes1.Text.Equals(nodes2.Text)) return 0; // not equal
if (length_children1 != length_children2) return 0; // not equal
return nodes1.Nodes.OfType<TreeNode>.Select((node, idx) =>
Compare_ChildNodes(node, nodes2.Nodes[idx]).Any(result => result == 0) ? 0 : 1;
}
I changed the comparison for the node's text to another recursion level so you can recurse using linq.
The linq Any method checks if any of the comparisons (in the Select method) returns 0 indicating that a child node collection is not equal.
The Select is called for each TreeNode in node1.Nodes and with it's index in that collection. So you can use this index to get the matching node from node2.Nodes and call Compare_ChildNodes for these two.
As soon as you find unequal (child-)nodes, you can return 0 and don't need to continue comparing the other nodes.
If you cannot use the linq statements (for Framework reasons or others), you can still use your for loop:
for (int idx = 0; idx < length_children1; idx++)
if (Compare_ChildNodes(nodes1.Nodes[idx], nodes2.Nodes[idx]) == 0)
return 0;
return 1;
Is there a more elegant way to implement going 5 items at a time than a for loop like this?
var q = Campaign_stats.OrderByDescending(c=>c.Leads).Select(c=>c.PID).Take(23);
var count = q.Count();
for (int i = 0; i < (count/5)+1; i++)
{
q.Skip(i*5).Take(5).Dump();
}
for(int i = 0; i <= count; i+=5)
{
}
So you want to efficiently call Dump() on every 5 items in q.
The solution you have now will re-iterate the IEnumerable<T> every time through the for loop. It may be more efficient to do something like this: (I don't know what your type is so I'm using T)
const int N = 5;
T[] ar = new T[N]; // Temporary array of N items.
int i=0;
foreach(var item in q) { // Just one iterator.
ar[i++] = item; // Store a reference to this item.
if (i == N) { // When we have N items,
ar.Dump(); // dump them,
i = 0; // and reset the array index.
}
}
// Dump the remaining items
if (i > 0) {
ar.Take(i).Dump();
}
This only uses one iterator. Considering your variable is named q, I'm assuming that is short for "query", which implies this is against a database. So using just one iterator may be very beneficial.
I may keep this code, and wrap it up in an extension method. How about "clump"?
public static IEnumerable<IEnumerable<T>> Clump<T>(this IEnumerable<T> items, int clumpSize) {
T[] ar = new T[clumpSize];
int i=0;
foreach(var item in items) {
ar[i++] = item;
if (i == clumpSize) {
yield return ar;
i = 0;
}
}
if (i > 0)
yield return ar.Take(i);
}
Calling it in the context of your code:
foreach (var clump in q.Clump(5)) {
clump.Dump();
}
try iterating by 5 instead!
for(int i = 0; i < count; i += 5)
{
//etc
}
Adding more LINQ with GroupBy and Zip:
q
// add indexes
.Zip(Enumerable.Range(0, Int32.MaxValue),(a,index)=> new {Index=index, Value=a})
.GroupBy(m=>m.Index /5) // divide in groups by 5 items each
.Select(k => {
k.Select(v => v.Value).Dump(); // Perform operation on 5 elements
return k.Key; // return something to satisfy Select.
});