473,803 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1354
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%numOfFr amesToCheck, where numOfFramesToCh eck 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.stanfo rd.edu/~seander/bithacks.html#C ountBitsSetPara llel

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_histor y" 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.GetHistory Count() << "\n";

flag = true;

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

flag = false;

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

flag = true;

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

flag = false;

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

std::cout << "Flag count(3) is " << flag.GetHistory Count(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
2331
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 advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
0
2710
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 some text just above the class header saying "Its adapts a RoundPeg to a SquarePeg". In the class that acts as a client which isTestPegs
193
9656
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 speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
5
1284
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 DLL developed with Visual Studio 6.0 with C# code developed with Windows Forms Application(.NET)? Have an application developed with Windows Forms Application(.NET) any advantages compared to an
2
1294
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 code of a web application? Put put all logic directly into the event handlers? Use a command pattern? Use a state-event pattern? - Martin
2
2695
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> <Datasets> <Dataset name="dataset1" Pattern="abc" AnotherAttribute="xyz"/>
3
3008
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 is somewhat limited to say the least, so I'm struggling with making it work as I wand. What I have is an associative array like this: smileys = 'smile.gif'; smileys = 'sad.gif';
5
1483
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 exact kind stored in the "type" member. Each kind has different data, that is stored in a "value" array. So, if a Model is type "Car", the maximum speed is stored in value. If it's a "House", value is used to store number of inhabitants. A huge mess!...
9
1430
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 packed byte bit mask is set mask1 = 1, mask2 = 4, "123456789ABCDEF123456789ABCDEF" == "123456789ABCDEF1234x6789ABCDEx". The mask means that the character at x can match either the original number, or a set character ('x'). The original string can...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.