Example #1
0
def new_sort_class(elem=NONE, sort_type=0, has_cmp_func=False, reverse=False):
    TimSort = make_timsort_class()
    key_func = _get_key_func(sort_type)
    cmp_func = _get_cmp_func(sort_type)
    if has_cmp_func:
        SortMixin = CustomSortMixin
    else:

        class SortMixin(object):
            _mixin_ = True

            def cmp(self, a, b):
                return cmp_func(self.space, a, b)

    class Sort(TimSort, SortMixin):
        def __init__(self,
                     list,
                     listlength=None,
                     cmp_func=None,
                     space=None,
                     reverse=reverse):
            TimSort.__init__(self, list, listlength)
            self.cmp_func = cmp_func
            self.space = space
            self.reverse = reverse

        def get_cmp_elem(self, a, b):
            if elem == KEY:
                a, _ = a
                b, _ = b
            elif elem == VALUE:
                _, a = a
                _, b = b
            return key_func(self.space, a), key_func(self.space, b)

        def lt(self, a, b):
            a, b = self.get_cmp_elem(a, b)
            res = self.cmp(a, b)
            if self.reverse:
                return res > 0
            return res < 0

    if elem == NONE:
        Sort.__name__ = 'Sort_NONE'
    elif elem == KEY:
        Sort.__name__ = 'Sort_KEY'
    else:
        Sort.__name__ = 'Sort_VALUE'
    return Sort
Example #2
0
def new_sort_class(elem=NONE, sort_type=0, has_cmp_func=False, reverse=False):
    TimSort = make_timsort_class()
    key_func = _get_key_func(sort_type)
    cmp_func = _get_cmp_func(sort_type)
    if has_cmp_func:
        SortMixin = CustomSortMixin
    else:
        class SortMixin(object):
            _mixin_ = True

            def cmp(self, a, b):
                return cmp_func(self.space, a, b)

    class Sort(TimSort, SortMixin):
        def __init__(self, list, listlength=None, cmp_func=None,
                     space=None, reverse=reverse):
            TimSort.__init__(self, list, listlength)
            self.cmp_func = cmp_func
            self.space = space
            self.reverse = reverse

        def get_cmp_elem(self, a, b):
            if elem == KEY:
                a, _ = a
                b, _ = b
            elif elem == VALUE:
                _, a = a
                _, b = b
            return key_func(self.space, a), key_func(self.space, b)

        def lt(self, a, b):
            a, b = self.get_cmp_elem(a, b)
            res = self.cmp(a, b)
            if self.reverse:
                return res > 0
            return res < 0

    if elem == NONE:
        Sort.__name__ = 'Sort_NONE'
    elif elem == KEY:
        Sort.__name__ = 'Sort_KEY'
    else:
        Sort.__name__ = 'Sort_VALUE'
    return Sort
Example #3
0
import copy

from rpython.rlib.listsort import make_timsort_class

from topaz.coerce import Coerce
from topaz.module import ClassDef, check_frozen
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.utils.packing.pack import RPacker


BaseRubySorter = make_timsort_class()
BaseRubySortBy = make_timsort_class()


class RubySorter(BaseRubySorter):
    def __init__(self, space, list, listlength=None, sortblock=None):
        BaseRubySorter.__init__(self, list, listlength=listlength)
        self.space = space
        self.sortblock = sortblock

    def lt(self, w_a, w_b):
        w_cmp_res = self.space.compare(w_a, w_b, self.sortblock)
        return self.space.int_w(w_cmp_res) < 0


class RubySortBy(BaseRubySortBy):
    def __init__(self, space, list, listlength=None, sortblock=None):
        BaseRubySortBy.__init__(self, list, listlength=listlength)
        self.space = space
        self.sortblock = sortblock
Example #4
0
def make_sort_function(space, itemtype, comp_type, count=1):
    TP = itemtype.T
    step = rffi.sizeof(TP)

    class Repr(object):
        def __init__(self, stride_size, size, values, start):
            self.stride_size = stride_size
            self.start = start
            self.size = size
            self.values = values

        def getitem(self, item):
            if count < 2:
                v = raw_storage_getitem(TP, self.values, item * self.stride_size
                                    + self.start)
            else:
                v = []
                for i in range(count):
                    _v = raw_storage_getitem(TP, self.values, item * self.stride_size
                                    + self.start + step * i)
                    v.append(_v)
            if comp_type == 'int':
                v = widen(v)
            elif comp_type == 'float':
                v = float(v)
            elif comp_type == 'complex':
                v = [float(v[0]),float(v[1])]
            else:
                raise NotImplementedError('cannot reach')
            return (v)

        def setitem(self, idx, item):
            if count < 2:
                raw_storage_setitem(self.values, idx * self.stride_size +
                                self.start, rffi.cast(TP, item))
            else:
                i = 0
                for val in item:
                    raw_storage_setitem(self.values, idx * self.stride_size +
                                self.start + i*step, rffi.cast(TP, val))
                    i += 1

    class ArgArrayRepWithStorage(Repr):
        def __init__(self, stride_size, size):
            start = 0
            values = alloc_raw_storage(size * stride_size,
                                            track_allocation=False)
            Repr.__init__(self, stride_size,
                          size, values, start)

        def __del__(self):
            free_raw_storage(self.values, track_allocation=False)

    def arg_getitem(lst, item):
        return lst.getitem(item)

    def arg_setitem(lst, item, value):
        lst.setitem(item, value)

    def arg_length(lst):
        return lst.size

    def arg_getitem_slice(lst, start, stop):
        retval = ArgArrayRepWithStorage(lst.stride_size, stop-start)
        for i in range(stop-start):
            retval.setitem(i, lst.getitem(i+start))
        return retval

    if count < 2:
        def arg_lt(a, b):
            # handles NAN and INF
            return a < b or b != b and a == a
    else:
        def arg_lt(a, b):
            for i in range(count):
                if b[i] != b[i] and a[i] == a[i]:
                    return True
                elif b[i] == b[i] and a[i] != a[i]:
                    return False
            for i in range(count):
                if a[i] < b[i]:
                    return True
                elif a[i] > b[i]:
                    return False
            # Does numpy do True?
            return False

    ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
                                 arg_getitem_slice, arg_lt)

    def sort(arr, space, w_axis):
        if w_axis is space.w_None:
            # note that it's fine to pass None here as we're not going
            # to pass the result around (None is the link to base in slices)
            arr = arr.reshape(None, [arr.get_size()])
            axis = 0
        elif w_axis is None:
            axis = -1
        else:
            axis = space.int_w(w_axis)
        with arr as storage:
            if len(arr.get_shape()) == 1:
                r = Repr(arr.strides[0], arr.get_size(), storage,
                         arr.start)
                ArgSort(r).sort()
            else:
                shape = arr.get_shape()
                if axis < 0:
                    axis = len(shape) + axis
                if axis < 0 or axis >= len(shape):
                    raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
                arr_iter = AllButAxisIter(arr, axis)
                arr_state = arr_iter.reset()
                stride_size = arr.strides[axis]
                axis_size = arr.shape[axis]
                while not arr_iter.done(arr_state):
                    r = Repr(stride_size, axis_size, storage, arr_state.offset)
                    ArgSort(r).sort()
                    arr_state = arr_iter.next(arr_state)

    return sort
Example #5
0
File: ast.py Project: dirk/stalk
            if type(ret) == SL_Method:
                scope.define_method(ret)
        return ret
    def eval(self, scope):
        # TODO: Fix all these co-dependent import hacks.
        try:
            return self.eval_shallow(scope)
        except ReturnException as re:
            return re.get_return()
        

from rpython.rlib.listsort import make_timsort_class
def _sig_comp(a, b):
    # return len(a) > len(b) # Descending order by length
    return len(a) < len(b) # Ascending order by length
SigSort = make_timsort_class(lt = _sig_comp)

class S_Statement(S_List):
    def __init__(self, exprs):
        self.expressions = exprs
    def get_expressions(self):
        return self.expressions
    def __repr__(self):
        return "st("+(",".join([e.__repr__() for e in self.expressions]))+")"
    def set_target(self):
        from stalk.object import SL_String, compile_signature
        scope = self.e_scope
        expr = self.e_expr
        # Leading keyword without target then it's a send
        if type(expr) == S_Keyword:
            self.do_send()
Example #6
0
def make_sort_function(space, itemtype, comp_type, count=1):
    TP = itemtype.T
    step = rffi.sizeof(TP)

    class Repr(object):
        def __init__(self, stride_size, size, values, start):
            self.stride_size = stride_size
            self.start = start
            self.size = size
            self.values = values

        def getitem(self, item):
            if count < 2:
                v = raw_storage_getitem(TP, self.values,
                                        item * self.stride_size + self.start)
            else:
                v = []
                for i in range(count):
                    _v = raw_storage_getitem(
                        TP, self.values,
                        item * self.stride_size + self.start + step * i)
                    v.append(_v)
            if comp_type == 'int':
                v = widen(v)
            elif comp_type == 'float':
                v = float(v)
            elif comp_type == 'complex':
                v = [float(v[0]), float(v[1])]
            else:
                raise NotImplementedError('cannot reach')
            return (v)

        def setitem(self, idx, item):
            if count < 2:
                raw_storage_setitem(self.values,
                                    idx * self.stride_size + self.start,
                                    rffi.cast(TP, item))
            else:
                i = 0
                for val in item:
                    raw_storage_setitem(
                        self.values,
                        idx * self.stride_size + self.start + i * step,
                        rffi.cast(TP, val))
                    i += 1

    class ArgArrayRepWithStorage(Repr):
        def __init__(self, stride_size, size):
            start = 0
            values = alloc_raw_storage(size * stride_size,
                                       track_allocation=False)
            Repr.__init__(self, stride_size, size, values, start)

        def __del__(self):
            free_raw_storage(self.values, track_allocation=False)

    def arg_getitem(lst, item):
        return lst.getitem(item)

    def arg_setitem(lst, item, value):
        lst.setitem(item, value)

    def arg_length(lst):
        return lst.size

    def arg_getitem_slice(lst, start, stop):
        retval = ArgArrayRepWithStorage(lst.stride_size, stop - start)
        for i in range(stop - start):
            retval.setitem(i, lst.getitem(i + start))
        return retval

    if count < 2:

        def arg_lt(a, b):
            # handles NAN and INF
            return a < b or b != b and a == a
    else:

        def arg_lt(a, b):
            for i in range(count):
                if b[i] != b[i] and a[i] == a[i]:
                    return True
                elif b[i] == b[i] and a[i] != a[i]:
                    return False
            for i in range(count):
                if a[i] < b[i]:
                    return True
                elif a[i] > b[i]:
                    return False
            # Does numpy do True?
            return False

    ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
                                 arg_getitem_slice, arg_lt)

    def sort(arr, space, w_axis):
        if w_axis is space.w_None:
            # note that it's fine to pass None here as we're not going
            # to pass the result around (None is the link to base in slices)
            arr = arr.reshape(None, [arr.get_size()])
            axis = 0
        elif w_axis is None:
            axis = -1
        else:
            axis = space.int_w(w_axis)
        with arr as storage:
            if len(arr.get_shape()) == 1:
                r = Repr(arr.strides[0], arr.get_size(), storage, arr.start)
                ArgSort(r).sort()
            else:
                shape = arr.get_shape()
                if axis < 0:
                    axis = len(shape) + axis
                if axis < 0 or axis >= len(shape):
                    raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
                arr_iter = AllButAxisIter(arr, axis)
                arr_state = arr_iter.reset()
                stride_size = arr.strides[axis]
                axis_size = arr.shape[axis]
                while not arr_iter.done(arr_state):
                    r = Repr(stride_size, axis_size, storage, arr_state.offset)
                    ArgSort(r).sort()
                    arr_state = arr_iter.next(arr_state)

    return sort
Example #7
0
# initialize it with empty list and store suffix n. If p is present, simply add n to the list.
# If such decomposition is impossible (i.e. identifier doesn't end with number), then v is added to the mapping with -1. -1 is used to represent occurence of variable v s.t. v == p.

# Given prefix p, attempt to find pprime in the mapping such that p == pprime. Retrieve appropriate list of suffixes interpreted as integers.
# If list is empty, then p is fresh and simply return p.
# Otherwise, sort the list in the ascending order. If first element in the list is not -1, this means that prefix # p is itself a fresh variable and return p.
# Otherwise , our goal is to find the smallest number i>0 that is not in the sorted list. Starting from the second element j of the list (since the first one must be -1) and initializing suffix s to 1, there are several cases to consider.

# s < suffixes[j]: return s
# s > suffixes[j]: increment j. This only happens when 0 is in the list.
# s == suffixes[j]: increment j and s by 1.
# Iterate until the end of the list.
# Return string prefix + s.

from rpython.rlib import listsort
IntSorter = listsort.make_timsort_class()


def decompose_variable(var):
    i = len(var) - 1
    if not (ord(var[i]) >= 48 and ord(var[i]) <= 57):
        return False, None, None
    while ord(var[i]) >= 48 and ord(var[i]) <= 57:
        i -= 1
    i = i + 1
    assert i >= 0
    return True, var[0:i], var[i:len(var)]


def variable_not_in(term, variable):
    if not isinstance(variable, Variable):
Example #8
0
    for op, (start, stop) in live_ranges.iteritems():
        starts.append((start, stop, op))
    sort_starts(starts).sort()
    for current, stop, op in starts:
        assert current <= stop
        if len(avail) > 0:
            op.index = avail.pop()
        else:
            op.index = body.tmpc
            body.tmpc += 1
        stops.append((stop, op))
        sort_ends(stops).sort()
        while len(stops) > 0 and stops[0][0] < current:
            _, exp = stops.pop(0)
            assert exp.index not in avail
            avail.append(exp.index)


def plot_range(ranges, key, pos):
    if key not in ranges:
        ranges[key] = (pos, pos)
    else:
        start, stop = ranges[key]
        ranges[key] = (min(start, pos), max(stop, pos))


# The previous version of this algorithm was run by RPython,
# which depended on this kind of constructs to sort.
sort_starts = make_timsort_class(lt=lambda x, y: x[0] < y[0])
sort_ends = make_timsort_class(lt=lambda x, y: x[0] < y[0])
Example #9
0
    def setup_threads(self, space):
        # for test_ztranslation
        pass

@not_rpython
def make_weak_value_dictionary(space, keytype, valuetype):
    if space.config.translation.rweakref:
        from rpython.rlib.rweakref import RWeakValueDictionary
        return RWeakValueDictionary(keytype, valuetype)
    else:
        class FakeWeakValueDict(object):
            def __init__(self):
                self._dict = {}
            def get(self, key):
                return self._dict.get(key, None)
            def set(self, key, value):
                self._dict[key] = value
        return FakeWeakValueDict()


_StringBaseTimSort = make_timsort_class()

class StringSort(_StringBaseTimSort):
    def lt(self, a, b):
        return a < b

def string_sort(lst):
    """Sort a (resizable) list of strings."""
    sorter = StringSort(lst, len(lst))
    sorter.sort()
Example #10
0
        else:
            builder.append(char)
            builder.append(':')
            builder.append(str(m))
            builder.append(';')
            return True

    def add_counter(self):
        self.counter += 1


class SerializerError(Exception):
    pass


IntSort = make_timsort_class()


def remove_duplicates_fin(lst):
    prev = lst[0]
    count = 1
    for i in range(1, len(lst)):
        if prev != lst[i]:
            prev = lst[i]
            count += 1
    result = [0] * (count + 1)
    prev = lst[0]
    result[0] = prev
    count = 1
    for i in range(1, len(lst)):
        if prev != lst[i]:
Example #11
0
# -*- coding: utf-8 -*-
# module-description: 
# animation calculations
from ast_nodes import *
from bexceptions import ValueNotInDomainException
from config import MAX_OP_SOLUTIONS, MAX_SELECT_BRANCHES, PRINT_WARNINGS, USE_RPYTHON_CODE, BMACHINE_SEARCH_DIR, BFILE_EXTENSION, USE_ANIMATION_HISTORY
from constraintsolver import compute_constrained_domains
from enumeration import try_all_values, gen_all_values


if USE_RPYTHON_CODE:
    from rpython_interp import exec_substitution, interpret 
    from rpython_b_objmodel import frozenset
    from rpython.rlib import jit, listsort
    Sorter = listsort.make_timsort_class(lt = lambda a, b: a.opName < b.opName)

else:
    from interp import exec_substitution, interpret
    import mockjit as jit 
      


# Wrapper object. Replaces list of dif. typed items.
# only returned by calc_next_states
class Executed_Operation():
    def __init__(self, opName, parameter_names, parameter_values, return_names, return_values, bstate):
        self.opName = opName
        self.parameter_names  = parameter_names
        self.parameter_values = parameter_values
        self.return_names  = return_names
        self.return_values = return_values 
Example #12
0
from typhon.objects.collections.lists import unwrapList, wrapList
from typhon.objects.collections.maps import EMPTY_MAP, monteMap
from typhon.objects.data import (BigInt, CharObject, DoubleObject, IntObject,
                                 StrObject, promoteToBigInt, unwrapStr)
from typhon.objects.ejectors import throwStr
from typhon.objects.root import Object, audited
from typhon.pretty import Buffer, LineWriter
from typhon.profile import profileTyphon
from typhon.quoting import quoteChar, quoteStr


def lt0(a, b):
    return a[0] < b[0]


TimSort0 = make_timsort_class(lt=lt0)


class InvalidAST(LoadFailed):
    """
    An AST was ill-formed.
    """


@autohelp
@audited.DF
class KernelAstStamp(Object):
    @method("Bool", "Any")
    def audit(self, audition):
        return True
Example #13
0
    catch _:
        return right.op__cmp(left).aboveZero()
    """

    try:
        comparison = left.call(u"op__cmp", [right])
        b = comparison.call(u"belowZero", [])
        return unwrapBool(b)
    except UserException:
        comparison = right.call(u"op__cmp", [left])
        b = comparison.call(u"aboveZero", [])
        return unwrapBool(b)

def monteLTKey(left, right):
    """
    return left[0] < right[0]
    """

    return monteLessThan(left[0], right[0])

def monteLTValue(left, right):
    """
    return left[1] < right[1]
    """

    return monteLessThan(left[1], right[1])

MonteSorter = make_timsort_class(lt=monteLessThan)
KeySorter = make_timsort_class(lt=monteLTKey)
ValueSorter = make_timsort_class(lt=monteLTValue)
Example #14
0
    _immutable_fields_ = ['iterator']
    def __init__(self, iterator):
        self.iterator = iterator

    def iter(self):
        return self

@ListIterator.builtin_method
@signature(ListIterator)
def next(self):
    return self.iterator.next()

@List.instantiator
def instantiate(argv):
    list_ = List([])
    if len(argv) > 0:
        other = argv[0]
        it = other.iter()
        try:
            while True:
                list_.contents.append(it.callattr(u"next", []))
        except StopIteration as stop:
            pass
    return list_

TimSort = make_timsort_class()

class ListSort(TimSort):
    def lt(self, a, b):
        return space.is_true(self.w_lt.call([a, b]))
Example #15
0
import copy

from rpython.rlib.listsort import make_timsort_class

from topaz.coerce import Coerce
from topaz.module import ClassDef, check_frozen
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.utils.packing.pack import RPacker

BaseRubySorter = make_timsort_class()
BaseRubySortBy = make_timsort_class()


class RubySorter(BaseRubySorter):
    def __init__(self, space, list, listlength=None, sortblock=None):
        BaseRubySorter.__init__(self, list, listlength=listlength)
        self.space = space
        self.sortblock = sortblock

    def lt(self, w_a, w_b):
        w_cmp_res = self.space.compare(w_a, w_b, self.sortblock)
        return self.space.int_w(w_cmp_res) < 0


class RubySortBy(BaseRubySortBy):
    def __init__(self, space, list, listlength=None, sortblock=None):
        BaseRubySortBy.__init__(self, list, listlength=listlength)
        self.space = space
        self.sortblock = sortblock
Example #16
0
    """ agree on dtype from a list of arrays. if out is allocated,
    use it's dtype, otherwise allocate a new one with agreed dtype
    """
    from pypy.module.micronumpy.ufuncs import find_binop_result_dtype

    if not space.is_none(out):
        return out
    dtype = None
    for w_arr in w_arr_list:
        if not space.is_none(w_arr):
            dtype = find_binop_result_dtype(space, dtype, w_arr.get_dtype())
    assert dtype is not None
    out = base.W_NDimArray.from_shape(space, shape, dtype)
    return out

DescrFieldSort = make_timsort_class(lt=lambda a, b: a[1][0] < b[1][0])


class W_Dtype(W_Root):
    _immutable_fields_ = [
        "itemtype?", "num", "kind", "char", "w_box_type",
        "byteorder?", "names?", "fields?", "elsize?", "alignment?",
        "shape?", "subdtype?", "base?",
    ]

    def __init__(self, itemtype, num, kind, char, w_box_type,
                 byteorder=None, names=[], fields={},
                 elsize=None, shape=[], subdtype=None):
        self.itemtype = itemtype
        self.num = num
        self.kind = kind
Example #17
0
    catch _:
        return right.op__cmp(left).aboveZero()
    """

    try:
        comparison = left.call(u"op__cmp", [right])
        b = comparison.call(u"belowZero", [])
        return unwrapBool(b)
    except UserException:
        comparison = right.call(u"op__cmp", [left])
        b = comparison.call(u"aboveZero", [])
        return unwrapBool(b)

def monteLTKey(left, right):
    """
    return left[0] < right[0]
    """

    return monteLessThan(left[0], right[0])

def monteLTValue(left, right):
    """
    return left[1] < right[1]
    """

    return monteLessThan(left[1], right[1])

MonteSorter = make_timsort_class(lt=monteLessThan)
KeySorter = make_timsort_class(lt=monteLTKey)
ValueSorter = make_timsort_class(lt=monteLTValue)
Example #18
0
                t = sum_values(self.region_times, self.categories[cat], cat, self.region_subs)
                t_gc = sum_values(self.region_gc_times, self.categories[cat], cat, self.region_gc_subs)
                if not(0 == t) and not(0 == t_gc):
                    self.region_times[cat] = t
                    self.region_gc_times[cat] = t_gc

            self.loop(self.region_times, self.region_gc_times, 0)
            self.report(0, "total", self.total, " [%s]"%self.total_gc, "ms", "")

linklet_perf = LinkletPerf()

def second(l,i):
    x,y = l[i]
    return y

Sorter = make_timsort_class(getitem=second)

def ht_to_sorted_list(ht):
    l = []
    for k,v in enumerate(ht):
        l.append((k,v))
    s = Sorter(l)
    s.sort()
    return l


def table_add(t, l, v):
    t.setdefault(l,0)
    t[l] += v

def lprintf(fmt, args):
Example #19
0
        else:
            builder.append(char)
            builder.append(':')
            builder.append(str(m))
            builder.append(';')
            return True

    def add_counter(self):
        self.counter += 1


class SerializerError(Exception):
    pass


IntSort = make_timsort_class()

def remove_duplicates_fin(lst):
    prev = lst[0]
    count = 1
    for i in range(1, len(lst)):
        if prev != lst[i]:
            prev = lst[i]
            count += 1
    result = [0] * (count + 1)
    prev = lst[0]
    result[0] = prev
    count = 1
    for i in range(1, len(lst)):
        if prev != lst[i]:
            prev = lst[i]
Example #20
0
def make_argsort_function(space, itemtype, comp_type, count=1):
    TP = itemtype.T
    step = rffi.sizeof(TP)

    class Repr(object):
        def __init__(self, index_stride_size, stride_size, size, values,
                     indexes, index_start, start):
            self.index_stride_size = index_stride_size
            self.stride_size = stride_size
            self.index_start = index_start
            self.start = start
            self.size = size
            self.values = values
            self.indexes = indexes

        def getitem(self, idx):
            if count < 2:
                v = raw_storage_getitem(TP, self.values,
                                        idx * self.stride_size + self.start)
            else:
                v = []
                for i in range(count):
                    _v = raw_storage_getitem(
                        TP, self.values,
                        idx * self.stride_size + self.start + step * i)
                    v.append(_v)
            if comp_type == 'int':
                v = widen(v)
            elif comp_type == 'float':
                v = float(v)
            elif comp_type == 'complex':
                v = [float(v[0]), float(v[1])]
            else:
                raise NotImplementedError('cannot reach')
            return (v,
                    raw_storage_getitem(
                        lltype.Signed, self.indexes,
                        idx * self.index_stride_size + self.index_start))

        def setitem(self, idx, item):
            if count < 2:
                raw_storage_setitem(self.values,
                                    idx * self.stride_size + self.start,
                                    rffi.cast(TP, item[0]))
            else:
                i = 0
                for val in item[0]:
                    raw_storage_setitem(
                        self.values,
                        idx * self.stride_size + self.start + i * step,
                        rffi.cast(TP, val))
                    i += 1
            raw_storage_setitem(
                self.indexes, idx * self.index_stride_size + self.index_start,
                item[1])

    class ArgArrayRepWithStorage(Repr):
        def __init__(self, index_stride_size, stride_size, size):
            start = 0
            dtype = descriptor.get_dtype_cache(space).w_longdtype
            indexes = dtype.itemtype.malloc(size * dtype.elsize)
            values = alloc_raw_storage(size * stride_size,
                                       track_allocation=False)
            Repr.__init__(self, dtype.elsize, stride_size, size, values,
                          indexes, start, start)

        def __del__(self):
            free_raw_storage(self.indexes, track_allocation=False)
            free_raw_storage(self.values, track_allocation=False)

    def arg_getitem(lst, item):
        return lst.getitem(item)

    def arg_setitem(lst, item, value):
        lst.setitem(item, value)

    def arg_length(lst):
        return lst.size

    def arg_getitem_slice(lst, start, stop):
        retval = ArgArrayRepWithStorage(lst.index_stride_size, lst.stride_size,
                                        stop - start)
        for i in range(stop - start):
            retval.setitem(i, lst.getitem(i + start))
        return retval

    if count < 2:

        def arg_lt(a, b):
            # Does numpy do <= ?
            return a[0] < b[0] or b[0] != b[0] and a[0] == a[0]
    else:

        def arg_lt(a, b):
            for i in range(count):
                if b[0][i] != b[0][i] and a[0][i] == a[0][i]:
                    return True
                elif b[0][i] == b[0][i] and a[0][i] != a[0][i]:
                    return False
            for i in range(count):
                if a[0][i] < b[0][i]:
                    return True
                elif a[0][i] > b[0][i]:
                    return False
            # Does numpy do True?
            return False

    ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
                                 arg_getitem_slice, arg_lt)

    def argsort(arr, space, w_axis):
        if w_axis is space.w_None:
            # note that it's fine ot pass None here as we're not going
            # to pass the result around (None is the link to base in slices)
            if arr.get_size() > 0:
                arr = arr.reshape(None, [arr.get_size()])
            axis = 0
        elif w_axis is None:
            axis = -1
        else:
            axis = space.int_w(w_axis)
        # create array of indexes
        dtype = descriptor.get_dtype_cache(space).w_longdtype
        index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype)
        with index_arr.implementation as storage, arr as arr_storage:
            if len(arr.get_shape()) == 1:
                for i in range(arr.get_size()):
                    raw_storage_setitem(storage, i * INT_SIZE, i)
                r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage,
                         storage, 0, arr.start)
                ArgSort(r).sort()
            else:
                shape = arr.get_shape()
                if axis < 0:
                    axis = len(shape) + axis
                if axis < 0 or axis >= len(shape):
                    raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
                arr_iter = AllButAxisIter(arr, axis)
                arr_state = arr_iter.reset()
                index_impl = index_arr.implementation
                index_iter = AllButAxisIter(index_impl, axis)
                index_state = index_iter.reset()
                stride_size = arr.strides[axis]
                index_stride_size = index_impl.strides[axis]
                axis_size = arr.shape[axis]
                while not arr_iter.done(arr_state):
                    for i in range(axis_size):
                        raw_storage_setitem(
                            storage,
                            i * index_stride_size + index_state.offset, i)
                    r = Repr(index_stride_size, stride_size, axis_size,
                             arr_storage, storage, index_state.offset,
                             arr_state.offset)
                    ArgSort(r).sort()
                    arr_state = arr_iter.next(arr_state)
                    index_state = index_iter.next(index_state)
            return index_arr

    return argsort
Example #21
0
    sort_starts(starts).sort()
    for current, stop, op in starts:
        assert current <= stop
        if len(avail) > 0:
            op.i = avail.pop()
        else:
            op.i = body.tmpc
            body.tmpc += 1
        stops.append((stop, op))
        sort_ends(stops).sort()
        while len(stops) > 0 and stops[0][0] < current:
            _, exp = stops.pop(0)
            assert exp.i not in avail
            avail.append(exp.i)

sort_starts = make_timsort_class(lt=lambda x, y: x[0] < y[0])
sort_ends = make_timsort_class(lt=lambda x, y: x[0] < y[0])

#    This is just here in case my register alloc func messes up
#    But I will require better tools for debugging my dumps.
#    if True:
#        tab = {}
#        def opval_repr(op):
#            return "%s:r%d" % (tab[op], op.i)
#        for block in body.blocks:
#            i = block.base
#            for op in block:
#                tab[op] = i
#                i += 1
#        for block in body.blocks:
#            i = block.base
Example #22
0
from rpython.rlib.rstring import StringBuilder
from rpython.rlib.rawstorage import alloc_raw_storage, free_raw_storage, \
    raw_storage_getitem, raw_storage_setitem, RAW_STORAGE
from rpython.rtyper.lltypesystem import rffi, lltype, llmemory
from pypy.module.micronumpy import support, loop, constants as NPY
from pypy.module.micronumpy.base import convert_to_array, W_NDimArray, \
    ArrayArgumentException, W_NumpyObject
from pypy.module.micronumpy.iterators import ArrayIter
from pypy.module.micronumpy.strides import (
    IntegerChunk, SliceChunk, NewAxisChunk, EllipsisChunk, BooleanChunk,
    new_view, calc_strides, calc_new_strides, shape_agreement,
    calculate_broadcast_strides, calc_backstrides, calc_start, is_c_contiguous,
    is_f_contiguous)
from rpython.rlib.objectmodel import keepalive_until_here

TimSort = make_timsort_class()
class StrideSort(TimSort):
    '''
    argsort (return the indices to sort) a list of strides
    '''
    def __init__(self, rangelist, strides, order):
        self.strides = strides
        self.order = order
        TimSort.__init__(self, rangelist)

    def lt(self, a, b):
        if self.order == NPY.CORDER:
            return self.strides[a] <= self.strides[b]
        return self.strides[a] < self.strides[b]

Example #23
0
                    self.region_gc_times[cat] = t_gc

            self.loop(self.region_times, self.region_gc_times, 0)
            self.report(0, "total", self.total, " [%s]" % self.total_gc, "ms",
                        "")


linklet_perf = LinkletPerf()


def second(l, i):
    x, y = l[i]
    return y


Sorter = make_timsort_class(getitem=second)


def ht_to_sorted_list(ht):
    l = []
    for k, v in enumerate(ht):
        l.append((k, v))
    s = Sorter(l)
    s.sort()
    return l


def table_add(t, l, v):
    t.setdefault(l, 0)
    t[l] += v
Example #24
0
            last_group_offset += 1
        self.group_offsets.append(last_group_offset)
        self.group_state[group] = self.CLOSED

    def normalize_group(self, name):
        if name.isdigit():
            return int(name)
        else:
            return self.group_index[name]

    def is_open_group(self, name):
        group = self.normalize_group(name)
        return group in self.group_state and self.group_state[group] == self.OPEN


BaseSorter = make_timsort_class()


class BranchSorter(BaseSorter):
    def __init__(self, items, order):
        BaseSorter.__init__(self, items)
        self.order = order

    def lt(self, a, b):
        return self.order[a[0]] < self.order[b[0]]


class CompilerContext(object):
    def __init__(self):
        self.data = []
Example #25
0
from rpython.rlib import jit_libffi, clibffi

from pypy.module.cppyy import converter, executor, helper


class FastCallNotPossible(Exception):
    pass

# overload priorities: lower is preferred
priority = { 'void*'  : 100,
             'void**' : 100,
             'float'  :  30,
             'double' :  10, }

from rpython.rlib.listsort import make_timsort_class
CPPMethodBaseTimSort = make_timsort_class()
class CPPMethodSort(CPPMethodBaseTimSort):
    def lt(self, a, b):
        return a.priority() < b.priority()

@unwrap_spec(name=str)
def load_dictionary(space, name):
    try:
        cdll = capi.c_load_dictionary(name)
    except rdynload.DLOpenError, e:
        raise OperationError(space.w_RuntimeError, space.wrap(str(e.msg)))
    return W_CPPLibrary(space, cdll)

class State(object):
    def __init__(self, space):
        self.cppscope_cache = {
Example #26
0
def make_argsort_function(space, itemtype, comp_type, count=1):
    TP = itemtype.T
    step = rffi.sizeof(TP)

    class Repr(object):
        def __init__(self, index_stride_size, stride_size, size, values,
                     indexes, index_start, start):
            self.index_stride_size = index_stride_size
            self.stride_size = stride_size
            self.index_start = index_start
            self.start = start
            self.size = size
            self.values = values
            self.indexes = indexes

        def getitem(self, idx):
            if count < 2:
                v = raw_storage_getitem(TP, self.values, idx * self.stride_size
                                    + self.start)
            else:
                v = []
                for i in range(count):
                    _v = raw_storage_getitem(TP, self.values, idx * self.stride_size
                                    + self.start + step * i)
                    v.append(_v)
            if comp_type == 'int':
                v = widen(v)
            elif comp_type == 'float':
                v = float(v)
            elif comp_type == 'complex':
                v = [float(v[0]),float(v[1])]
            else:
                raise NotImplementedError('cannot reach')
            return (v, raw_storage_getitem(lltype.Signed, self.indexes,
                                           idx * self.index_stride_size +
                                           self.index_start))

        def setitem(self, idx, item):
            if count < 2:
                raw_storage_setitem(self.values, idx * self.stride_size +
                                self.start, rffi.cast(TP, item[0]))
            else:
                i = 0
                for val in item[0]:
                    raw_storage_setitem(self.values, idx * self.stride_size +
                                self.start + i*step, rffi.cast(TP, val))
                    i += 1
            raw_storage_setitem(self.indexes, idx * self.index_stride_size +
                                self.index_start, item[1])

    class ArgArrayRepWithStorage(Repr):
        def __init__(self, index_stride_size, stride_size, size):
            start = 0
            dtype = descriptor.get_dtype_cache(space).w_longdtype
            indexes = dtype.itemtype.malloc(size * dtype.elsize)
            values = alloc_raw_storage(size * stride_size,
                                            track_allocation=False)
            Repr.__init__(self, dtype.elsize, stride_size,
                          size, values, indexes, start, start)

        def __del__(self):
            free_raw_storage(self.indexes, track_allocation=False)
            free_raw_storage(self.values, track_allocation=False)

    def arg_getitem(lst, item):
        return lst.getitem(item)

    def arg_setitem(lst, item, value):
        lst.setitem(item, value)

    def arg_length(lst):
        return lst.size

    def arg_getitem_slice(lst, start, stop):
        retval = ArgArrayRepWithStorage(lst.index_stride_size, lst.stride_size,
                stop-start)
        for i in range(stop-start):
            retval.setitem(i, lst.getitem(i+start))
        return retval

    if count < 2:
        def arg_lt(a, b):
            # Does numpy do <= ?
            return a[0] < b[0] or b[0] != b[0] and a[0] == a[0]
    else:
        def arg_lt(a, b):
            for i in range(count):
                if b[0][i] != b[0][i] and a[0][i] == a[0][i]:
                    return True
                elif b[0][i] == b[0][i] and a[0][i] != a[0][i]:
                    return False
            for i in range(count):
                if a[0][i] < b[0][i]:
                    return True
                elif a[0][i] > b[0][i]:
                    return False
            # Does numpy do True?
            return False

    ArgSort = make_timsort_class(arg_getitem, arg_setitem, arg_length,
                                 arg_getitem_slice, arg_lt)

    def argsort(arr, space, w_axis):
        if w_axis is space.w_None:
            # note that it's fine ot pass None here as we're not going
            # to pass the result around (None is the link to base in slices)
            if arr.get_size() > 0:
                arr = arr.reshape(None, [arr.get_size()])
            axis = 0
        elif w_axis is None:
            axis = -1
        else:
            axis = space.int_w(w_axis)
        # create array of indexes
        dtype = descriptor.get_dtype_cache(space).w_longdtype
        index_arr = W_NDimArray.from_shape(space, arr.get_shape(), dtype)
        with index_arr.implementation as storage, arr as arr_storage:
            if len(arr.get_shape()) == 1:
                for i in range(arr.get_size()):
                    raw_storage_setitem(storage, i * INT_SIZE, i)
                r = Repr(INT_SIZE, arr.strides[0], arr.get_size(), arr_storage,
                         storage, 0, arr.start)
                ArgSort(r).sort()
            else:
                shape = arr.get_shape()
                if axis < 0:
                    axis = len(shape) + axis
                if axis < 0 or axis >= len(shape):
                    raise oefmt(space.w_IndexError, "Wrong axis %d", axis)
                arr_iter = AllButAxisIter(arr, axis)
                arr_state = arr_iter.reset()
                index_impl = index_arr.implementation
                index_iter = AllButAxisIter(index_impl, axis)
                index_state = index_iter.reset()
                stride_size = arr.strides[axis]
                index_stride_size = index_impl.strides[axis]
                axis_size = arr.shape[axis]
                while not arr_iter.done(arr_state):
                    for i in range(axis_size):
                        raw_storage_setitem(storage, i * index_stride_size +
                                            index_state.offset, i)
                    r = Repr(index_stride_size, stride_size, axis_size,
                         arr_storage, storage, index_state.offset, arr_state.offset)
                    ArgSort(r).sort()
                    arr_state = arr_iter.next(arr_state)
                    index_state = index_iter.next(index_state)
            return index_arr

    return argsort
Example #27
0
    def __init__(self, recorder, label):
        self.recorder = recorder
        self.label = label

    def __enter__(self):
        self.recorder.pushContext(self.label)

    def __exit__(self, *unused):
        self.recorder.popContext()


def scriptCountCmp(left, right):
    return left[1] > right[1]

ScriptSorter = make_timsort_class(lt=scriptCountCmp)


class Recorder(object):

    startTime = endTime = 0

    def __init__(self):
        self.timings = {}
        self.rates = {}
        self.scripts = {}
        self.contextStack = []

    def start(self):
        self.startTime = time()
Example #28
0
File: nodes.py Project: dckc/typhon
from typhon.objects.collections.helpers import asSet
from typhon.objects.collections.lists import unwrapList, wrapList
from typhon.objects.collections.maps import EMPTY_MAP, monteMap
from typhon.objects.data import (BigInt, CharObject, DoubleObject, IntObject,
                                 StrObject, promoteToBigInt, unwrapStr)
from typhon.objects.ejectors import throwStr
from typhon.objects.root import Object, audited
from typhon.pretty import Buffer, LineWriter
from typhon.profile import profileTyphon
from typhon.quoting import quoteChar, quoteStr


def lt0(a, b):
    return a[0] < b[0]

TimSort0 = make_timsort_class(lt=lt0)


class InvalidAST(LoadFailed):
    """
    An AST was ill-formed.
    """


@autohelp
@audited.DF
class KernelAstStamp(Object):

    @method("Bool", "Any")
    def audit(self, audition):
        return True
Example #29
0
                                           stack=proc.frames,
                                           env=proc.current_frame().env)

                proc.step()

        debug(0, ['%%%%% PHASE: resolve %%%%%'])
        for channel in self.channels.table:
            debug(0, ['+', channel.s()])
            assert channel.channelable is not None
            channel.channelable.resolve()

        debug(0, ['%%%%% PHASE: check %%%%%'])
        for p in self.procs.table:
            debug(0, [p.s()])

        running = 0
        waiting = 0
        for proc in self.procs.table:
            if proc.is_running():
                running += 1
            elif proc.state == Proc.WAITING:
                waiting += 1

        if running == 0 and waiting > 0: raise Deadlock
        if running == 0: raise Done


machine = Machine()

age_sort = make_timsort_class(lt=lambda p, q: p.age < q.age)