WIP: make libraries and names first-class!
This commit is contained in:
parent
f834ec6be5
commit
7aaf73cb37
3 changed files with 1780 additions and 1812 deletions
|
|
@ -5,6 +5,7 @@ from .types import layer_t, annotations_t
|
|||
|
||||
from .array import is_scalar
|
||||
from .autoslots import AutoSlots
|
||||
from .deferreddict import DeferredDict
|
||||
|
||||
from .bitwise import get_bit, set_bit
|
||||
from .vertices import (
|
||||
|
|
|
|||
48
masque/utils/deferreddict.py
Normal file
48
masque/utils/deferreddict.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
from typing import Callable, TypeVar, Generic
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
Key = TypeVar('Key')
|
||||
Value = TypeVar('Value')
|
||||
|
||||
|
||||
class DeferredDict(dict, Generic[Key, Value]):
|
||||
"""
|
||||
This is a modified `dict` which is used to defer loading/generating
|
||||
values until they are accessed.
|
||||
|
||||
```
|
||||
bignum = my_slow_function() # slow function call, would like to defer this
|
||||
numbers = DeferredDict()
|
||||
numbers['big'] = my_slow_function # no slow function call here
|
||||
assert(bignum == numbers['big']) # first access is slow (function called)
|
||||
assert(bignum == numbers['big']) # second access is fast (result is cached)
|
||||
```
|
||||
|
||||
The `set_const` method is provided for convenience;
|
||||
`numbers['a'] = lambda: 10` is equivalent to `numbers.set_const('a', 10)`.
|
||||
"""
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
dict.__init__(self)
|
||||
self.update(*args, **kwargs)
|
||||
|
||||
def __setitem__(self, key: Key, value: Callable[[], Value]) -> None:
|
||||
cached_fn = lru_cache(maxsize=1)(value)
|
||||
dict.__setitem__(self, key, cached_fn)
|
||||
|
||||
def __getitem__(self, key: Key) -> Value:
|
||||
return dict.__getitem__(self, key)()
|
||||
|
||||
def update(self, *args, **kwargs) -> None:
|
||||
for k, v in dict(*args, **kwargs).items():
|
||||
self[k] = v
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return '<DeferredDict with keys ' + repr(set(self.keys())) + '>'
|
||||
|
||||
def set_const(self, key: Key, value: Value) -> None:
|
||||
"""
|
||||
Convenience function to avoid having to manually wrap
|
||||
constant values into callables.
|
||||
"""
|
||||
self[key] = lambda: value
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
2D bin-packing
|
||||
"""
|
||||
from typing import Tuple, List, Set, Sequence, Callable
|
||||
from typing import Tuple, List, Set, Sequence, Callable, Mapping
|
||||
|
||||
import numpy
|
||||
from numpy.typing import NDArray, ArrayLike
|
||||
|
|
@ -11,16 +11,18 @@ from ..pattern import Pattern
|
|||
from ..subpattern import SubPattern
|
||||
|
||||
|
||||
def pack_patterns(patterns: Sequence[Pattern],
|
||||
regions: numpy.ndarray,
|
||||
spacing: Tuple[float, float],
|
||||
presort: bool = True,
|
||||
allow_rejects: bool = True,
|
||||
packer: Callable = maxrects_bssf,
|
||||
) -> Tuple[Pattern, List[Pattern]]:
|
||||
def pack_patterns(
|
||||
library: Mapping[str, Pattern],
|
||||
patterns: Sequence[str],
|
||||
regions: numpy.ndarray,
|
||||
spacing: Tuple[float, float],
|
||||
presort: bool = True,
|
||||
allow_rejects: bool = True,
|
||||
packer: Callable = maxrects_bssf,
|
||||
) -> Tuple[Pattern, List[str]]:
|
||||
half_spacing = numpy.array(spacing) / 2
|
||||
|
||||
bounds = [pp.get_bounds() for pp in patterns]
|
||||
bounds = [library[pp].get_bounds() for pp in patterns]
|
||||
sizes = [bb[1] - bb[0] + spacing if bb is not None else spacing for bb in bounds]
|
||||
offsets = [half_spacing - bb[0] if bb is not None else (0, 0) for bb in bounds]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue