473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to write a lower_bound_if function modification to STL

Do any gurus out there know what the source code for the STL binary
search algorithm "lower_bound" would look like? I want to make a slight
modification called "lower_bound_if" which would take a predicate as the
third argument similar to "find_if" and "copy_if". Is there some reasom
why the STL does not provide such an algorithm already?

template<class For, class Pred>
For lower_bound_if(For first, For last, Pred p);

Thanks in advance for any help.

-DCB

Jul 22 '05 #1
5 1880
"Dorwin C. Black" <dc*****@ccs.nrl.navy.mil> asked:
Do any gurus out there know what the source code for the STL binary
search algorithm "lower_bound" would look like?


Being a template, the source code should be visible in the <algorithm>
header file ...

David F
Jul 22 '05 #2
Dorwin C. Black wrote:
Do any gurus out there know what the source code for the STL binary
search algorithm "lower_bound" would look like? I want to make a slight
modification called "lower_bound_if" which would take a predicate as the
third argument similar to "find_if" and "copy_if". Is there some reasom
why the STL does not provide such an algorithm already?

template<class For, class Pred>
For lower_bound_if(For first, For last, Pred p);

Thanks in advance for any help.

-DCB

What would the predicate do? The lower_bound algorithm compares two
elements at a time, so it needs a comparison function, not a
find_if-style unary predicate. Here's the implementation that came with
g++:

/**
* @brief Finds the first position in which @a val could be inserted
* without changing the ordering.
* @param first An iterator.
* @param last Another iterator.
* @param val The search term.
* @return An iterator pointing to the first element "not less than"
* @a val.
* @ingroup binarysearch
*/
template<typename _ForwardIter, typename _Tp>
_ForwardIter
lower_bound(_ForwardIter __first, _ForwardIter __last, const _Tp& __val)
{
typedef typename iterator_traits<_ForwardIter>::value_type _ValueType;
typedef typename
iterator_traits<_ForwardIter>::difference_type _DistanceType;

// concept requirements
// Note that these are slightly stricter than those of the 4-argument
// version, defined next. The difference is in the strictness of the
// comparison operations... so for looser checking, define your own
// comparison function, as was intended.

__glibcpp_function_requires(_ForwardIteratorConcep t<_ForwardIter>)
__glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
__glibcpp_function_requires(_LessThanComparableCon cept<_Tp>)
_DistanceType __len = distance(__first, __last);
_DistanceType __half;
_ForwardIter __middle;
while (__len > 0) {
__half = __len >> 1;
__middle = __first;
advance(__middle, __half);
if (*__middle < __val) {
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else
__len = __half;
}
return __first;
}

Jul 22 '05 #3
In article <o4********************@comcast.com>, Jeff Schwab wrote:
What would the predicate do? The lower_bound algorithm compares two
elements at a time, so it needs a comparison function, not a
find_if-style unary predicate. Here's the implementation that came with
g++:


About their conventions for _naming __identifiers: I've always wondered why
they do this. Is it to avoid possible collision with user macro definitions ?

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #4
On Fri, 09 Jan 2004 03:02:42 +0000, Donovan Rebbechi wrote:
In article <o4********************@comcast.com>, Jeff Schwab wrote:
What would the predicate do? The lower_bound algorithm compares two
elements at a time, so it needs a comparison function, not a
find_if-style unary predicate. Here's the implementation that came with
g++:


About their conventions for _naming __identifiers: I've always wondered why
they do this. Is it to avoid possible collision with user macro definitions ?


Yes, and for the globally visible symbols to avoid collision with user
defined symbols. These identifiers are reserved for implementors, so a
user is not allowed to use them.

HTH,
M4

Jul 22 '05 #5


Jeff Schwab wrote:
What would the predicate do? The lower_bound algorithm compares two
elements at a time, so it needs a comparison function, not a
find_if-style unary predicate.
You are correct. I will need another argument in my algorithm. What I am
trying to do is find a way to perform a binary search on a collection of
objects that are sorted based on a member variable. For example, say I have a
class called CBaseballPlayer with member variable to represent their age
(m_usAge - unsigned short), and I set up a collection of pointers to CBaseball
player objects sorted by age. Now I want to find the first player in the list
whose age is equal to some value. I could use find_if( ), but if I have a
very large list, that may be slow. So what I want is an algorithm that can do
a binary search like the following:

template<class For, class T, class Op>
For lower_bound_using(For first, For last, const T& value, Op op)

The operator will be a functional object ( say for the above example the
operator takes a pointer to CBaseballPlayer and returns m_usAge )
Here's the implementation that came with
g++:


Thanks, I think this will help me figure out how to do what I want to do.

-DCB

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: NSC | last post by:
Hi all, I have this : <SNIP> foreach $item (@NEW_FILES) print "\n $item \n ";
2
by: Steve D | last post by:
I've looked all over but can't find a solid answer. I've got a function that runs from a View and when the function runs the first time it is calculating a Temperature for a group of Formulas. ...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
3
by: stan | last post by:
I am working on some documentation in html format and I would really like to display the date the html file, itself was modified. I am writing my documentation in vi and the html server involved is...
10
by: malv | last post by:
I am involved in a major scientific algorithm design problem in which simulation of the underlying physics and visualization play an important role. Algorithm adaptation from run to run often...
3
by: sparks | last post by:
Have to read in a bunch of files into access and wanted to ask is there a way to read the file modification date before reading them in so I don't read files that I have already read? man thats...
4
by: Stephan Tobies | last post by:
Hi everyone, I am looking for a good data structure that could be used to represent families of trees with shared sub-trees and copy-on-write semantics. On a very abstract level, I would like...
10
by: sandorf | last post by:
I'm using the Windows version of Python and IDLE. When I debug my .py file, my modification to the .py file does not seem to take effect unless I restart IDLE. Saving the file and re-importing it...
0
by: Marco Segurini | last post by:
HI, my form contains a combobox and a propertygrid control. At each string of the combobox is associated an object. When I select a string of the combobox the associated object is selected in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.