DanielStutzbach / blist

A list-like type with better asymptotic performance and similar performance on small lists
Other
310 stars 36 forks source link

Case insensitive sortedset #50

Closed philfreo closed 11 years ago

philfreo commented 11 years ago

Would be great to see a case insensitive version of sortedset

Doing this now for sorting is easy because you have the key parameter (sortedlist(foo, key=lambda x: x.lower()))

However in those cases you also want to de-dupe in a case insensitive way, which it doesn't do.

If there's an easy way to accomplish this now, I'd love to hear it. The best I've come up with so far is:

class isortedset(sortedset):
    def __contains__(self, key):
        return key.lower() in (n.lower() for n in self)

but this breaks the nice time complexity

DanielStutzbach commented 11 years ago

One solution is to put the strings into a canonical (e.g., lowercase) form before inserting them into the set.

philfreo commented 11 years ago

@DanielStutzbach of course, but that's not always what you want. For example file systems often only allow 1 version (in a case insensitive comparison) but keep track of the original version of the case.

DanielStutzbach commented 11 years ago

In that case, you could use a dict that maps canonical values to original values.

Or you could use sortedlist(foo, key=str.lower), then something like:

def __contains__(self, key):
    if not self:
        return False
    return self[self.bisect_left(key)] == key.lower()