473,387 Members | 1,516 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,387 software developers and data experts.

queue like operation on array of structs

I have a struct which holds an array of structs like this

struct Struct1
{
struct Struct2* strt2 ;
....
};

I allocate a predetermined size to hold n structs of type Struct2.
However, Struct2 represents data that is coming in from a feed. I want
to have an operation that "shifts" the array elements down by one when a
new packet (of type Struct2) is received, so that Struct1 ALWAYS
contains the latest n values.

memove comes to mind, but I'd appreciate some pointers (pun intended)
May 2 '07 #1
4 1634
On May 2, 3:16 am, "2b|!2b==?" <r...@your.box.comwrote:
I have a struct which holds an array of structs like this

struct Struct1
{
struct Struct2* strt2 ;
....

};

I allocate a predetermined size to hold n structs of type Struct2.
However, Struct2 represents data that is coming in from a feed. I want
to have an operation that "shifts" the array elements down by one when a
new packet (of type Struct2) is received, so that Struct1 ALWAYS
contains the latest n values.

memove comes to mind, but I'd appreciate some pointers (pun intended)
The first question comes to mind is: do you really need to move
the data? It's generally easier to implement some sort of
ring buffer:
struct Struct2 *current;
current = ((struct Struct1 *)a)->strt2;
for( ;;) {
overwrite_current_from_feed( current );
if( current - a->strt2 N )
current = a->strt2;
else
current += 1;
}

So current always points to the oldest packet in the
buffer (and is probably misnamed).

May 2 '07 #2
2b|!2b==? wrote:
I have a struct which holds an array of structs like this

struct Struct1
{
struct Struct2* strt2 ;
....
};

I allocate a predetermined size to hold n structs of type Struct2.
However, Struct2 represents data that is coming in from a feed. I want
to have an operation that "shifts" the array elements down by one when a
new packet (of type Struct2) is received, so that Struct1 ALWAYS
contains the latest n values.

memove comes to mind, but I'd appreciate some pointers (pun intended)
A "circular buffer" comes to mind. Keep the array at
the fixed size n (if n is really truly fixed, you might just
make strt2 an array instead of a pointer), and don't move
things around at all. Instead, keep an index k that tells
where the newest[*] item is. To insert a still newer item,
set k = (k + 1) % n and deposit the newer item there; it
will automatically overwrite the oldest item. To inspect
all the n newest items from newest to oldest, loop through
k, k-1, ..., 0, n-1, ..., k+1 (again, the % operator may
come in handy). To inspect them oldest to newest, loop on
k+1, ..., n-1, 0, ..., k.
[*] Variations: let the index point at the oldest item,
and/or use k = (k - 1) % n to run the index in the opposite
direction. Use whichever convention fits most neatly with
whatever else you're doing to the items.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 2 '07 #3


2b|!2b==? wrote:
I have a struct which holds an array of structs like this

struct Struct1
{
struct Struct2* strt2 ;
....
};

I allocate a predetermined size to hold n structs of type Struct2.
However, Struct2 represents data that is coming in from a feed. I want
to have an operation that "shifts" the array elements down by one when a
new packet (of type Struct2) is received, so that Struct1 ALWAYS
contains the latest n values.

memove comes to mind, but I'd appreciate some pointers (pun intended)
Thanks for the replies guys. However it appears I forgot to mention that
I need to look at the HISTORY of the packets rather than just the
current/latest packet. So that I am able to carry statistical analysis
on the data every time a new packet arrives. This is why I need to
maintain a "window" of the latest N packets. This involves "pushing" all
the elements of the array down by one index when a new packet arrives -
if I correctly understand the suggestions made so far, they do not
address the issue I raised (i.e. maintaining an ordered set of the
latest N items, i.e, in the order recieved) - unless I'm missing
something ??
May 2 '07 #4
2b|!2b==? wrote, On 02/05/07 07:59:

<snip>
Thanks for the replies guys. However it appears I forgot to mention that
I need to look at the HISTORY of the packets rather than just the
current/latest packet. So that I am able to carry statistical analysis
on the data every time a new packet arrives. This is why I need to
maintain a "window" of the latest N packets. This involves "pushing" all
the elements of the array down by one index when a new packet arrives -
if I correctly understand the suggestions made so far, they do not
address the issue I raised (i.e. maintaining an ordered set of the
latest N items, i.e, in the order recieved) - unless I'm missing
something ??
Sounds to me like you need to implement a circular buffer. To do this
you define an array which is large enough (or malloc it if the size is
determined at run time).

Depending on situation you can then handle it one of two ways. Have one
index (or pointer) to the newest entry which is wrapped back to the
start of the array when it reaches the end and a flag to say when you
have got a full buffer. Since you always want the last N elements
available this is probably appropriate.

Alternatively, if something is emptying the data as well, you also keep
a pointer to the tail of the buffer, and you have to obviously check for
when head meets tail (buffer full) or tail meets head (buffer empty).

A search for "circular buffer" should turn up lots of hits with more
detailed explanations and implementations.
--
Flash Gordon
May 2 '07 #5

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

Similar topics

6
by: herrcho | last post by:
#include <stdio.h> #define MAX 10 int queue; int front, rear; void init_queue() { front=rear=0; }
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
6
by: rmunson8 | last post by:
I have a derived class from the Queue base class. I need it to be thread-safe, so I am using the Synchronized method (among other things out of scope of this issue). The code below compiles, but...
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,...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
10
by: John | last post by:
I want to write a code for Breadth First Traveral for Graph, which needs a queue to implement. I wonder that for such a powerful language as Python, whether there is a better and simpler...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.