Solved

Please check these two simple functions...

Posted on 2008-06-18
19
177 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now