473,404 Members | 2,137 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,404 software developers and data experts.

variable length FIFO Queue with constant total element value.

Hi All,
I have a strange requirement.
I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc....
( each input number range from 1 to 10 ).

lets take a simple case where all inputs will have same value = 1 (
which may not be the case )
I need to create a FIFO queue in which at any given time I need the
total element value = 10. ( not the size of array ).
say if the data stream is all 1 e.g. 1,1,1,1,1,1,1,1,1
in this case the queue should accomodate 10 elements and than start
removing one element from the front as a one element with value = 1
gets added to the queue end.

if the data stream = 2,2,2,4,5 .. etc in this case the queue should get
filled til 2 + 2 + 2 + 4 ( which = 10 ) than when 5 is added to queue
then elemeent totalling to 5 should be removed from the front of the
queue. e.g in this scenario after adding 5 to the end of queue , the
queue should look 1, 4, 5 ( notice the first and the second 2 were
removed and one was substracted from the third 2 which left us with 1 )

basicallly at any given time the total value of all elements in the
queue should be exactly = 10.

any C# solution will be good , or worst case stl soultion might do as
well.

Regards,
Manish

Nov 16 '05 #1
2 3067
How about something like this?

public class ConstantTotalNumberStream
{
private int target;
private int total;
private int[] stream;
private int startIndex;
private int endIndex;
private bool streamChanged;

public ConstantTotalNumberStream(int totalToMaintain)
{
this.target = totalToMaintain;
this.stream = new int[totaltoMaintain];
for (int i = 0; i < totalToMaintain; i++)
{
this.stream[i] = 0;
}
this.total = 0;
this.startIndex = 0;
this.endIndex = 0;
this.streamChanged = false;
}

public AddNumber(int newNumber)
{
if (newNumber <= 0 || newNumber > this.total)
{
throw new ArgumentException(String.Format("Number to add must
be between 1 and {0}, not {1}", this.total, newNumber));
}
Push(newNumber);
while (this.total > this.target)
{
if (this.total - this.stream[this.startIndex] >= this.target)
{
Pop();
}
else
{
this.stream[this.startIndex] -= this.total - this.target;
this.total = this.target;
}
}
}

private void Push(int newNumber)
{
this.endIndex = (this.endIndex + 1) % this.total;
if (this.endIndex == this.startIndex)
{
// Array is full... have to pop one element in order to make
room
Pop();
}
this.stream[this.endIndex] = newNumber;
this.total += newNumber;
}

public void Pop()
{
if (this.startIndex != this.endIndex)
{
this.total -= this.stream[this.startIndex];
this.startIndex = (this.startIndex + 1) % this.target;
}
}

public int Total
{
get { return this.total; }
}

public int Count
{
get
{
int count = this.endIndex - this.startIndex;
if (count < 0)
{
count += this.target;
}
return count;
}
}

public int this[int index]
{
get
{
if (this.startIndex == this.endIndex)
{
throw new IndexOutOfRangeException("Cannot index stream
because it contains no numbers");
}
else if (index < 0 || index >= this.Count)
{
throw new IndexOutOfRangeException(String.Format("Index {0}
is outside valid range 0 to {1}", index, this.Count - 1));
}
int trueIndex = (index + startIndex) % this.target;
return this.stream[trueIndex];
}
}
}

Nov 16 '05 #2
Of course, that should have been:

private void Pop()
....

Nov 16 '05 #3

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

Similar topics

4
by: AJ | last post by:
Hi, I have 20 pictureBoxes named from PictureBox1 to PictureBox20. I want to fill them up one by one. I'm keeping a variable firstAvail to keep up with which pictureBox I should insert the next...
38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
2
by: Avi Laviad | last post by:
hi, sorry for the dumb question but how do i code a bit variable in c? i mean - shoudlnt the synax need to be: bit j; j = 1; correct me if im wrong (and i suppose i am). Avi.
8
by: Jack | last post by:
I want to implement a fixed-size FIFO queue for characters. I only want to use array not linked list. For example, const int N = 10; char c_array; The question is when the queue is full,...
0
by: kmoeWW | last post by:
Hi all, I'm using the PICC CCS C compiler and am having issues with starting an enque/deque for my program. What I need it to do is to hold about 9 data packets, each packet is an array of 4 HEX...
5
by: akenato | last post by:
Hi for me this is th first time that I use c++. I have to do this exercise. Somebody can help me? Thanx. Implement a FIFO-queue ( first in, first out ) as a ring buffer. Use a static array as the...
7
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
40
by: sazd1 | last post by:
Hi Student2 I am working on similar kind of thing for stock calculation but could not find any solution to my problem even after putting my problem to different forums. I saw your post that you...
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: 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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.