Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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

SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Unimatrix_001

ASKER

Hello! :) Yeah I could, although to be honest I'd rather not get too involved with iterators after recent experiences. ;)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Arrhgggg... Uri... please give time for people to read and respond in full before you accept answers eh!?
>>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 ;)
>> 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!
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.
>>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.
>> 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.
>>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
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 ;)
>> 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 :)
>> Nice :) Some good company I hope :)
Maybe, I'll chat with you laters ;)
Thanking you... :)
Much better...
Very kind Uri, thanks. Mucho apreciado ;)