04-14-2023 01:26 PM
Is there a special way or function to peek (not dequeue) the latest element added to the back.
I see one for front but not back.
04-14-2023 02:31 PM
Get Queue Status allows you to get all of the elements currently in the queue.
04-14-2023 02:36 PM
yeah, I saw some suggestions about that, but that seems to be a very inefficient way to get the last value. I'm thinking there has to be a more elegant way.
Otherwise, I'll just create a lastvalue variable and make a copy there as I enqueue. Seems to be more efficient than array conversion.
04-14-2023 04:36 PM
@shuttlefan wrote:
yeah, I saw some suggestions about that, but that seems to be a very inefficient way to get the last value. I'm thinking there has to be a more elegant way.
Otherwise, I'll just create a lastvalue variable and make a copy there as I enqueue. Seems to be more efficient than array conversion.
I don't think there's any other way, and I also don't think your alternative solution is a bad one.
04-14-2023 04:44 PM
Just understand that you *might* be opening a can of worms if you create your own "wrapper function with memory" around the normal Enqueue function.
1. Native Enqueue functions adapt to datatype seamlessly. Normal vi's don't. VIM's largely adapt to type, but not as readily as the native functions.
2. You need to be careful about reentrancy settings for your wrapper function. You might want non-reentrant, so all instances share the same "last value" memory space no matter what part of the code does the enqueuing. Or you might want pre-allocated clones, so each instance gets its own unique memory space for its own private "last value". You almost certainly *don't* want shared clones.
-Kevin P
04-15-2023 11:51 PM
My use case is that I have a library call that gets data back for speed, heading, alt, etc
Then it pushes the latest values into their respective buffer.
For a certain display widget, I may need to see historical data.
For a another display widget, I may only care about latest value. This is the use case where I want to just peek at the latest value.
I suppose for display widgets that I know will never need historical data, I can define a buffer size of 1? that way, the head and back are the same.
But that won't work if I need to have historical AND at times latest value.
04-16-2023 06:59 AM
How about using a queue for historical data and a local variable for the latest data?
04-16-2023 07:14 AM
Let me politely suggest that you *might* be doing some premature optimization that may not be necessary.
When you expressed your concerns about extracting a full array of queue elements using the preview function, I figured maybe your queue elements were large and memory-heavy - like kB per queue element or something like that. It now sounds like they're either scalar elements or perhaps a pretty limited size array or cluster of scalars.
I respect how it bothers your sense of efficiency to pull the whole array just to retrieve the latest element, but that inefficiency may very well may not be discernible at all in a practical sense. If you're just doing it a few times a second to update a few screen indicators, you're not going to run into problems.
I'd tend to recommend the "inefficient" approach over either of the following 2 approaches:
1. Maintaining a "most recent value" variable.
2. Redundant updates to both a queue and a notifier.
While either approach is probably a little more "efficient", the problem I'd have with them is that they require the "producer" part of your code to have detailed awareness of the "display" part of your code. Your decision about which parameters need to be written to a "most recent value" variable or to a notifier would be embedded in the code for your producer.
It's generally more modular and extensible if the "special logic" is contained withing the module that needs it. Here, that means that you let the producer simply enqueue without regard for the display's special needs, and then confine the special data handling to reside in the display-related code.
Again, general rule of thumb. There can be good reasons to build in tighter coupling when needed, but it's *typically* better to aim for loose coupling.
-Kevin P