473,395 Members | 2,467 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,395 software developers and data experts.

Is there a standard binary search with overridable comparisons?

I've got an ordered list of MyClasses that I want to be able to do
binary searches on, but against a tuple. MyClass has valid
__lt__(self, rhs) and __eq__(self, rhs) member functions that work
when rhs is a tuple.

This works:
l = [MyClass(..), MyClass(..), ...]
l.find((a,b))

But this doesn't:
bisect.bisect(l, (a,b))

I'm assuming this is because inside bisect, it does 'key < list[x]'
rather than 'list[x] < key', so it's the tuple's __lt__ that is
called, rather than MyClass's tuple.

Is there a way around this? Can I monkeypatch a new __lt__ into the
tuple class?

Here's some sample code that demonstrates the problem (it uses ints
rather than tuples, but the

import bisect
class MyC:
def __init__(self, v):
self.v = v

def __lt__(self, rhs):
return self.v < rhs

# cant search for int in a list of MyC's
l = sorted([MyC(x) for x in range(1000)])
bisect.bisect(l, 40)
1001 # AKA not found

# but, I can search for MyC in a list of ints
l = sorted(range(1000))
bisect.bisect(l, MyC(40))
41
Jun 27 '08 #1
1 929
On Jun 18, 6:55 am, markscottwright <markscottwri...@gmail.comwrote:
I've got an ordered list of MyClasses that I want to be able to do
binary searches on, but against a tuple. MyClass has valid
__lt__(self, rhs) and __eq__(self, rhs) member functions that work
when rhs is a tuple.

This works:
l = [MyClass(..), MyClass(..), ...]
l.find((a,b))

But this doesn't:
bisect.bisect(l, (a,b))

I'm assuming
.... Don't. It can be dangerous.
this is because inside bisect, it does 'key < list[x]'
rather than 'list[x] < key', so it's the tuple's __lt__ that is
called, rather than MyClass's tuple.
Actually it appears (extremely gory details in Objects/object.c) that
it tries all rich comparison possibilities first:
tuple < myclass: not defined in tuple type
myclass tuple: not defined in MyClass
before falling through to the default (which is based on addresses).
>
Is there a way around this? Can I monkeypatch a new __lt__ into the
tuple class?
Looks like you need to implement MyClass.__gt__

I suspect someone will say that this section of the manual is trying
to tell us this:
"""
There are no reflected (swapped-argument) versions of these methods
(to be used when the left argument does not support the operation but
the right argument does); rather, __lt__() and __gt__() are each
other's reflection, __le__() and __ge__() are each other's reflection,
and __eq__() and __ne__() are their own reflection.
"""
.... "trying" being the operative word :-)

Alternatively, do you really need rich comparison? Consider defining
__cmp__ instead of 2 to 6 of the __lt__ etc brigade.

HTH,
John
Jun 27 '08 #2

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

Similar topics

43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
22
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a...
6
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
0
by: j_depp_99 | last post by:
My program is required to build a binary search tree using integers inputted from a file. It also searches for items and counts the nodes accessed in each search. Also it needs to calculate the...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.