|
Actually I was considering the suggestion about array of pre-calculated values, but I couldn't think of a good way of making the bits between the bytes (last bit from one byte and the first one from the next) count.
Not sure if I understand your larger accumulator approach though...
|
|
|
|
|
The simplest way to look at it is to have an accumulator that holds two consecutive characters,
which you achieve by shift-and-add or shift-and-or. The trick is to AND away the extraneous
bits that would cause some positions to be counted twice, hence the P+B-1 stuff.
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.
|
|
|
|
|
Thats almost what I did if I understand correctly. Although I did only use one character and just shifted the needed bit from the next character into its correct position and then OR'ed it with the one character.
But I think I understand the difference...like:
int16 holding b[0] and b[1]
then shift it 1 position at a time until its shiftet 8 times (therefore all bits from b[0] have been in the MSB position while the first bit from b[1] being in the consecutive position to the right from the MSB...and then jump to b[1] and repeat....
|
|
|
|
|
You still have not fully understood my original reply[^] where I use one (or more) precalculated arrays to do the counting.
So counting all the "00" patterns in one data item takes:
- one accumulator adjustment (shift+or)
- one mask
- one array lookup.
And counting all the "00", "01", "10" and "11" patterns in one data item takes:
- one accumulator adjustment (shift+or)
- one mask
- four array lookups.
i.e. I never treat bits individually, I always work B bits at a time.
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.
|
|
|
|
|
I wrote a hybrid solution. The first count and the generateTable are just used to make the table so ignore those. The last method is the actual counting. As you can see I look up the entire byte, BUT to count the "in between" bits I use my old method.
Any feedback to this solution? At least I am getting closer :p
<br />
static int[] count(int x, int q)
{<br />
int size = (int)Math.Pow(2, q);<br />
int mask = size - 1;<br />
int[] vector = new int[size];<br />
<br />
for (int j = 8 - q; j >= 0; j--)<br />
{<br />
vector[((x >> j) & mask)]++;<br />
}<br />
<br />
return vector;<br />
}<br />
<br />
static int[][] generateTable()
{<br />
int[][] table = new int[256][];<br />
<br />
for (int i = 0; i < 256; i++)<br />
table[i] = G_Binary2(i, 2);<br />
<br />
return table;<br />
}<br />
<br />
static int[] count(string s, int[][] table)
{<br />
const int mask = 0x03;<br />
int[] vector = new int[4];<br />
<br />
for (int i = 0; i < s.Length; i++)<br />
{<br />
int x = s[i] & 0xFF;<br />
for (int j = 0; j < 4; j++)<br />
vector[j] += table[x][j];<br />
if(i < s.Length-1)<br />
vector[(((s[i] << 1) | (s[i + 1] >> 7)) & mask)]++;<br />
}<br />
<br />
return vector;<br />
}<br />
|
|
|
|
|
static int[] GetCounts(string s, int P, int[][] table) {
int powerP=1<<P;
int[] counts=new int[powerP];
int mask=(0x80<<P)-1;
int acc=0;
foreach (char c in s) {
acc=(acc<<8) | (((int)c) & 0xFF);
for (int pat=0; pat<powerP; pat++) counts[pat]+=table[pat][acc & mask];
}
return counts;
}
BTW: the above is off by one (for P==2), you may or may not care to fix it (not very easy without
performance penalty)
PS: don't use pow() in an integer application, it is both slower and potentially inaccurate.
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.
|
|
|
|
|
Thanks! I was not aware of the problem with pow(). is there any similar integer issue/fix with Math.Abs() and Math.sqrt() as well?
|
|
|
|
|
invictus3 wrote: s there any similar integer issue/fix with...
each time you convert to and from reals for no good reason you may loose performance, and
may get wrong results due to limited accuracy and/or rounding errors.
x=iabs(x) ==> if (x<0) x=-x;
x=isqrt(x) ==> no simple int solution, but one seldom needs isqrt
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.
|
|
|
|
|
I am grateful for the help.
I got one final question though. Its more relating to the last topic, Math.pow(). I would run Math.pow(94, x), but since you told me that pow was inefficient for integer operations I wonder what is the better way to solve this?
|
|
|
|
|
for loop
while loop
loop + array
94?
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.
|
|
|
|
|
Use unsafe code, get the pointer to the string data and then cast it into byte or int or whatever size you want. Then use the binary operator & to do the comparison.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
This was an interesting idea.
Lets assume I have a string that consists only of the ascii symbols from code 32 and up to 126. Now each character must be at least 7 bits long (but probably are 8 as a result of byte of 8 bits). This leads to alot of possible combinations to be checked with the & operator if I want to check for all possible 2-bit patterns (even the one overlapping into the next character).
If I make the assumption that characters are 8 bits and include the first 0 in my calculations (to make it easier) I would still have to test against all possible combinations of 00, 01, 10, and 11 at all two consecutive slots within each character. Furthermore, I would have to test each of the four against the last bit of the current char and the first bit of the next char.
I am fully aware that this post perhaps should be located in the math/algo forum now. The question I now want to ask is this: is there any clever boolean algebra method for testing for this bit pattern in each possible location or do I have to manually test each of the possible locations?
If any mod reads this then perhaps this and the previous posts can be moved to the correct forum. Thank you.
|
|
|
|
|
I don't have the exact method but the strlen method in C in the assembly uses some really good math in a similar method.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
|
Hello...
I have 1 Form and 2 Panels in it. How to catch mousemove event from all of them ?
Now I'm using :
form.MouseMove += new MouseEventHandler(OnMouseMove);
panel1.MouseMove += new MouseEventHandler(OnMouseMove);
panel2.MouseMove += new MouseEventHandler(OnMouseMove);
If there are only 3 control, it's ok, but if I have 20 controls, it will be a little bit complex to write event for all of them.
Thx...
|
|
|
|
|
Use only one event for all the controls.
|
|
|
|
|
Hello experts,
I create an application using C#.net 2005, MS Access 2000. In that application I have one exe and 9-10 DLL's and some xml files are also included. I distribute this application using standard VS.NET setup and deployment project. Everything is working fine.
Now that was release 1.0. After using v1.0 my clients reports some bugs and I also add some more functionality into the application.
If any of my customer needs upgraded version of application then what should I do...? Because I have to change the EXE and DLL's including new and modified xml's. And their may be some database changes are also occurred...please help.
I don't want to use the ClickOnce deployment .. Please guide me......
Regards
Pankaj Joshi
If you want to shape your dreams into reality, please wake-up...
|
|
|
|
|
Pankaj - Joshi wrote: I don't want to use the ClickOnce deployment .. Please guide me......
First Wake Up, it's difficult to read while sleeping. There are all sorts of installer systems available including of course Windows Installer. I imagine they all support upgrading scenarios. You will just have to do some searching and some reading to figure out which one(s) you want to investigate.
|
|
|
|
|
That means you cant' help me...?
I search a lot but unable to get proper solution...
Please help....
Regards
Pankaj Joshi
If you want to shape your dreams into reality, please wake-up...
|
|
|
|
|
Pankaj - Joshi wrote: That means you cant' help me...?
You asked for guidance
Pankaj - Joshi wrote: Please guide me
I guided you
Pankaj - Joshi wrote: Please help
If you want more specific help you need to ask more specific questions. I have given you all the general guidance I can on the subject of updating applications. I can't alter the reality that it is a large subject and in many cases it is also somewhat complex. If you desire to be a competent software developer, at some point you have to stop looking for the magic bullet solution to problems and realize you just have to start researching a subject and stick with it until you have gained an understanding of the domain sufficient to provide yourself with a solution to your problem.
led mike
|
|
|
|
|
Hi,
I want to assign Styles to certain WPF UserControls at runtime. The first assignment always works, but assigning other Styles to the control never results in calling the PropertyChangeCallbacks. On the other hand OnStyleChange is called...?
The DP-definition of the UserControl looks like following:
public static readonly DependencyProperty FooterGradientBottomProperty =
DependencyProperty.Register ("FooterGradientBottom", typeof (Color),
typeof (subItem), new FrameworkPropertyMetadata (Color.FromArgb (255, 255, 255, 255),
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback (FooterGradientBottom_Changed)));
public Color FooterGradientBottom
{
get
{
return (Color) GetValue (FooterGradientBottomProperty);
}
set
{
SetValue (FooterGradientBottomProperty, value);
this.footerGradientBottom.Color = value;
}
}
private static void FooterGradientBottom_Changed (object sender,
DependencyPropertyChangedEventArgs args)
{
((subItem) sender).FooterGradientBottom = (Color) args.NewValue;
}
The Style-item, which shall be changed by the DPs was defined in XAML like this:
<code>
...
<GradientStop x:Name="footerGradientBottom" Offset="1"/>
...
</code>
Finally I assign Styles (from the static resource) to the UserControl in following way:
<code>
this.Style = (Style) this.FindResource ("HighlightedStyle");
</code>
As mentioned, the first Style is applied properly, all following assignments are without effect.
Has somebody an idea why?
Many thanks in advance,
Alex
modified on Tuesday, January 15, 2008 3:35:20 AM
|
|
|
|
|
I have created a windows application using C# and I am trying to send an email from this code. I have configured the SMTP server on the same machine on which the application resides through IIS, though the mail server is different (some different IP). The mail application runs but, no mail is sent and an error message is received.
Kindly Help
-----Have A Nice Day-----
|
|
|
|
|
assiduous wrote: no mail is sent and an error message is received.
What error message are you getting???
|
|
|
|
|
Sorry for the delay.
The error message I am getting is "Failure Sending Email..."
-----Have A Nice Day-----
|
|
|
|
|