list.index on a class with __cmp__ fails #306

Open
pyjsorg opened this Issue Apr 27, 2012 · 1 comment

Comments

Projects
None yet
1 participant
Contributor

pyjsorg commented Apr 27, 2012

see Libtest ListTest.py testIndexClass

Original issue: http://code.google.com/p/pyjamas/issues/detail?id=413 (June 08, 2010 11:44:53)

Contributor

pyjsorg commented Apr 27, 2012

From thomas.k...@gmail.com on September 01, 2010 17:13:08:
.indexOf is not aware of cmp

def index(self, value, _start=0):
JS("""
var start = @{{_start}}.valueOf();
{
var len = @{{self}}.__array.length >>> 0;

        start = (start < 0)
                ? Math.ceil(start)
                : Math.floor(start);
        if (start < 0)
            start += len;

        for (; start < len; start++) {
            if ( @{{cmp}}(@{{self}}.__array[start], @{{value}}) == 0)
                return start;
        }
    }
    """)
    raise ValueError("list.index(x): x not in list")

would do the job right.
I do not know, weather this will be much slower then

def index(self, value, _start=0):
JS("""
var start = @{{_start}}.valueOf();
if (typeof @{{value}} == 'number' || typeof @{{value}} == 'string' ) {
start = @{{self}}.__array.indexOf(@{{value}}, start);
if (start >= 0)
return start;
} else {
var len = @{{self}}.__array.length >>> 0;

        start = (start < 0)
                ? Math.ceil(start)
                : Math.floor(start);
        if (start < 0)
            start += len;

        for (; start < len; start++) {
            if ( start in @{{self}}.__array && 
                @{{cmp}}(@{{self}}.__array[start], @{{value}}) == 0)
                return start;
        }
    }
    """)
    raise ValueError("list.index(x): x not in list")

Maybee it would be good to have a slow and save index and expose indexOf to python, suggesting to use it, if one is sure, that objects do not have a compare function defined ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment