Dequeueing an class and individual values c# - c#

This is a follow up from yesterday's question
I have 2 queue, they contain 5 elements each called player 1 and player 2.
They were queued using something like the following
player2.Enqueue(chuckcards[i]);
Chuck cards is a class that has several different data members including 6 ints and one string. Now I wish to dequeue one item give the values to a labels (Multipul one per data member) and the string to a picture box (I don't need help with that). But I would like to know how I get the values from the dequeue. I would also like to be able to say re queue on the other players queue. So I might use the peak operation to get the values then if the outcome is successful just move it off the end of queue 1 and onto queue 2.
Hope that's clear.
Any help would be appreciated. I have searched around but found no real answer yet.

Queue<Foo> firstQueue = new Queue<Foo>();
Queue<Foo> secondQueue = new Queue<Foo>();
//todo populate
var item = firstQueue.Dequeue();
item.DoStuff();
secondQueue.Enqueue(item);

Related

Swapping data in arrays

I made Twitch bot for points and i need now !top10 command,i made it to read my points.ini file and if contains channelName that i use to store number of points and usernames in usernamesTop10 and pointsTop10 arrays.
I need now to cycle trough whole document and compare points and then swap points and username if newUser has more points.
These are steps:
1) get first username in file.
2) get points of that user.
3) record both in respective arrays.
4) get next username.
5) get points of that user.
6) compare points vs entire array and insert at point where they
have more points than the last person and fewer points than the next
person Repeat steps 4 to 6 until end of file
Here is code : http://pastebin.com/PztMj9Nw (I couldn't past it here, dunno why)
It's not finished, so please, could you help me with it? I'm not that smart i guess...
Thanks in advance, i appreciate any help!
Perhaps some help from a design standpoint might help.
I'm assuming you are storing your current top 10 in some array?
If so you should access this top 10 array and find the user with the lowest points. Keep track of this value. Lets say we call this variable 'threshold'.
To update your top 10, loop through your file calling GetPoints on every user. If you find a user with points > threshold, kick the lowest user out of the top10 and replace them with this new user. Be sure to update your new threshold value.
Repeat this until you can no longer find any users with more points than your threshold.
Additionally, if you want to keep your top 10 in order you can implement a simple sort algorithm.

List<object> Remove objects past a certain index (and Speed for doing so)

I am storing a bunch of url's in a list, There is expected to be many (around 3,000,000 at the most, more commonly around 50,000 to 200,000).
For an example of what I need to do:
ListOfStrings
[0]www.google.ca
[1]www.yahoo.com
[2]www.wikipedia.com
[3]www.youtube.com
[4]www.gmail.com
[5]www.stackoverflow.com
[6]www.steam.com
[7]www.microsoft.com
[8]www.ebay.com
I need to remove everything past string[5]. Should I just create a loop that does
while (ListOfStrings.Count>5)
{
ListOfStrings[6].Remove
}
Or is there faster method of doing this? (I was thinking possibly using .GetRange(0, ListOfStrings.IndexOf()) however there will be a maximum of 25 strings that need to be removed, so I'm not sure which one would be more efficient)
I am Also going to need to do the reverse (everything before Entry [5]) only I will need to remove many more of the strings, rather then 25 max. It will be more like 50,000 removals.
As I mentioned in comments:
If you want to don't touch the list and just want to take a subset:
If you want to take a subset from beginning of the list , you can use Take method:
var result= list.Take(5).ToList(); //The result is first 5 items
If you want to take a subset from middle of list, you can use Skip and Take methods:
var result = list.Skip(3).Take(5).ToList(); //the result is item 4 to 8
If you want to remove items from the list:
You can use RemoveRange:
list.RemoveRange(5, 4); //it changes list and removes all items above 5
Why not use Enumerable.Take()?
var result = list.Take(5);

how to implement an object[] where as top object is always the latest?

I'm writing an app where by i would like to implement an object array which will periodically have a new item added.
i would always like to know which the last object added was and was considering placing that object at position 0. (thus pushing each item down one index to a maximum of 130 items)
this is fairly easy to implement using a List using
items.Insert(0,new item());
items.RemoveAt(130);
which will then push each item down automatically and remove the item at 130 yet this is not so simple with an array
my initial thoughts where somewhere along the line of
for(int i = 129; i>0;i--)
{
items[i] = items[i-1];
}
items[0] = new item();
this then allows me simple access to the latest item (via index[0]), and each preceding item in order of creation (1 -> 129);
now in itself this is fairly simple i was wondering however if there were any other ways of performing this.
EDIT: thanks for the quick replies,
i have performed some testing on this (using 1 million iterations)
it would seem that the queue method is the quickest here but only marginally
then the list
then the array which took some 50% longer to process 1 million items
i think i'll explore the queue Stack option
thanks again;
i would always like to know which the last object added was and was
considering placing that object at position 0. (thus pushing each item
down one index to a maximum of 130 items)
...
now in itself this is fairly simple i was wondering however if there
were any other ways of performing this.
Yes, there is a simpler way of performing this by using an already builded structure which is the Queue (First Input First Output). You mainly need three methods which are:
Enqueue() adds an object to the end of the queue.
Dequeue() returns the object at the beginning of the queue removing it.
Peek() returns the object at the beginning of the queue without removing it.
Your loop is unnecessary, Array.Insert will "shift down" the elements for you.
But if you really want to always add new elements at the beginning, a LinkedList will be better for performance reasons because it doesn't need to shift elements regardless of where you insert an element.
I think that would be about as simple as you can make it. Personally I would make it a generic extension method.
public static void Insert<T>(this T[] array, int position, T item)
{
for ( int i = array.Length-1; i > position; i-- )
array[i] = array[i-1];
array[position] = item;
}
So you can use it like
string[] lolCats = { "ceiling cat", "invisible bike cat", "lime cat" };
lolCats.Insert(0, "monorailcat");

Best Class to Hold Fixed Amount of Data in C#

Lets assume I have plenty of strings that need processing, I like to place the last processed string in memory to avoid repeated processing against it. I only need to record the last 100 strings, which means if I use
List<string> oldString after oldString.Add(), I have to use oldString.TakeFromEnd(100) As you know, TakeFromEnd() not exist, which means if I go this path, I have to write lots of things to keep a 100 length List which would lead to bad performance I can imagine.
I'd like to ask, is there any pre-made in system Class that just holds a fixed amount of data, and throws away oldest data when a new one is added. Thanks
[EDIT]
Queue<string> is very good indeed, use .Any() to check if already exist, use .Enqueue() to add (not Equeue as answered below, it shot a N), use .Count to check length, and .Dequeue() to remove the first added one.
Work with a Queue
And the idea is to:
public void addToQueue(Object obj){
if (myQueue.Count > 100)
myQueue.Dequeue();
myQueue.Equeue(obj);
}
This is roughly a sketch of the code that you need to use, but you`ll get the idea.
You will then have a queue than contains only the latest 100 records

PeekRange on a stack in C#?

I have a program that needs to store data values and periodically get the last 'x' data values.
It initially thought a stack is the way to go but I need to be able to see more than just the top value - something like a PeekRange method where I can peek the last 'x' number of values.
At the moment I'm just using a list and get the last, say, 20 values like this:
var last20 = myList.Skip(myList.Count - 20).ToList();
The list grows all the time the program runs, but I only ever want the last 20 values. Could someone give some advice on a better data structure?
I'd probably be using a ring buffer. It's not hard to implement one on your own, AFAIK there's no implementation provided by the Framework..
Well since you mentioned the stack, I guess you only need modifications at the end of the list?
In that case the list is actually a nice solution (cache efficient and with fast insertion/removal at the end). However your way of extracting the last few items is somewhat inefficient, because IEnumerable<T> won't expose the random access provided by the List. So the Skip()-Implementation has to scan the whole List until it reaches the end (or do a runtime type check first to detect that the container implements IList<T>). It is more efficient, to either access the items directly by index, or (if you need a second array) to use List<T>.CopyTo().
If you need fast removal/insertion at the beginning, you may want to consider a ring buffer or (doubly) linked list (see LinkedList<T>). The linked list will be less cache-efficient, but it is easy and efficient to navigate and alter from both directions. The ring buffer is a bit harder to implement, but will be more cache- and space-efficient. So its probably better if only small value types or reference types are stored. Especially when the buffers size is fixed.
You could just removeat(0) after each add (if the list is longer than 20), so the list will never be longer than 20 items.
You said stack, but you also said you only ever want the last 20 items. I don't think these two requirements really go together.
I would say that Johannes is right about a ring buffer. It is VERY easy to implement this yourself in .NET; just use a Queue<T> and once you reach your capacity (20) start dequeuing (popping) on every enqueue (push).
If you want your PeekRange to enumerate from the most recent to least recent, you can defineGetEnumerator to do somehing likereturn _queue.Reverse().GetEnumerator();
Woops, .Take() wont do it.
Here's an implementation of .TakeLast()
http://www.codeproject.com/Articles/119666/LINQ-Introducing-The-Take-Last-Operators.aspx

Categories

Resources