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

Is this some kind of pattern?

Hi,

I have a flag to control over the time. I need to decide if the flag
has been mostly true in the last second.
I am sure this kind of problems have been solved before. Could anyone
give me a hint on this?

Thanks

Apr 21 '07 #1
6 1340
On Apr 21, 11:21 am, Giff <Giffn...@gmail.comwrote:
Hi,

I have a flag to control over the time. I need to decide if the flag
has been mostly true in the last second.
I am sure this kind of problems have been solved before. Could anyone
give me a hint on this?

Thanks
First you need to define what is "mostly true". You would probably say
that "mostly true" means the flag has been true for at least 50% of
the last second.

You would then have to poll or sample the flag often (e.g. 20 times
per second, or some other suitable rate). Each time you find the flag
true, increment a counter. Each time you find it false, do nothing.
Then, for every 20 samples:

- check if the counter is greater than or less than 10 (i.e. 50% of
the samples acquired in the last second)
- report the "mostly true" status of the flag
- reset the counter

If you are doing this under Win32, I have plenty of experience with
similar things and would be happy to send you some sample code if
interested. I usually implement stuff like this in a separate thread,
and have the thread post/send a message to another window or thread,
to report the status of the process.

Regards,
Markus.

Apr 21 '07 #2
Giff wrote:
Hi,

I have a flag to control over the time. I need to decide if the flag
has been mostly true in the last second.
I am sure this kind of problems have been solved before. Could anyone
give me a hint on this?
Do you have control of how the flag is set or reset ?
Apr 21 '07 #3
Gianni Mariani ha scritto:
Do you have control of how the flag is set or reset ?
Yes, I do it.
Apr 21 '07 #4
Markus Svilans ha scritto:
First you need to define what is "mostly true". You would probably say
that "mostly true" means the flag has been true for at least 50% of
the last second.
Precisely what I was assuming.

You would then have to poll or sample the flag often (e.g. 20 times
per second, or some other suitable rate).
The thing is that I sample it every frame (it is computer vision that I
am doing, and this method is called for each frame grabbed from the camera).

Each time you find the flag
true, increment a counter. Each time you find it false, do nothing.

What I did is to increment a int counter and then do
counter%numOfFramesToCheck, where numOfFramesToCheck should be the
frames processed in one second (now it is just set to 30).
Then I check the value and if it is 15 I declare the flag "mostly true".

It works quite fine.

If you are doing this under Win32, I have plenty of experience with
similar things and would be happy to send you some sample code if
interested.

That would definitely not harm, my email is correct, just remove .invalid
Thanks a lot for your reply.

Cheers

Apr 21 '07 #5
Giff wrote:
Gianni Mariani ha scritto:
>Do you have control of how the flag is set or reset ?

Yes, I do it.
You can use a data structure that records the last N states of the value
and then count the states when you want to know the answer. If you only
need 32 or less of the last samples, (or 64 if you have a 64 bit
machine), then you can use simple bit manipulation.

This below is a basic flag class that records history. This brute
forces the count of the number of set bits. You could keep a separate
count of the number of set bits and avoid the computation, however, the
computation only takes 20 cycles and on a modern superscalar CPU,
probably more like 12, so it depends on how often you need to count bits
as to which one is faster. I would go with the smaller class and bet
that you don't need this count very often but that's a design decision
for you.

---------------------------------------------------------------------

// Fast routine for counting number of set bits
// ... lifted from
//http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel

template <typename T>
int CountBits( T v )
{
enum { bits_in_char = 8 };

v = v - ((v >1) & (T)~(T)0/3); // temp
v = (v & (T)~(T)0/15*3) + ((v >2) & (T)~(T)0/15*3); // temp
v = (v + (v >4)) & (T)~(T)0/255*15; // temp
return (T)(v * ((T)~(T)0/255)) >(sizeof(v) - 1) * bits_in_char;
}
// ======== FlagWithHistory ===========================================
/**
* Special flag that stores the flag state for the last 32 (for 32 bit
* word systems).
*
*/

class FlagWithHistory
{
// change to unsigned long long for 64 bits on platform that support
// long long - and if you need 64 history values.
typedef unsigned int t_FlagValue;

t_FlagValue m_value;

public:

FlagWithHistory( bool i_state = false )
: m_value( i_state ? 1 : 0 )
{
}

bool Set( bool i_state )
{
m_value <<= 1;
m_value |= i_state ? 1 : 0;
return i_state;
}

bool Get()
{
return m_value & 1;
}

bool operator=( bool i_state )
{
return Set( i_state );
}

operator bool()
{
return Get();
}

static const unsigned bits_in_word = 8 * sizeof( t_FlagValue );

// GetHistoryCount gets the number of times the flag was
// set over the last "i_event_history" modifications of the
// flag.

unsigned GetHistoryCount( unsigned i_event_history = bits_in_word )
{
if ( i_event_history <= bits_in_word )
{
t_FlagValue l_mask = ~ t_FlagValue();

if ( i_event_history < bits_in_word )
{
l_mask = ( 1 << ( i_event_history ) ) -1;
}

return CountBits( m_value & l_mask );
}

throw "out of range";
}

};
#include <iostream>

int main()
{

FlagWithHistory flag( true );

std::cout << "Flag state is " << flag << "\n";

std::cout << "Flag count is " << flag.GetHistoryCount() << "\n";

flag = true;

std::cout << "Flag count is " << flag.GetHistoryCount() << "\n";

flag = false;

std::cout << "Flag count is " << flag.GetHistoryCount() << "\n";

flag = true;

std::cout << "Flag count is " << flag.GetHistoryCount() << "\n";

flag = false;

std::cout << "Flag count is " << flag.GetHistoryCount() << "\n";
std::cout << "Flag state is " << flag << "\n";

std::cout << "Flag count(3) is " << flag.GetHistoryCount(3) << "\n";

}
Apr 22 '07 #6
Gianni Mariani wrote:
>
This below is a basic flag class that records history.
Thanks, your example looks useful, I will study it.
Apr 22 '07 #7

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

Similar topics

2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
0
by: Tony Johansson | last post by:
Hello! Below is a small program using the adaptor pattern. We have four classes these are: PegAdapter, RoundPeg, SquarePeg and a class TestPegs acting like a client In class PegAdapter has...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
5
by: Tony Johansson | last post by:
Hello! Is it possible to mix C++ code for example DLL developed with Visual Studio 6.0 with C++ code developed with Windows Forms Application(.NET)? Is it possible to mix C++ code for example...
2
by: Tinu | last post by:
Hi, I have some kind of basic-design question. While developing my web application I asked myself if it makes sense to use some kind of command pattern. How do others implement the functional...
2
by: Duncan | last post by:
Can I use the contains() function within an xpath query to return a node whose attribute contains some text? Something like: selectNodes("//Dataset so for the given xml doc <root>...
3
by: Roy W. Andersen | last post by:
Hi, I need to do some replace-calls on certain strings in order to replace smiley glyphs and other keywords with graphical icons on the client. Unfortunately, my knowledge of regular expressions...
5
by: doamud | last post by:
Hello all, I've been trying to reengineer some C code to C++, but got stuck on some complex data structure: On the C code, there is an "Model" struct used to store various kind of data, the...
9
by: feck | last post by:
Hi, Got a query, don't know if anyone would be kind enough to help... I have to either match a string exactly eg "123456789ABCDEF123456789ABCDEF" == "123456789ABCDEF123456789ABCDEF", or if a...
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: 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,...
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.