• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Please check these two simple functions...

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;
}
/********************************************************************************/

Open in new window

0
Unimatrix_001
Asked:
Unimatrix_001
  • 8
  • 7
  • 3
3 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
Unimatrix_001Author Commented:
Hello! :) Yeah I could, although to be honest I'd rather not get too involved with iterators after recent experiences. ;)
0
 
Infinity08Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Unimatrix_001Author Commented:
>>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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
Arrhgggg... Uri... please give time for people to read and respond in full before you accept answers eh!?
0
 
Unimatrix_001Author Commented:
>>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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
Infinity08Commented:
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
 
Unimatrix_001Author Commented:
>>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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
Unimatrix_001Author Commented:
>>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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
Infinity08Commented:
>> 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
 
evilrixSenior Software Engineer (Avast)Commented:
>> Nice :) Some good company I hope :)
Maybe, I'll chat with you laters ;)
0
 
Unimatrix_001Author Commented:
Thanking you... :)
0
 
Unimatrix_001Author Commented:
Much better...
0
 
evilrixSenior Software Engineer (Avast)Commented:
Very kind Uri, thanks. Mucho apreciado ;)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now