473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

round-robin an arraylist

I can think of a couple ways to do this, but was wonder what the fastest way
you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This will
be called a lot, so looking for speed. This just saving the index 0 as tmp
and then enum the list to move up the elements, then put the tmp object in
last pos is the best way? Cheers

--
William Stacey, MVP
Nov 16 '05 #1
7 14234
Something like this? Or can you think of a better way? Cheers!

public static void RoundRobin(ArrayList rrSet)
{
if ( rrSet == null )
throw new ArgumentNullException("rrSet");
if ( rrSet.Count < 2 )
return;
object firstObj = rrSet[0];
for(int i = 1; i < rrSet.Count; i++)
{
rrSet[i - 1] = rrSet[i];
}
rrSet[rrSet.Count - 1] = firstObj;
}

--
William Stacey, MVP

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
I can think of a couple ways to do this, but was wonder what the fastest way you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list to move up the elements, then put the tmp object in
last pos is the best way? Cheers

--
William Stacey, MVP


Nov 16 '05 #2
William Stacey [MVP] wrote:
I can think of a couple ways to do this, but was wonder what the fastest way
you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This will
be called a lot, so looking for speed. This just saving the index 0 as tmp
and then enum the list to move up the elements, then put the tmp object in
last pos is the best way? Cheers

I Think that you need a Queue or something like that?

http://msdn.microsoft.com/library/de...classtopic.asp
Nov 16 '05 #3
Hello

Would the following do the job? Basically this doesn't do any real shifting
of elements, you just move the index of the first element, so the rotate
operation should be very fast, while the access needs extra addition and
modulo operations. I demonstrated only Rotate and access, of course you may
need to modify Add, Insert, RemoveAt, IndexOf, etc according to this change
if you need to. I also didn't put error checking in the indexer. You should
check that index is less than Count and greater than zero.

public class RoundRobinArrayList : ArrayList {
private int startIndex = 0;
public RoundRobinArrayList() {
}

void Rotate() {
startIndex++;
}

public override object this[int index] {
get { return base[(index +startIndex) % base.Count]; }
set { base[(index +startIndex) % base.Count] = value;}
}
}

Best regards,
Sherif

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
I can think of a couple ways to do this, but was wonder what the fastest way you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list to move up the elements, then put the tmp object in
last pos is the best way? Cheers

--
William Stacey, MVP

Nov 16 '05 #4
That is interesting idea. I will need to think about it as would need to
change a bunch of stuff to integrate it. The manual method is pretty fast
and by my testing the for loop is faster then removing first element and
adding it back to the end (not by much however.) Thanks again for the
reply!

--
William Stacey, MVP

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
Hello

Would the following do the job? Basically this doesn't do any real shifting of elements, you just move the index of the first element, so the rotate
operation should be very fast, while the access needs extra addition and
modulo operations. I demonstrated only Rotate and access, of course you may need to modify Add, Insert, RemoveAt, IndexOf, etc according to this change if you need to. I also didn't put error checking in the indexer. You should check that index is less than Count and greater than zero.

public class RoundRobinArrayList : ArrayList {
private int startIndex = 0;
public RoundRobinArrayList() {
}

void Rotate() {
startIndex++;
}

public override object this[int index] {
get { return base[(index +startIndex) % base.Count]; }
set { base[(index +startIndex) % base.Count] = value;}
}
}

Best regards,
Sherif

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
I can think of a couple ways to do this, but was wonder what the fastest

way
you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This

will
be called a lot, so looking for speed. This just saving the index 0 as

tmp
and then enum the list to move up the elements, then put the tmp object in last pos is the best way? Cheers

--
William Stacey, MVP



Nov 16 '05 #5
Hi William,

Oh, yes, Sherif's algorithm is very interesting. :-)

Does it meet your need? If you need further help, please feel free to
feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #6
Of all of them the work on the Array is the fastest. RoundRobinArrayList, using
the
RemoveAt(), Add() paradigm to do the shuffle is only slightly slower than the
Array
version. This is expected since internally the ArrayList is just doing the same
Copy
operation we performed. The array doesn't resize so intrinsically the same
speed is
achieved.

The for loop method is insanely slow at 4 seconds compared to only 2 tenths of a
second for the other two algorithms.

Creating the head/tail pointer version that has been suggested would by far be
the
fastest, however, it would require an entirely new collection be generated.
Depending
on how and when you do these shuffles there are some methods you could use that
would limit the number of copy operations if you do maintain the head/tail
yourself.
For instance, in an array of 50 elements with a backing store of 100 elements,
you could
shuffle the collection 50 times before you ran out of tail space to move the
first element
to. At this point you could have to copy the 50 element collection back to the
front of
your 100 element store. This isn't bad since it saves you 49 copy operations
you would
otherwise have had to incur.

using System;
using System.Collections;

public class RoundRobin {
private static void Main(string[] args) {
int iterations = 100000;

DateTime start, end;

start = DateTime.Now;
RoundRobinArray(iterations);
end = DateTime.Now;

Console.WriteLine("RoundRobinArray: {0}", end - start);

start = DateTime.Now;
RoundRobinArrayList(iterations);
end = DateTime.Now;

Console.WriteLine("RoundRobinArrayList: {0}", end - start);

start = DateTime.Now;
RoundRobinArrayList2(iterations);
end = DateTime.Now;

Console.WriteLine("RoundRobinArrayList2: {0}", end - start);
}

public static void RoundRobinArrayList2(int iterations) {
int capacity = 500;
ArrayList list = new ArrayList(capacity);
for(int i = 0; i < capacity; i++) {
list.Add(i);
}

for(int i = 0; i < iterations; i++) {
object first = list[0];
for(int j = 1; j < list.Count; j++) {
list[j - 1] = list[j];
}
list[list.Count - 1] = first;
}
}

public static void RoundRobinArrayList(int iterations) {
int capacity = 500;
ArrayList list = new ArrayList(capacity);
for(int i = 0; i < capacity; i++) {
list.Add(i);
}

for(int i = 0; i < iterations; i++) {
object tempElement = list[0];
list.RemoveAt(0);
list.Add(tempElement);
}
}

public static void RoundRobinArray(int iterations) {
int[] array = new int[500];
for(int i = 0; i < array.Length; i++) {
array[i] = i;
}

for(int i = 0; i < iterations; i++) {
int tempElement = array[0];
Array.Copy(array, 1, array, 0, array.Length - 1);
array[array.Length - 1] = tempElement;
}
}
}

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:e7****************@TK2MSFTNGP09.phx.gbl...
That is interesting idea. I will need to think about it as would need to
change a bunch of stuff to integrate it. The manual method is pretty fast
and by my testing the for loop is faster then removing first element and
adding it back to the end (not by much however.) Thanks again for the
reply!

--
William Stacey, MVP

"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:e3**************@TK2MSFTNGP12.phx.gbl...
Hello

Would the following do the job? Basically this doesn't do any real

shifting
of elements, you just move the index of the first element, so the rotate
operation should be very fast, while the access needs extra addition and
modulo operations. I demonstrated only Rotate and access, of course you

may
need to modify Add, Insert, RemoveAt, IndexOf, etc according to this

change
if you need to. I also didn't put error checking in the indexer. You

should
check that index is less than Count and greater than zero.

public class RoundRobinArrayList : ArrayList {
private int startIndex = 0;
public RoundRobinArrayList() {
}

void Rotate() {
startIndex++;
}

public override object this[int index] {
get { return base[(index +startIndex) % base.Count]; }
set { base[(index +startIndex) % base.Count] = value;}
}
}

Best regards,
Sherif

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
I can think of a couple ways to do this, but was wonder what the fastest

way
you can think of.
Want to take any arraylist of > 1 element and make the first element the
last, and all the rest of the elements will move up one position. This

will
be called a lot, so looking for speed. This just saving the index 0 as

tmp
and then enum the list to move up the elements, then put the tmp object in last pos is the best way? Cheers

--
William Stacey, MVP


Nov 16 '05 #7
> The for loop method is insanely slow at 4 seconds compared to only 2
tenths of a
second for the other two algorithms.


hmm. Must be because I only testing with 5 elements (which would be about
average for me.)

Output of code below:
---------
Time DoRoundRobin (For) 3,000,000:
Time:671.875

Time DoRoundRobin (Remove/Add) 3,000,000:
Time:578.125
----------

private void button26_Click(object sender, System.EventArgs e)
{
if ( this.testal == null )
{
testal = new ArrayList();
testal.Add("One");
testal.Add("Two");
testal.Add("Three");
testal.Add("Four");
testal.Add("Five");
}

StopWatch sw = new StopWatch();
Console.WriteLine();
Console.WriteLine("Time DoRoundRobin (For) 3,000,000:");
sw.Start();
for(int i =0; i < 3000000; i++)
{
DoRoundRobin(testal);
}
Console.WriteLine("Time:"+sw.ElapsedTime.TotalMill iseconds);

Console.WriteLine();
Console.WriteLine("Time DoRoundRobin (Remove/Add) 3,000,000:");
sw.Reset();
sw.Start();
for(int i =0; i < 3000000; i++)
{
DoRoundRobin2(testal);
}
Console.WriteLine("Time:"+sw.ElapsedTime.TotalMill iseconds);
}

public static void DoRoundRobin(ArrayList rrSet)
{
if ( rrSet == null )
throw new ArgumentNullException("rrSet");
if ( rrSet.Count < 2 )
return;
object firstObj = rrSet[0];
for(int i = 1; i < rrSet.Count; i++)
{
rrSet[i - 1] = rrSet[i];
}
rrSet[rrSet.Count - 1] = firstObj;
}
public static void DoRoundRobin2(ArrayList rrSet)
{
if ( rrSet == null )
throw new ArgumentNullException("rrSet");
if ( rrSet.Count < 2 )
return;
object firstObj = rrSet[0];
rrSet.RemoveAt(0);
rrSet.Add(firstObj);
}
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matias Silva | last post by:
Can anybody tell me why I am getting rounding errors using the ROUND function. 3.7125 rounds to 3.70 when I use the following: TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2))) The correct...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
17
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
4
by: Fuzzydave | last post by:
I have been using a round command in a few places to round a value to zero decimal places using the following format, round('+value+', 0) but this consistantly returns the rounded result of...
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545...
3
by: Krishna.K.1900 | last post by:
Does round() always perfectly return the output expected or are there some artifacts which don't allow perfect functionality Using python 2.5: 12.23 12.234 12.199999999999999 but was...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
9
by: josh logan | last post by:
Hello, I need a round function that _always_ rounds to the higher integer if the argument is equidistant between two integers. In Python 3.0, this is not the advertised behavior of the built-in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.