Skip to content

0.4.1

Choose a tag to compare

@franknoh franknoh released this 25 Sep 13:01
· 264 commits to main since this release
21c1bee

Changes

This release adds the two pieces 0.4.0 left out: objects that order, hash, and compare themselves inside collections, and generic classes in inheritance.

Objects as elements and keys

  • Heap[Task], Vec[Task].sort(), sorted(...), TreeMap[Task, V], and TreeSet[Task] order instances by the class's __lt__.
  • HashMap[Point, V] and HashSet[Point] hash and compare them by __hash__ and __eq__, or by identity where the class defines neither.
  • Native code calls the compiled methods from inside the collection runtime.
    • A guard that fails inside one hands the call back to Python, and a standalone binary prints what CPython raises.
    • __eq__(self, other: object) works as Python's typing spells it.
    • A method a subclass overrides keeps the collection in Python.
  • The checker takes a class as a key where Python could use it as one, and refuses an unhashable one (E1305). A tree's keys are one key when neither is less than the other.
from ppy import HashMap, Heap


class Cell:
    def __init__(self, row: int, col: int) -> None:
        self.row: int = row
        self.col: int = col

    def __eq__(self, other: object) -> bool:
        return isinstance(other, Cell) and self.row == other.row and self.col == other.col

    def __hash__(self) -> int:
        return self.row * 1000 + self.col


class Visit:
    def __init__(self, cost: int, cell: Cell) -> None:
        self.cost: int = cost
        self.cell: Cell = cell

    def __lt__(self, other: "Visit") -> bool:
        return self.cost < other.cost

A search over Heap[Visit] and HashMap[Cell, int] compiles to native code with both collections.

Generic bases

  • class Counted[T](Stack[T]), class IntStack(Stack[int]), and class Pair[A, B](Box[A]) are checked and compiled.
    • The checker binds each base's parameters from what the class gives it, so IntStack().pop() is an int, and super() is the base with those arguments.
    • Native code lays out, instantiates, and dispatches by the same bindings.

Fixes

  • TreeSet[str].pop_min()/pop_max() natively returned a string the tree had already freed.
  • A string or object made only to look a key up was freed in the wrong place when the lookup ran inside a loop.

Install or upgrade with pip install --upgrade ppy-lang==0.4.1.

The full repository gate passed, and the emitted C and C++ of the new tests runs clean under AddressSanitizer with leak detection.