Solved

# Please check these two simple functions...

Posted on 2008-06-18
186 Views
I've given them a good test, but please make sure I haven't missed anything.

Thank you,
Uni
``````/*********************************************************************************
This returns true if the needle was found in the haystack and the index is given
in the aFoundIndex parameter. If the needle wasn't found then the method returns
false and aFoundIndex is undefined. This method is bound safe.
*********************************************************************************/
bool findNeedleInHaystack(vector<BIT> &aHaystack, unsigned int aStartIndex, vector<BIT> &aNeedle, unsigned int &aFoundIndex){
for(unsigned int haystackIndex=aStartIndex;haystackIndex<aHaystack.size();haystackIndex++){
if(haystackIndex+aNeedle.size()>aHaystack.size())
return false;
if(memcmp(&aHaystack[haystackIndex], &aNeedle[0], aNeedle.size())==0){
aFoundIndex=haystackIndex;
return true;
}
}
return false;
}
/********************************************************************************/

/*********************************************************************************
This returns true if the needle was found in the haystack and the index is given
in the aFoundIndex parameter. If the needle wasn't found then the method returns
false and aFoundIndex is undefined. This method is bound safe.
*********************************************************************************/
bool findNeedleInHaystackReverse(vector<BIT> &aHaystack, unsigned int aStartIndex, vector<BIT> &aNeedle, unsigned int &aFoundIndex){
for(int haystackIndex=aStartIndex;haystackIndex>=0;haystackIndex--){
if(haystackIndex>=aHaystack.size())
return false;
if(haystackIndex+aNeedle.size()>aHaystack.size())
continue;
if(memcmp(&aHaystack[haystackIndex], &aNeedle[0], aNeedle.size())==0){
aFoundIndex=haystackIndex;
return true;
}
}
return false;
}
/********************************************************************************/
``````
0
Question by:Unimatrix_001
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 7
• 3

LVL 40

Assisted Solution

evilrix earned 250 total points
ID: 21812844
Hi Uri,

Can't you just use std::search to do this? You can do a reserve search using reverse iterators.

http://www.sgi.com/tech/stl/search.html
0

LVL 3

Author Comment

ID: 21812868
Hello! :) Yeah I could, although to be honest I'd rather not get too involved with iterators after recent experiences. ;)
0

LVL 53

Accepted Solution

Infinity08 earned 250 total points
ID: 21812881
They would work as expected, assuming that sizeof(BIT) == 1.

You can avoid the first if in findNeedleInHaystack by adjusting the upper limit for the for loop though.

Why did you decide to not use the STL search algorithm ?
0

LVL 3

Author Comment

ID: 21812916
>>They would work as expected, assuming that sizeof(BIT) == 1.
Yes it is.

>>You can avoid the first if in findNeedleInHaystack by adjusting the upper limit for the for loop though.
I'll keep that in there just for the clarity of it. I'm not wanting speed at the moment, just wanting to make sure everything is nice and tidy... :)

>>Why did you decide to not use the STL search algorithm ?
I'm keeping my distance from iterators atm! ;)

Thanks,
Uni
0

LVL 40

Assisted Solution

evilrix earned 250 total points
ID: 21812926
Iterators are the way to go... all the STL relies on them... you'll be rewarded by getting to know them.

>> for(int haystackIndex=aStartIndex;haystackIndex>=0;haystackIndex--){
Your mix of signed and unsigned types is something to be concerned about. I think, you've probably protected against any problems here but at the cost of only allowing 1/2 the addressable range to be accessed.

Otherwise, just from reading them I can't see anything obviously wrong.
0

LVL 40

Expert Comment

ID: 21812930
Arrhgggg... Uri... please give time for people to read and respond in full before you accept answers eh!?
0

LVL 3

Author Comment

ID: 21812958
>>Iterators are the way to go... all the STL relies on them... you'll be rewarded by getting to know them.
As I said in my previous question I'd like to use them solidly, but that problem that I had has shaken my confidence in them (or rather VS2K8 + iterators)....

>>Your mix of signed and unsigned types is something to be concerned about. I think, you've probably protected against any problems here but at the cost of only allowing 1/2 the addressable range to be accessed.
Cheers for the heads up, I'm only working with a few thousand elements though, nowhere near enough for 2^31 ;)
0

LVL 40

Expert Comment

ID: 21812962
>> please give time for people to read and respond in full before you accept answers eh!?
If you want code peered properly at least give enough time for it to be reviewed and commented on.. the first comment you get isn't necessarily correct or covering all bases! Also, I think my first comment was valid!
0

LVL 53

Expert Comment

ID: 21812978
Sorry evilrix - that wasn't my intention ... I'm working with a Java process in the background that takes up 99% of CPU, so I type a letter, and it takes 2 seconds to show up, let alone that clicking the send button takes ages to do its job lol. Oh, how I love Java.
0

LVL 3

Author Comment

ID: 21812980
>>Arrhgggg... Uri... please give time for people to read and respond in full before you accept answers eh!?
My apologies, sometimes I am a bit too quick to hit accept.... I'll get it reopened and split the points... ;)

Sorry again!
Uni.
0

LVL 40

Expert Comment

ID: 21813035
>> I'll get it reopened and split the points... ;)
You don't have too... I'm just pointing out that if you want good feedback you need to give enough time for people to read the code thoroughly and respond. By closing it as soon as you get a comment you put off other experts who might find something valid to comment on.

Also, a Q like this has no right or wrong answer so, IMHO, all constructive/useful/helpful feedback that is useful should be considered -- although that doesn't mean all comments should of course. In this case I think my first comment was valid and so was my second, even though you have your own reasons for not going that route it might be you just didn't know about std::search or problems with mxing signed/unsigned so the comments were valid and, potentially, useful for someone else reason this thread in the future.

Just my opinion and something to consider.

Cheers.
0

LVL 3

Author Comment

ID: 21813112
>>You don't have too...
No no, fair's fair - you've got a point... (no pun intended...) ;)

>>I'm just pointing out that if you want good feedback you need to give enough time for people to read the code thoroughly and respond. By closing it as soon as you get a comment you put off other experts who might find something valid to comment on.
Yes, you're correct, as I said one of my failings it pressing the accept button a bit too quickly...

Again, you're correct, this question isn't really a right or wrong... I should take into account any suggestions... :)

Infinity, I hope you don't mind if I split the points between you and evilrix, his points are valid and I think it would be unfair for me not to reward him...

Thanks both,
Uni
0

LVL 40

Expert Comment

ID: 21813144
Many thanks Uri, but like I said there is no need to reopen -- unless you truely want to -- I am happy for I8 to get the points, he helped me out with a personal issue last night, we'll call it payback :)

BTW I8, I passed and got an interview ;)
0

LVL 53

Expert Comment

ID: 21813205
>> Infinity, I hope you don't mind if I split the points between you and evilrix, his points are valid and I think it would be unfair for me not to reward him...

I don't mind at all !

>> BTW I8, I passed and got an interview ;)

Nice :) Some good company I hope :)
0

LVL 40

Expert Comment

ID: 21813281
>> Nice :) Some good company I hope :)
Maybe, I'll chat with you laters ;)
0

LVL 3

Author Comment

ID: 21813584
Thanking you... :)
0

LVL 3

Author Closing Comment

ID: 31468332
Much better...
0

LVL 40

Expert Comment

ID: 21813712
Very kind Uri, thanks. Mucho apreciado ;)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there isâ€¦
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. Aâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month7 days, 5 hours left to enroll