Solved

Please check these two simple functions...

Posted on 2008-06-18
19
187 Views
Last Modified: 2010-04-21
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
Comment
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
  • Learn & ask questions
  • 8
  • 7
  • 3
19 Comments
 
LVL 40

Assisted Solution

by:evilrix
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

by:Unimatrix_001
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

by:
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
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!

 
LVL 3

Author Comment

by:Unimatrix_001
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

by:evilrix
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

by:evilrix
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

by:Unimatrix_001
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

by:evilrix
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

by:Infinity08
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

by:Unimatrix_001
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

by:evilrix
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

by:Unimatrix_001
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

by:evilrix
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

by:Infinity08
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

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

Author Comment

by:Unimatrix_001
ID: 21813584
Thanking you... :)
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31468332
Much better...
0
 
LVL 40

Expert Comment

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

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question