Click here to Skip to main content
15,902,114 members
Home / Discussions / C#
   

C#

 
AnswerRe: Count bitpattern occurences in textstrings Pin
Ajay.k_Singh14-Jan-08 23:11
Ajay.k_Singh14-Jan-08 23:11 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus314-Jan-08 23:29
invictus314-Jan-08 23:29 
GeneralRe: Count bitpattern occurences in textstrings Pin
Russell Jones15-Jan-08 0:02
Russell Jones15-Jan-08 0:02 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 0:05
invictus315-Jan-08 0:05 
GeneralRe: Count bitpattern occurences in textstrings Pin
Russell Jones15-Jan-08 0:45
Russell Jones15-Jan-08 0:45 
GeneralRe: Count bitpattern occurences in textstrings [modified] Pin
Luc Pattyn15-Jan-08 0:56
sitebuilderLuc Pattyn15-Jan-08 0:56 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 23:20
invictus315-Jan-08 23:20 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn16-Jan-08 8:56
sitebuilderLuc Pattyn16-Jan-08 8:56 
Hi,

here is my suggested algorithm:

- looking for a pattern of length P;
- have an accumulator that holds a sliding part of the binary data stream; one way to do
that is to initialize it to zero, then inside a loop shift it left over B positions and
ORing B bits of new data (B could be 8 or 16, the accumulator's bit count must
equal or exceed B+P-1);
- have a method that counts the number of pattern matches in the rightmost B+P-1 bits
of the accumulator, i.e. ignore the higher bits in the accumulator;
doing so makes sure every match will be counted only once.

There are basically two ways to do the count method:
1. inside a loop say for(x=0; x<b; x++),="" mask="" away="" all="" but="" p="" bits="" and="" compare="" those<br="" mode="hold"> with the pattern; so have a mask initially equal to (1<<P)-1, AND with accumulator,
compare with pattern; then shift left mask and pattern and compare again, etc.
2. if B+P-1 is small, say less than 16, you can precalculate an array with the
count value for every possible accumulator value, so the count method is just
a single array lookup.

Remarks:
- even if you think of your data as 16-bit items (say Unicode characters), you can still
work with B=8 if you choose to do so, the algorithm does not care what the bit
stream means;
- the sliding accumulator works best if the most-significant-bit of data item 1
connects to the least-significant-bit of data item 0; otherwise it will take
more complex load-and-shift operations.
- the algorithm requires B+P-1 not to exceed the length of the largest available integer
type; otherwise "big integer" techniques become necessary;
- precalculating an array is a common way to drastically improve performance provided
there is sufficient data to process; here the array's dimension is only 1<<(B+P-1)
which is 512 for B=8 and P=2.
- obviously when B+P-1 becomes large, allocating and precalculating an array may become
prohibitive and the loop-implementation may be prefered.
- when the pattern size increases AND the count of all possible pattern values is
required, it may not be obvious what the best order of execution would be:
either first loop over the pattern values, and scan the data multiple times,
or allocate 1<<P arrays and do all pattern values in parallel, scanning the data
only once.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.


GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 8:41
invictus319-Jan-08 8:41 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 9:13
sitebuilderLuc Pattyn19-Jan-08 9:13 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 11:16
invictus319-Jan-08 11:16 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 11:36
sitebuilderLuc Pattyn19-Jan-08 11:36 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus319-Jan-08 12:57
invictus319-Jan-08 12:57 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn19-Jan-08 14:01
sitebuilderLuc Pattyn19-Jan-08 14:01 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 0:09
invictus321-Jan-08 0:09 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn21-Jan-08 0:45
sitebuilderLuc Pattyn21-Jan-08 0:45 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 0:57
invictus321-Jan-08 0:57 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn21-Jan-08 1:09
sitebuilderLuc Pattyn21-Jan-08 1:09 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus321-Jan-08 22:41
invictus321-Jan-08 22:41 
GeneralRe: Count bitpattern occurences in textstrings Pin
Luc Pattyn22-Jan-08 1:07
sitebuilderLuc Pattyn22-Jan-08 1:07 
GeneralRe: Count bitpattern occurences in textstrings Pin
Ennis Ray Lynch, Jr.15-Jan-08 11:00
Ennis Ray Lynch, Jr.15-Jan-08 11:00 
GeneralRe: Count bitpattern occurences in textstrings Pin
invictus315-Jan-08 23:10
invictus315-Jan-08 23:10 
GeneralRe: Count bitpattern occurences in textstrings Pin
Ennis Ray Lynch, Jr.16-Jan-08 4:11
Ennis Ray Lynch, Jr.16-Jan-08 4:11 
GeneralRe: Count bitpattern occurences in textstrings Pin
PIEBALDconsult15-Jan-08 15:41
mvePIEBALDconsult15-Jan-08 15:41 
GeneralCatching MouseMove Event Pin
stancrm14-Jan-08 21:16
stancrm14-Jan-08 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.