mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-07 20:20:17 +00:00
Compare commits
5 Commits
e96f1e92bd
...
634648c1d4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
634648c1d4 | ||
|
|
a27e0e2210 | ||
|
|
d744e654cc | ||
|
|
7e9fd338b7 | ||
|
|
3d6325473e |
207
.github/workflows/main.yml
vendored
Normal file
207
.github/workflows/main.yml
vendored
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
name: build
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- 'docs/**'
|
||||||
|
- 'web/**'
|
||||||
|
- '**.md'
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- 'docs/**'
|
||||||
|
- 'web/**'
|
||||||
|
- '**.md'
|
||||||
|
jobs:
|
||||||
|
build_win32_amalgamated:
|
||||||
|
runs-on: windows-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
- name: Compile
|
||||||
|
shell: powershell
|
||||||
|
run: |
|
||||||
|
python amalgamate.py
|
||||||
|
cd amalgamated
|
||||||
|
cl.exe /std:c11 /utf-8 /Ox /I. pocketpy.c main.c /link /out:pkpy.exe
|
||||||
|
build_win32:
|
||||||
|
runs-on: windows-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
- name: Compile
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
mkdir -p output/x86_64
|
||||||
|
python cmake_build.py
|
||||||
|
cp main.exe output/x86_64
|
||||||
|
cp pocketpy.dll output/x86_64
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: windows
|
||||||
|
path: output
|
||||||
|
- name: Unit Test
|
||||||
|
run: python scripts/run_tests.py
|
||||||
|
- name: Benchmark
|
||||||
|
run: python scripts/run_tests.py benchmark
|
||||||
|
build_linux:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Setup Clang
|
||||||
|
uses: egor-tensin/setup-clang@v1
|
||||||
|
with:
|
||||||
|
version: 15
|
||||||
|
platform: x64
|
||||||
|
- name: Install dependencies
|
||||||
|
run: sudo apt-get install -y libclang-rt-15-dev
|
||||||
|
- name: Unit Test with Coverage
|
||||||
|
run: bash run_tests.sh
|
||||||
|
- name: Upload coverage reports to Codecov
|
||||||
|
uses: codecov/codecov-action@v4
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
directory: .coverage
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
|
- name: Compile and Test
|
||||||
|
run: |
|
||||||
|
mkdir -p output/x86_64
|
||||||
|
python cmake_build.py
|
||||||
|
python scripts/run_tests.py
|
||||||
|
cp main output/x86_64
|
||||||
|
cp libpocketpy.so output/x86_64
|
||||||
|
env:
|
||||||
|
CC: clang
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: linux
|
||||||
|
path: output
|
||||||
|
- name: Benchmark
|
||||||
|
run: python scripts/run_tests.py benchmark
|
||||||
|
build_linux_x86:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Setup Alpine Linux for aarch64
|
||||||
|
uses: jirutka/setup-alpine@v1
|
||||||
|
with:
|
||||||
|
arch: x86
|
||||||
|
packages: gcc g++ make cmake libc-dev linux-headers python3
|
||||||
|
- name: Build and Test
|
||||||
|
run: |
|
||||||
|
uname -m
|
||||||
|
python cmake_build.py
|
||||||
|
python scripts/run_tests.py
|
||||||
|
python scripts/run_tests.py benchmark
|
||||||
|
shell: alpine.sh --root {0}
|
||||||
|
build_darwin:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Compile and Test
|
||||||
|
run: |
|
||||||
|
python cmake_build.py
|
||||||
|
python scripts/run_tests.py
|
||||||
|
- name: Benchmark
|
||||||
|
run: python scripts/run_tests.py benchmark
|
||||||
|
- name: Test Amalgamated Build
|
||||||
|
run: python amalgamate.py
|
||||||
|
build_android:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- uses: nttld/setup-ndk@v1
|
||||||
|
id: setup-ndk
|
||||||
|
with:
|
||||||
|
ndk-version: r23
|
||||||
|
local-cache: false
|
||||||
|
add-to-path: false
|
||||||
|
- name: Compile Shared Library
|
||||||
|
run: |
|
||||||
|
bash build_android.sh arm64-v8a
|
||||||
|
bash build_android.sh armeabi-v7a
|
||||||
|
bash build_android.sh x86_64
|
||||||
|
|
||||||
|
mkdir -p output/arm64-v8a
|
||||||
|
mkdir -p output/armeabi-v7a
|
||||||
|
mkdir -p output/x86_64
|
||||||
|
|
||||||
|
cp build/android/arm64-v8a/libpocketpy.so output/arm64-v8a
|
||||||
|
cp build/android/armeabi-v7a/libpocketpy.so output/armeabi-v7a
|
||||||
|
cp build/android/x86_64/libpocketpy.so output/x86_64
|
||||||
|
env:
|
||||||
|
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: android
|
||||||
|
path: output
|
||||||
|
build_ios:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- name: Compile Frameworks
|
||||||
|
run: |
|
||||||
|
git clone https://github.com/leetal/ios-cmake --depth 1 ~/ios-cmake
|
||||||
|
bash build_ios.sh
|
||||||
|
mkdir -p output
|
||||||
|
cp -r build/pocketpy.xcframework output/pocketpy.xcframework
|
||||||
|
- uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: ios
|
||||||
|
path: output
|
||||||
|
|
||||||
|
merge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: [ build_win32, build_linux, build_darwin, build_android, build_ios ]
|
||||||
|
steps:
|
||||||
|
- name: "Create output directory"
|
||||||
|
run: "mkdir $GITHUB_WORKSPACE/output"
|
||||||
|
|
||||||
|
- name: "Merge win32"
|
||||||
|
uses: actions/download-artifact@v4.1.7
|
||||||
|
with:
|
||||||
|
name: windows
|
||||||
|
path: $GITHUB_WORKSPACE/output/windows
|
||||||
|
|
||||||
|
- name: "Merge linux"
|
||||||
|
uses: actions/download-artifact@v4.1.7
|
||||||
|
with:
|
||||||
|
name: linux
|
||||||
|
path: $GITHUB_WORKSPACE/output/linux
|
||||||
|
|
||||||
|
# - name: "Merge darwin"
|
||||||
|
# uses: actions/download-artifact@v4.1.7
|
||||||
|
# with:
|
||||||
|
# name: macos
|
||||||
|
# path: $GITHUB_WORKSPACE/output/macos
|
||||||
|
|
||||||
|
- name: "Merge android"
|
||||||
|
uses: actions/download-artifact@v4.1.7
|
||||||
|
with:
|
||||||
|
name: android
|
||||||
|
path: $GITHUB_WORKSPACE/output/android
|
||||||
|
|
||||||
|
- name: "Merge ios"
|
||||||
|
uses: actions/download-artifact@v4.1.7
|
||||||
|
with:
|
||||||
|
name: ios
|
||||||
|
path: $GITHUB_WORKSPACE/output/ios
|
||||||
|
|
||||||
|
- name: "Upload merged artifact"
|
||||||
|
uses: actions/upload-artifact@v4.3.3
|
||||||
|
with:
|
||||||
|
name: all-in-one
|
||||||
|
path: $GITHUB_WORKSPACE/output
|
||||||
76
.github/workflows/pybind11.yml
vendored
Normal file
76
.github/workflows/pybind11.yml
vendored
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
name: PKBIND Build and Test
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- "docs/**"
|
||||||
|
- "web/**"
|
||||||
|
- "**.md"
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- "docs/**"
|
||||||
|
- "web/**"
|
||||||
|
- "**.md"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build_linux:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up GCC
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y gcc g++
|
||||||
|
|
||||||
|
- name: Set up CMake
|
||||||
|
uses: jwlawson/actions-setup-cmake@v1.10
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
cd include/pybind11/tests
|
||||||
|
cmake -B build
|
||||||
|
cmake --build build --config Release --parallel
|
||||||
|
./build/PKBIND_TEST
|
||||||
|
|
||||||
|
build_win:
|
||||||
|
runs-on: windows-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up MSVC
|
||||||
|
uses: ilammy/msvc-dev-cmd@v1
|
||||||
|
|
||||||
|
- name: Set up CMake
|
||||||
|
uses: jwlawson/actions-setup-cmake@v1.10
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
cd include\pybind11\tests
|
||||||
|
cmake -B build
|
||||||
|
cmake --build build --config Release --parallel
|
||||||
|
build\Release\PKBIND_TEST.exe
|
||||||
|
|
||||||
|
build_mac:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Clang
|
||||||
|
run: |
|
||||||
|
brew install llvm
|
||||||
|
echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc
|
||||||
|
source ~/.zshrc
|
||||||
|
|
||||||
|
- name: Set up CMake
|
||||||
|
uses: jwlawson/actions-setup-cmake@v1.10
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: |
|
||||||
|
cd include/pybind11/tests
|
||||||
|
cmake -B build -DENABLE_TEST=ON
|
||||||
|
cmake --build build --config Release --parallel
|
||||||
|
./build/PKBIND_TEST
|
||||||
42
.github/workflows/website.yml
vendored
Normal file
42
.github/workflows/website.yml
vendored
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
name: website
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
###################################################
|
||||||
|
- uses: actions/setup-node@v3.1.1
|
||||||
|
- name: Retype build
|
||||||
|
run: |
|
||||||
|
python scripts/gen_docs.py
|
||||||
|
cd docs
|
||||||
|
npm install retypeapp -g
|
||||||
|
retype build
|
||||||
|
###################################################
|
||||||
|
- name: Setup emsdk
|
||||||
|
uses: mymindstorm/setup-emsdk@v12
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
actions-cache-folder: 'emsdk-cache'
|
||||||
|
- name: Compile
|
||||||
|
run: |
|
||||||
|
bash build_web.sh
|
||||||
|
mv web docs/.retype/static
|
||||||
|
###################################################
|
||||||
|
- uses: crazy-max/ghaction-github-pages@v3
|
||||||
|
with:
|
||||||
|
target_branch: gh-pages
|
||||||
|
build_dir: docs/.retype
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
BIN
.github/workflows/workflows.zip
vendored
BIN
.github/workflows/workflows.zip
vendored
Binary file not shown.
@ -1,10 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
|
#include "pocketpy/common/smallmap.h"
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/objects/base.h"
|
||||||
#include "pocketpy/common/sstream.h"
|
|
||||||
#include "pocketpy/interpreter/vm.h"
|
|
||||||
|
|
||||||
typedef struct c11_array2d_like {
|
typedef struct c11_array2d_like {
|
||||||
int n_cols;
|
int n_cols;
|
||||||
@ -27,7 +25,9 @@ typedef struct c11_array2d {
|
|||||||
|
|
||||||
typedef struct c11_array2d_view {
|
typedef struct c11_array2d_view {
|
||||||
c11_array2d_like header;
|
c11_array2d_like header;
|
||||||
c11_array2d_like* array;
|
void* ctx;
|
||||||
|
py_Ref (*f_get)(void* ctx, int col, int row);
|
||||||
|
bool (*f_set)(void* ctx, int col, int row, py_Ref value);
|
||||||
c11_vec2i origin;
|
c11_vec2i origin;
|
||||||
} c11_array2d_view;
|
} c11_array2d_view;
|
||||||
|
|
||||||
@ -54,12 +54,5 @@ typedef struct c11_chunked_array2d {
|
|||||||
py_TValue context_builder;
|
py_TValue context_builder;
|
||||||
} c11_chunked_array2d;
|
} c11_chunked_array2d;
|
||||||
|
|
||||||
void c11_chunked_array2d__dtor(c11_chunked_array2d* self);
|
|
||||||
|
|
||||||
py_Ref c11_chunked_array2d__get(c11_chunked_array2d* self, int col, int row);
|
py_Ref c11_chunked_array2d__get(c11_chunked_array2d* self, int col, int row);
|
||||||
bool c11_chunked_array2d__set(c11_chunked_array2d* self, int col, int row, py_Ref value) PY_RAISE;
|
bool c11_chunked_array2d__set(c11_chunked_array2d* self, int col, int row, py_Ref value);
|
||||||
void c11_chunked_array2d__del(c11_chunked_array2d* self, int col, int row);
|
|
||||||
|
|
||||||
void pk__register_chunked_array2d(py_Ref mod);
|
|
||||||
|
|
||||||
/* array2d_view */
|
|
||||||
|
|||||||
@ -302,10 +302,14 @@ PK_API const char* py_tpname(py_Type type);
|
|||||||
/// Call a type to create a new instance.
|
/// Call a type to create a new instance.
|
||||||
PK_API bool py_tpcall(py_Type type, int argc, py_Ref argv) PY_RAISE PY_RETURN;
|
PK_API bool py_tpcall(py_Type type, int argc, py_Ref argv) PY_RAISE PY_RETURN;
|
||||||
|
|
||||||
/// Check if the object is an instance of the given type.
|
/// Check if the object is an instance of the given type exactly.
|
||||||
/// Raise `TypeError` if the check fails.
|
/// Raise `TypeError` if the check fails.
|
||||||
PK_API bool py_checktype(py_Ref self, py_Type type) PY_RAISE;
|
PK_API bool py_checktype(py_Ref self, py_Type type) PY_RAISE;
|
||||||
|
|
||||||
|
/// Check if the object is an instance of the given type or its subclass.
|
||||||
|
/// Raise `TypeError` if the check fails.
|
||||||
|
PK_API bool py_checkinstance(py_Ref self, py_Type type) PY_RAISE;
|
||||||
|
|
||||||
#define py_checkint(self) py_checktype(self, tp_int)
|
#define py_checkint(self) py_checktype(self, tp_int)
|
||||||
#define py_checkfloat(self) py_checktype(self, tp_float)
|
#define py_checkfloat(self) py_checktype(self, tp_float)
|
||||||
#define py_checkbool(self) py_checktype(self, tp_bool)
|
#define py_checkbool(self) py_checktype(self, tp_bool)
|
||||||
|
|||||||
@ -36,6 +36,7 @@ class array2d_like[T]:
|
|||||||
def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
|
def map[R](self, f: Callable[[T], R]) -> array2d[R]: ...
|
||||||
def apply(self, f: Callable[[T], T]) -> None: ...
|
def apply(self, f: Callable[[T], T]) -> None: ...
|
||||||
def copy(self) -> 'array2d[T]': ...
|
def copy(self) -> 'array2d[T]': ...
|
||||||
|
def tolist(self) -> list[list[T]]: ...
|
||||||
|
|
||||||
def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
|
def __eq__(self, other: object) -> array2d[bool]: ... # type: ignore
|
||||||
def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
|
def __ne__(self, other: object) -> array2d[bool]: ... # type: ignore
|
||||||
@ -98,7 +99,6 @@ class array2d[T](array2d_like[T]):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromlist(data: list[list[T]]) -> array2d[T]: ...
|
def fromlist(data: list[list[T]]) -> array2d[T]: ...
|
||||||
def tolist(self) -> list[list[T]]: ...
|
|
||||||
|
|
||||||
|
|
||||||
class chunked_array2d[T, TContext]:
|
class chunked_array2d[T, TContext]:
|
||||||
|
|||||||
@ -1,4 +1,7 @@
|
|||||||
#include "pocketpy/interpreter/array2d.h"
|
#include "pocketpy/interpreter/array2d.h"
|
||||||
|
#include "pocketpy/interpreter/vm.h"
|
||||||
|
#include "pocketpy/pocketpy.h"
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
static bool c11_array2d_like_is_valid(c11_array2d_like* self, unsigned int col, unsigned int row) {
|
static bool c11_array2d_like_is_valid(c11_array2d_like* self, unsigned int col, unsigned int row) {
|
||||||
return col < self->n_cols && row < self->n_rows;
|
return col < self->n_cols && row < self->n_rows;
|
||||||
@ -199,6 +202,21 @@ static bool array2d_like_copy(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool array2d_like_tolist(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
c11_array2d_like* self = py_touserdata(argv);
|
||||||
|
py_newlistn(py_retval(), self->n_rows);
|
||||||
|
for(int j = 0; j < self->n_rows; j++) {
|
||||||
|
py_Ref row_j = py_list_getitem(py_retval(), j);
|
||||||
|
py_newlistn(row_j, self->n_cols);
|
||||||
|
for(int i = 0; i < self->n_cols; i++) {
|
||||||
|
py_Ref item = self->f_get(self, i, j);
|
||||||
|
py_list_setitem(row_j, i, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool _check_same_shape(int colA, int rowA, int colB, int rowB) {
|
static bool _check_same_shape(int colA, int rowA, int colB, int rowB) {
|
||||||
if(colA != colB || rowA != rowB) {
|
if(colA != colB || rowA != rowB) {
|
||||||
const char* fmt = "expected the same shape: (%d, %d) != (%d, %d)";
|
const char* fmt = "expected the same shape: (%d, %d) != (%d, %d)";
|
||||||
@ -300,29 +318,62 @@ static bool _array2d_like_IndexError(c11_array2d_like* self, int col, int row) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static py_Ref c11_array2d_view__get(c11_array2d_view* self, int col, int row) {
|
static py_Ref c11_array2d_view__get(c11_array2d_view* self, int col, int row) {
|
||||||
return self->array->f_get(self->array, col + self->origin.x, row + self->origin.y);
|
return self->f_get(self->ctx, col + self->origin.x, row + self->origin.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool c11_array2d_view__set(c11_array2d_view* self, int col, int row, py_Ref value) {
|
static bool c11_array2d_view__set(c11_array2d_view* self, int col, int row, py_Ref value) {
|
||||||
return self->array->f_set(self->array, col + self->origin.x, row + self->origin.y, value);
|
return self->f_set(self->ctx, col + self->origin.x, row + self->origin.y, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _array2d_view(py_OutRef out,
|
static c11_array2d_view* _array2d_view__new(py_OutRef out,
|
||||||
c11_array2d_like* array,
|
py_Ref keepalive,
|
||||||
int start_col,
|
int start_col,
|
||||||
int start_row,
|
int start_row,
|
||||||
int width,
|
int width,
|
||||||
int height) {
|
int height) {
|
||||||
c11_array2d_view* res = py_newobject(out, tp_array2d_view, 1, sizeof(c11_array2d_view));
|
c11_array2d_view* res = py_newobject(out, tp_array2d_view, 1, sizeof(c11_array2d_view));
|
||||||
if(width <= 0 || height <= 0) return ValueError("width and height must be positive");
|
if(width <= 0 || height <= 0) {
|
||||||
|
ValueError("width and height must be positive");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
res->header.n_cols = width;
|
res->header.n_cols = width;
|
||||||
res->header.n_rows = height;
|
res->header.n_rows = height;
|
||||||
res->header.numel = width * height;
|
res->header.numel = width * height;
|
||||||
res->header.f_get = (py_Ref(*)(c11_array2d_like*, int, int))c11_array2d_view__get;
|
res->header.f_get = (py_Ref(*)(c11_array2d_like*, int, int))c11_array2d_view__get;
|
||||||
res->header.f_set = (bool (*)(c11_array2d_like*, int, int, py_Ref))c11_array2d_view__set;
|
res->header.f_set = (bool (*)(c11_array2d_like*, int, int, py_Ref))c11_array2d_view__set;
|
||||||
res->array = array;
|
|
||||||
res->origin.x = start_col;
|
res->origin.x = start_col;
|
||||||
res->origin.y = start_row;
|
res->origin.y = start_row;
|
||||||
|
py_setslot(out, 0, keepalive);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _array2d_view(py_OutRef out,
|
||||||
|
py_Ref keepalive,
|
||||||
|
c11_array2d_like* array,
|
||||||
|
int start_col,
|
||||||
|
int start_row,
|
||||||
|
int width,
|
||||||
|
int height) {
|
||||||
|
c11_array2d_view* res = _array2d_view__new(out, keepalive, start_col, start_row, width, height);
|
||||||
|
if(res == NULL) return false;
|
||||||
|
res->ctx = array;
|
||||||
|
res->f_get = (py_Ref(*)(void*, int, int))array->f_get;
|
||||||
|
res->f_set = (bool (*)(void*, int, int, py_Ref))array->f_set;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _chunked_array2d_view(py_OutRef out,
|
||||||
|
py_Ref keepalive,
|
||||||
|
c11_chunked_array2d* array,
|
||||||
|
int start_col,
|
||||||
|
int start_row,
|
||||||
|
int width,
|
||||||
|
int height) {
|
||||||
|
c11_array2d_view* res = _array2d_view__new(out, keepalive, start_col, start_row, width, height);
|
||||||
|
if(res == NULL) return false;
|
||||||
|
res->ctx = array;
|
||||||
|
res->f_get = (py_Ref(*)(void*, int, int))c11_chunked_array2d__get;
|
||||||
|
res->f_set = (bool (*)(void*, int, int, py_Ref))c11_chunked_array2d__set;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,8 +396,9 @@ static bool array2d_like__getitem__(int argc, py_Ref argv) {
|
|||||||
for(int j = 0; j < self->n_rows; j++) {
|
for(int j = 0; j < self->n_rows; j++) {
|
||||||
for(int i = 0; i < self->n_cols; i++) {
|
for(int i = 0; i < self->n_cols; i++) {
|
||||||
py_Ref item = self->f_get(self, i, j);
|
py_Ref item = self->f_get(self, i, j);
|
||||||
if(!py_checkbool(item)) return false;
|
py_Ref cond = mask->f_get(mask, i, j);
|
||||||
if(py_tobool(item)) py_list_append(py_retval(), item);
|
if(!py_checkbool(cond)) return false;
|
||||||
|
if(py_tobool(cond)) py_list_append(py_retval(), item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -368,7 +420,13 @@ static bool array2d_like__getitem__(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
if(py_istype(x, tp_slice) && py_istype(y, tp_slice)) {
|
if(py_istype(x, tp_slice) && py_istype(y, tp_slice)) {
|
||||||
HANDLE_SLICE();
|
HANDLE_SLICE();
|
||||||
return _array2d_view(py_retval(), self, start_col, start_row, slice_width, slice_height);
|
return _array2d_view(py_retval(),
|
||||||
|
argv,
|
||||||
|
self,
|
||||||
|
start_col,
|
||||||
|
start_row,
|
||||||
|
slice_width,
|
||||||
|
slice_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TypeError("expected `tuple[int, int]` or `tuple[slice, slice]`");
|
return TypeError("expected `tuple[int, int]` or `tuple[slice, slice]`");
|
||||||
@ -394,9 +452,9 @@ static bool array2d_like__setitem__(int argc, py_Ref argv) {
|
|||||||
if(!_array2d_like_check_same_shape(self, mask)) return false;
|
if(!_array2d_like_check_same_shape(self, mask)) return false;
|
||||||
for(int j = 0; j < self->n_rows; j++) {
|
for(int j = 0; j < self->n_rows; j++) {
|
||||||
for(int i = 0; i < self->n_cols; i++) {
|
for(int i = 0; i < self->n_cols; i++) {
|
||||||
py_Ref item = self->f_get(self, i, j);
|
py_Ref cond = mask->f_get(mask, i, j);
|
||||||
if(!py_checkbool(item)) return false;
|
if(!py_checkbool(cond)) return false;
|
||||||
if(py_tobool(item)) {
|
if(py_tobool(cond)) {
|
||||||
bool ok = self->f_set(self, i, j, value);
|
bool ok = self->f_set(self, i, j, value);
|
||||||
if(!ok) return false;
|
if(!ok) return false;
|
||||||
}
|
}
|
||||||
@ -533,16 +591,16 @@ static bool array2d_like_count_neighbors(int argc, py_Ref argv) {
|
|||||||
int n_offsets;
|
int n_offsets;
|
||||||
if(strcmp(neighborhood, "Moore") == 0) {
|
if(strcmp(neighborhood, "Moore") == 0) {
|
||||||
offsets = Moore;
|
offsets = Moore;
|
||||||
n_offsets = 8;
|
n_offsets = c11__count_array(Moore);
|
||||||
} else if(strcmp(neighborhood, "von Neumann") == 0) {
|
} else if(strcmp(neighborhood, "von Neumann") == 0) {
|
||||||
offsets = von_Neumann;
|
offsets = von_Neumann;
|
||||||
n_offsets = 4;
|
n_offsets = c11__count_array(von_Neumann);
|
||||||
} else {
|
} else {
|
||||||
return ValueError("neighborhood must be 'Moore' or 'von Neumann'");
|
return ValueError("neighborhood must be 'Moore' or 'von Neumann'");
|
||||||
}
|
}
|
||||||
for(int j = 0; j < self->n_rows; j++) {
|
for(int j = 0; j < self->n_rows; j++) {
|
||||||
for(int i = 0; i < self->n_cols; i++) {
|
for(int i = 0; i < self->n_cols; i++) {
|
||||||
int count = 0;
|
py_i64 count = 0;
|
||||||
for(int k = 0; k < n_offsets; k++) {
|
for(int k = 0; k < n_offsets; k++) {
|
||||||
int x = i + offsets[k].x;
|
int x = i + offsets[k].x;
|
||||||
int y = j + offsets[k].y;
|
int y = j + offsets[k].y;
|
||||||
@ -561,8 +619,53 @@ static bool array2d_like_count_neighbors(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pk__register_array2d_like(py_Ref mod) {
|
// convolve(self: array2d_like[int], kernel: array2d_like[int], padding: int) -> array2d[int]
|
||||||
|
static bool array2d_like_convolve(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(3);
|
||||||
|
if(!py_checkinstance(&argv[1], tp_array2d_like)) return false;
|
||||||
|
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||||
|
c11_array2d_like* self = py_touserdata(&argv[0]);
|
||||||
|
c11_array2d_like* kernel = py_touserdata(&argv[1]);
|
||||||
|
int padding = py_toint(py_arg(2));
|
||||||
|
if(kernel->n_cols != kernel->n_rows) return ValueError("kernel must be square");
|
||||||
|
int ksize = kernel->n_cols;
|
||||||
|
if(ksize % 2 == 0) return ValueError("kernel size must be odd");
|
||||||
|
int ksize_half = ksize / 2;
|
||||||
|
c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
|
||||||
|
for(int j = 0; j < self->n_rows; j++) {
|
||||||
|
for(int i = 0; i < self->n_cols; i++) {
|
||||||
|
py_i64 sum = 0;
|
||||||
|
for(int jj = 0; jj < ksize; jj++) {
|
||||||
|
for(int ii = 0; ii < ksize; ii++) {
|
||||||
|
int x = i + ii - ksize_half;
|
||||||
|
int y = j + jj - ksize_half;
|
||||||
|
py_i64 _0, _1;
|
||||||
|
if(x < 0 || x >= self->n_cols || y < 0 || y >= self->n_rows) {
|
||||||
|
_0 = padding;
|
||||||
|
} else {
|
||||||
|
py_Ref item = self->f_get(self, x, y);
|
||||||
|
if(!py_checkint(item)) return false;
|
||||||
|
_0 = py_toint(item);
|
||||||
|
}
|
||||||
|
py_Ref kitem = kernel->f_get(kernel, ii, jj);
|
||||||
|
if(!py_checkint(kitem)) return false;
|
||||||
|
_1 = py_toint(kitem);
|
||||||
|
sum += _0 * _1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
py_newint(c11_array2d__get(res, i, j), sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
py_assign(py_retval(), py_peek(-1));
|
||||||
|
py_pop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef HANDLE_SLICE
|
||||||
|
|
||||||
|
static void register_array2d_like(py_Ref mod) {
|
||||||
py_Type type = py_newtype("array2d_like", tp_object, mod, NULL);
|
py_Type type = py_newtype("array2d_like", tp_object, mod, NULL);
|
||||||
|
assert(type == tp_array2d_like);
|
||||||
|
|
||||||
py_bindproperty(type, "n_cols", array2d_like_n_cols, NULL);
|
py_bindproperty(type, "n_cols", array2d_like_n_cols, NULL);
|
||||||
py_bindproperty(type, "n_rows", array2d_like_n_rows, NULL);
|
py_bindproperty(type, "n_rows", array2d_like_n_rows, NULL);
|
||||||
@ -582,6 +685,7 @@ static void pk__register_array2d_like(py_Ref mod) {
|
|||||||
py_bindmethod(type, "map", array2d_like_map);
|
py_bindmethod(type, "map", array2d_like_map);
|
||||||
py_bindmethod(type, "apply", array2d_like_apply);
|
py_bindmethod(type, "apply", array2d_like_apply);
|
||||||
py_bindmethod(type, "copy", array2d_like_copy);
|
py_bindmethod(type, "copy", array2d_like_copy);
|
||||||
|
py_bindmethod(type, "tolist", array2d_like_tolist);
|
||||||
|
|
||||||
py_bindmagic(type, __eq__, array2d_like__eq__);
|
py_bindmagic(type, __eq__, array2d_like__eq__);
|
||||||
py_bindmagic(type, __ne__, array2d_like__ne__);
|
py_bindmagic(type, __ne__, array2d_like__ne__);
|
||||||
@ -594,6 +698,40 @@ static void pk__register_array2d_like(py_Ref mod) {
|
|||||||
py_bindmethod(type, "count", array2d_like_count);
|
py_bindmethod(type, "count", array2d_like_count);
|
||||||
py_bindmethod(type, "get_bounding_rect", array2d_like_get_bounding_rect);
|
py_bindmethod(type, "get_bounding_rect", array2d_like_get_bounding_rect);
|
||||||
py_bindmethod(type, "count_neighbors", array2d_like_count_neighbors);
|
py_bindmethod(type, "count_neighbors", array2d_like_count_neighbors);
|
||||||
|
py_bindmethod(type, "convolve", array2d_like_convolve);
|
||||||
|
|
||||||
|
const char* scc =
|
||||||
|
"\ndef get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:\n from collections import deque\n from linalg import vec2i\n\n DIRS = [vec2i.LEFT, vec2i.RIGHT, vec2i.UP, vec2i.DOWN]\n assert neighborhood in ['Moore', 'von Neumann']\n\n if neighborhood == 'Moore':\n DIRS.extend([\n vec2i.LEFT+vec2i.UP,\n vec2i.RIGHT+vec2i.UP,\n vec2i.LEFT+vec2i.DOWN,\n vec2i.RIGHT+vec2i.DOWN\n ])\n\n visited = array2d[int](self.width, self.height, default=0)\n queue = deque()\n count = 0\n for y in range(self.height):\n for x in range(self.width):\n if visited[x, y] or self[x, y] != value:\n continue\n count += 1\n queue.append((x, y))\n visited[x, y] = count\n while queue:\n cx, cy = queue.popleft()\n for dx, dy in DIRS:\n nx, ny = cx+dx, cy+dy\n if self.is_valid(nx, ny) and not visited[nx, ny] and self[nx, ny] == value:\n queue.append((nx, ny))\n visited[nx, ny] = count\n return visited, count\n\narray2d_like.get_connected_components = get_connected_components\ndel get_connected_components\n";
|
||||||
|
if(!py_exec(scc, "array2d.py", EXEC_MODE, mod)) {
|
||||||
|
py_printexc();
|
||||||
|
c11__abort("failed to execute array2d.py");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool array2d_like_iterator__next__(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(1);
|
||||||
|
c11_array2d_like_iterator* self = py_touserdata(argv);
|
||||||
|
if(self->j >= self->array->n_rows) return StopIteration();
|
||||||
|
py_newtuple(py_retval(), 2);
|
||||||
|
py_TValue* data = py_tuple_data(py_retval());
|
||||||
|
py_newvec2i(&data[0],
|
||||||
|
(c11_vec2i){
|
||||||
|
{self->i, self->j}
|
||||||
|
});
|
||||||
|
py_assign(&data[1], self->array->f_get(self->array, self->i, self->j));
|
||||||
|
self->i++;
|
||||||
|
if(self->i >= self->array->n_cols) {
|
||||||
|
self->i = 0;
|
||||||
|
self->j++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void register_array2d_like_iterator(py_Ref mod) {
|
||||||
|
py_Type type = py_newtype("array2d_like_iterator", tp_object, mod, NULL);
|
||||||
|
assert(type == tp_array2d_like_iterator);
|
||||||
|
py_bindmagic(type, __iter__, pk_wrapper__self);
|
||||||
|
py_bindmagic(type, __next__, array2d_like_iterator__next__);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool array2d__new__(int argc, py_Ref argv) {
|
static bool array2d__new__(int argc, py_Ref argv) {
|
||||||
@ -629,36 +767,6 @@ static bool array2d__new__(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool _array2d_check_all_type(c11_array2d* self, py_Type type) {
|
|
||||||
for(int i = 0; i < self->numel; i++) {
|
|
||||||
py_Type item_type = self->data[i].type;
|
|
||||||
if(item_type != type) {
|
|
||||||
const char* fmt = "expected array2d[%t], got %t";
|
|
||||||
return TypeError(fmt, type, item_type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool array2d_like_iterator__next__(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(1);
|
|
||||||
c11_array2d_like_iterator* self = py_touserdata(argv);
|
|
||||||
if(self->j >= self->array->n_rows) return StopIteration();
|
|
||||||
py_newtuple(py_retval(), 2);
|
|
||||||
py_TValue* data = py_tuple_data(py_retval());
|
|
||||||
py_newvec2i(&data[0],
|
|
||||||
(c11_vec2i){
|
|
||||||
{self->i, self->j}
|
|
||||||
});
|
|
||||||
py_assign(&data[1], self->array->f_get(self->array, self->i, self->j));
|
|
||||||
self->i++;
|
|
||||||
if(self->i >= self->array->n_cols) {
|
|
||||||
self->i = 0;
|
|
||||||
self->j++;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// fromlist(data: list[list[T]]) -> array2d[T]
|
// fromlist(data: list[list[T]]) -> array2d[T]
|
||||||
static bool array2d_fromlist_STATIC(int argc, py_Ref argv) {
|
static bool array2d_fromlist_STATIC(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
@ -687,131 +795,27 @@ static bool array2d_fromlist_STATIC(int argc, py_Ref argv) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// tolist(self) -> list[list[T]]
|
static void register_array2d(py_Ref mod) {
|
||||||
static bool array2d_tolist(int argc, py_Ref argv) {
|
py_Type type = py_newtype("array2d", tp_array2d_like, mod, NULL);
|
||||||
PY_CHECK_ARGC(1);
|
assert(type == tp_array2d);
|
||||||
c11_array2d* self = py_touserdata(argv);
|
py_bind(py_tpobject(type),
|
||||||
py_newlistn(py_retval(), self->n_rows);
|
|
||||||
for(int j = 0; j < self->n_rows; j++) {
|
|
||||||
py_Ref row_j = py_list_getitem(py_retval(), j);
|
|
||||||
py_newlistn(row_j, self->n_cols);
|
|
||||||
for(int i = 0; i < self->n_cols; i++) {
|
|
||||||
py_list_setitem(row_j, i, c11_array2d__get(self, i, j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// convolve(self: array2d[int], kernel: array2d[int], padding: int) -> array2d[int]
|
|
||||||
static bool array2d_convolve(int argc, py_Ref argv) {
|
|
||||||
PY_CHECK_ARGC(3);
|
|
||||||
PY_CHECK_ARG_TYPE(1, tp_array2d);
|
|
||||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
|
||||||
c11_array2d_like* self = py_touserdata(argv);
|
|
||||||
c11_array2d_like* kernel = py_touserdata(py_arg(1));
|
|
||||||
int padding = py_toint(py_arg(2));
|
|
||||||
if(kernel->n_cols != kernel->n_rows) { return ValueError("kernel must be square"); }
|
|
||||||
int ksize = kernel->n_cols;
|
|
||||||
if(ksize % 2 == 0) return ValueError("kernel size must be odd");
|
|
||||||
int ksize_half = ksize / 2;
|
|
||||||
if(!_array2d_check_all_type(self, tp_int)) return false;
|
|
||||||
if(!_array2d_check_all_type(kernel, tp_int)) return false;
|
|
||||||
c11_array2d* res = py_newarray2d(py_pushtmp(), self->n_cols, self->n_rows);
|
|
||||||
for(int j = 0; j < self->n_rows; j++) {
|
|
||||||
for(int i = 0; i < self->n_cols; i++) {
|
|
||||||
py_i64 sum = 0;
|
|
||||||
for(int jj = 0; jj < ksize; jj++) {
|
|
||||||
for(int ii = 0; ii < ksize; ii++) {
|
|
||||||
int x = i + ii - ksize_half;
|
|
||||||
int y = j + jj - ksize_half;
|
|
||||||
py_i64 _0, _1;
|
|
||||||
if(x < 0 || x >= self->n_cols || y < 0 || y >= self->n_rows) {
|
|
||||||
_0 = padding;
|
|
||||||
} else {
|
|
||||||
_0 = py_toint(c11_array2d__get(self, x, y));
|
|
||||||
}
|
|
||||||
_1 = py_toint(c11_array2d__get(kernel, ii, jj));
|
|
||||||
sum += _0 * _1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
py_newint(c11_array2d__get(res, i, j), sum);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
py_assign(py_retval(), py_peek(-1));
|
|
||||||
py_pop();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void pk__add_module_array2d() {
|
|
||||||
py_GlobalRef mod = py_newmodule("array2d");
|
|
||||||
|
|
||||||
py_Type array2d_like = pk_newtype("array2d_like", tp_object, mod, NULL, false, true);
|
|
||||||
py_Type array2d_like_iterator =
|
|
||||||
pk_newtype("array2d_like_iterator", tp_object, mod, NULL, false, true);
|
|
||||||
assert(array2d_like == tp_array2d_like);
|
|
||||||
assert(array2d_like_iterator == tp_array2d_like_iterator);
|
|
||||||
py_setdict(mod, py_name("array2d_like"), py_tpobject(array2d_like));
|
|
||||||
py_setdict(mod, py_name("array2d_like_iterator"), py_tpobject(array2d_like_iterator));
|
|
||||||
|
|
||||||
py_Type array2d = py_newtype("array2d", tp_array2d_like, mod, NULL);
|
|
||||||
py_Type chunked_array2d = py_newtype("chunked_array2d", tp_array2d_like, mod, NULL);
|
|
||||||
|
|
||||||
py_setdict(py_tpobject(array2d_like), __hash__, py_None());
|
|
||||||
py_setdict(py_tpobject(array2d_like_iterator), __hash__, py_None());
|
|
||||||
py_setdict(py_tpobject(array2d), __hash__, py_None());
|
|
||||||
py_setdict(py_tpobject(chunked_array2d), __hash__, py_None());
|
|
||||||
|
|
||||||
py_bindmagic(array2d_like_iterator, __iter__, pk_wrapper__self);
|
|
||||||
py_bindmagic(array2d_like_iterator, __next__, array2d_like_iterator__next__);
|
|
||||||
|
|
||||||
py_bind(py_tpobject(array2d),
|
|
||||||
"__new__(cls, n_cols: int, n_rows: int, default=None)",
|
"__new__(cls, n_cols: int, n_rows: int, default=None)",
|
||||||
array2d__new__);
|
array2d__new__);
|
||||||
|
py_bindstaticmethod(type, "fromlist", array2d_fromlist_STATIC);
|
||||||
py_bindmagic(array2d, __eq__, array2d__eq__);
|
|
||||||
py_bindmagic(array2d, __ne__, array2d__ne__);
|
|
||||||
py_bindmagic(array2d, __repr__, array2d__repr__);
|
|
||||||
py_bindmagic(array2d, __iter__, array2d__iter__);
|
|
||||||
|
|
||||||
py_bindmagic(array2d, __getitem__, array2d__getitem__);
|
|
||||||
py_bindmagic(array2d, __setitem__, array2d__setitem__);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "is_valid", array2d_is_valid);
|
|
||||||
py_bindmethod(array2d, "get", array2d_get);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "map", array2d_map);
|
|
||||||
py_bindmethod(array2d, "copy", array2d_copy);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "fill_", array2d_fill_);
|
|
||||||
py_bindmethod(array2d, "apply_", array2d_apply_);
|
|
||||||
py_bindmethod(array2d, "copy_", array2d_copy_);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "render", array2d_render);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "all", array2d_all);
|
|
||||||
py_bindmethod(array2d, "any", array2d_any);
|
|
||||||
|
|
||||||
py_bindstaticmethod(array2d, "fromlist", array2d_fromlist_STATIC);
|
|
||||||
py_bindmethod(array2d, "tolist", array2d_tolist);
|
|
||||||
|
|
||||||
py_bindmethod(array2d, "count", array2d_count);
|
|
||||||
py_bindmethod(array2d, "get_bounding_rect", array2d_get_bounding_rect);
|
|
||||||
py_bindmethod(array2d, "count_neighbors", array2d_count_neighbors);
|
|
||||||
py_bindmethod(array2d, "convolve", array2d_convolve);
|
|
||||||
|
|
||||||
const char* scc =
|
|
||||||
"\ndef get_connected_components(self, value: T, neighborhood: Neighborhood) -> tuple[array2d[int], int]:\n from collections import deque\n from linalg import vec2i\n\n DIRS = [vec2i.LEFT, vec2i.RIGHT, vec2i.UP, vec2i.DOWN]\n assert neighborhood in ['Moore', 'von Neumann']\n\n if neighborhood == 'Moore':\n DIRS.extend([\n vec2i.LEFT+vec2i.UP,\n vec2i.RIGHT+vec2i.UP,\n vec2i.LEFT+vec2i.DOWN,\n vec2i.RIGHT+vec2i.DOWN\n ])\n\n visited = array2d[int](self.width, self.height, default=0)\n queue = deque()\n count = 0\n for y in range(self.height):\n for x in range(self.width):\n if visited[x, y] or self[x, y] != value:\n continue\n count += 1\n queue.append((x, y))\n visited[x, y] = count\n while queue:\n cx, cy = queue.popleft()\n for dx, dy in DIRS:\n nx, ny = cx+dx, cy+dy\n if self.is_valid(nx, ny) and not visited[nx, ny] and self[nx, ny] == value:\n queue.append((nx, ny))\n visited[nx, ny] = count\n return visited, count\n\narray2d.get_connected_components = get_connected_components\ndel get_connected_components\n";
|
|
||||||
|
|
||||||
if(!py_exec(scc, "array2d.py", EXEC_MODE, mod)) {
|
|
||||||
py_printexc();
|
|
||||||
c11__abort("failed to execute array2d.py");
|
|
||||||
}
|
|
||||||
|
|
||||||
pk__register_chunked_array2d(mod);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef INC_COUNT
|
static bool array2d_view_origin(int argc, py_Ref argv) {
|
||||||
#undef HANDLE_SLICE
|
PY_CHECK_ARGC(1);
|
||||||
|
c11_array2d_view* self = py_touserdata(argv);
|
||||||
|
py_newvec2i(py_retval(), self->origin);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void register_array2d_view(py_Ref mod) {
|
||||||
|
py_Type type = py_newtype("array2d_view", tp_array2d_like, mod, NULL);
|
||||||
|
assert(type == tp_array2d_view);
|
||||||
|
py_bindproperty(type, "origin", array2d_view_origin, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
/* chunked_array2d */
|
/* chunked_array2d */
|
||||||
#define SMALLMAP_T__SOURCE
|
#define SMALLMAP_T__SOURCE
|
||||||
@ -845,11 +849,11 @@ static py_TValue* c11_chunked_array2d__new_chunk(c11_chunked_array2d* self, c11_
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_chunked_array2d__world_to_chunk(c11_chunked_array2d* self,
|
static void c11_chunked_array2d__world_to_chunk(c11_chunked_array2d* self,
|
||||||
int col,
|
int col,
|
||||||
int row,
|
int row,
|
||||||
c11_vec2i* chunk_pos,
|
c11_vec2i* chunk_pos,
|
||||||
c11_vec2i* local_pos) {
|
c11_vec2i* local_pos) {
|
||||||
if(col >= 0) {
|
if(col >= 0) {
|
||||||
chunk_pos->x = col >> self->chunk_size_log2;
|
chunk_pos->x = col >> self->chunk_size_log2;
|
||||||
local_pos->x = col & self->chunk_size_mask;
|
local_pos->x = col & self->chunk_size_mask;
|
||||||
@ -882,7 +886,33 @@ static py_TValue* c11_chunked_array2d__parse_col_row(c11_chunked_array2d* self,
|
|||||||
self->last_visited.key = *chunk_pos;
|
self->last_visited.key = *chunk_pos;
|
||||||
self->last_visited.value = data;
|
self->last_visited.value = data;
|
||||||
}
|
}
|
||||||
return data + 1; // skip context
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
py_Ref c11_chunked_array2d__get(c11_chunked_array2d* self, int col, int row) {
|
||||||
|
c11_vec2i chunk_pos, local_pos;
|
||||||
|
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
||||||
|
if(data == NULL) return &self->default_T;
|
||||||
|
py_Ref retval = &data[1 + local_pos.y * self->chunk_size + local_pos.x];
|
||||||
|
if(py_isnil(retval)) return &self->default_T;
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool c11_chunked_array2d__set(c11_chunked_array2d* self, int col, int row, py_Ref value) {
|
||||||
|
c11_vec2i chunk_pos, local_pos;
|
||||||
|
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
||||||
|
if(data == NULL) {
|
||||||
|
data = c11_chunked_array2d__new_chunk(self, chunk_pos);
|
||||||
|
if(data == NULL) return false;
|
||||||
|
}
|
||||||
|
data[1 + local_pos.y * self->chunk_size + local_pos.x] = *value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void c11_chunked_array2d__del(c11_chunked_array2d* self, int col, int row) {
|
||||||
|
c11_vec2i chunk_pos, local_pos;
|
||||||
|
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
||||||
|
if(data != NULL) data[1 + local_pos.y * self->chunk_size + local_pos.x] = *py_NIL();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool chunked_array2d__new__(int argc, py_Ref argv) {
|
static bool chunked_array2d__new__(int argc, py_Ref argv) {
|
||||||
@ -933,11 +963,7 @@ static bool chunked_array2d__getitem__(int argc, py_Ref argv) {
|
|||||||
c11_chunked_array2d* self = py_touserdata(argv);
|
c11_chunked_array2d* self = py_touserdata(argv);
|
||||||
c11_vec2i pos = py_tovec2i(&argv[1]);
|
c11_vec2i pos = py_tovec2i(&argv[1]);
|
||||||
py_Ref res = c11_chunked_array2d__get(self, pos.x, pos.y);
|
py_Ref res = c11_chunked_array2d__get(self, pos.x, pos.y);
|
||||||
if(res != NULL) {
|
py_assign(py_retval(), res);
|
||||||
py_assign(py_retval(), res);
|
|
||||||
} else {
|
|
||||||
py_assign(py_retval(), &self->default_T);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1035,32 +1061,6 @@ void c11_chunked_array2d__dtor(c11_chunked_array2d* self) {
|
|||||||
c11_chunked_array2d_chunks__dtor(&self->chunks);
|
c11_chunked_array2d_chunks__dtor(&self->chunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
py_Ref c11_chunked_array2d__get(c11_chunked_array2d* self, int col, int row) {
|
|
||||||
c11_vec2i chunk_pos, local_pos;
|
|
||||||
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
|
||||||
if(data == NULL) return NULL;
|
|
||||||
py_Ref retval = &data[local_pos.y * self->chunk_size + local_pos.x];
|
|
||||||
if(py_isnil(retval)) return NULL;
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool c11_chunked_array2d__set(c11_chunked_array2d* self, int col, int row, py_Ref value) {
|
|
||||||
c11_vec2i chunk_pos, local_pos;
|
|
||||||
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
|
||||||
if(data == NULL) {
|
|
||||||
data = c11_chunked_array2d__new_chunk(self, chunk_pos);
|
|
||||||
if(data == NULL) return false;
|
|
||||||
}
|
|
||||||
data[local_pos.y * self->chunk_size + local_pos.x] = *value;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void c11_chunked_array2d__del(c11_chunked_array2d* self, int col, int row) {
|
|
||||||
c11_vec2i chunk_pos, local_pos;
|
|
||||||
py_TValue* data = c11_chunked_array2d__parse_col_row(self, col, row, &chunk_pos, &local_pos);
|
|
||||||
if(data != NULL) data[local_pos.y * self->chunk_size + local_pos.x] = *py_NIL();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void c11_chunked_array2d__mark(void* ud) {
|
static void c11_chunked_array2d__mark(void* ud) {
|
||||||
c11_chunked_array2d* self = ud;
|
c11_chunked_array2d* self = ud;
|
||||||
pk__mark_value(&self->default_T);
|
pk__mark_value(&self->default_T);
|
||||||
@ -1074,25 +1074,106 @@ static void c11_chunked_array2d__mark(void* ud) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void pk__register_chunked_array2d(py_Ref mod) {
|
static bool chunked_array2d_view(int argc, py_Ref argv) {
|
||||||
py_Type cls = py_newtype("chunked_array2d", tp_object, mod, (py_Dtor)c11_chunked_array2d__dtor);
|
PY_CHECK_ARGC(1);
|
||||||
pk__tp_set_marker(cls, c11_chunked_array2d__mark);
|
c11_chunked_array2d* self = py_touserdata(&argv[0]);
|
||||||
|
if(self->chunks.length == 0) { return ValueError("chunked_array2d is empty"); }
|
||||||
|
int min_chunk_x = INT_MAX;
|
||||||
|
int min_chunk_y = INT_MAX;
|
||||||
|
int max_chunk_x = INT_MIN;
|
||||||
|
int max_chunk_y = INT_MIN;
|
||||||
|
for(int i = 0; i < self->chunks.length; i++) {
|
||||||
|
c11_vec2i chunk_pos = c11__getitem(c11_chunked_array2d_chunks_KV, &self->chunks, i).key;
|
||||||
|
min_chunk_x = c11__min(min_chunk_x, chunk_pos.x);
|
||||||
|
min_chunk_y = c11__min(min_chunk_y, chunk_pos.y);
|
||||||
|
max_chunk_x = c11__max(max_chunk_x, chunk_pos.x);
|
||||||
|
max_chunk_y = c11__max(max_chunk_y, chunk_pos.y);
|
||||||
|
}
|
||||||
|
int start_col = min_chunk_x << self->chunk_size_log2;
|
||||||
|
int start_row = min_chunk_y << self->chunk_size_log2;
|
||||||
|
int width = (max_chunk_x - min_chunk_x + 1) * self->chunk_size;
|
||||||
|
int height = (max_chunk_y - min_chunk_y + 1) * self->chunk_size;
|
||||||
|
return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
py_bindmagic(cls, __new__, chunked_array2d__new__);
|
static bool chunked_array2d_view_rect(int argc, py_Ref argv) {
|
||||||
py_bind(py_tpobject(cls),
|
PY_CHECK_ARGC(4);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_vec2i);
|
||||||
|
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||||
|
PY_CHECK_ARG_TYPE(3, tp_int);
|
||||||
|
c11_chunked_array2d* self = py_touserdata(&argv[0]);
|
||||||
|
c11_vec2i pos = py_tovec2i(&argv[1]);
|
||||||
|
int width = py_toint(&argv[2]);
|
||||||
|
int height = py_toint(&argv[3]);
|
||||||
|
return _chunked_array2d_view(py_retval(), argv, self, pos.x, pos.y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool chunked_array2d_view_chunk(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(2);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_vec2i);
|
||||||
|
c11_chunked_array2d* self = py_touserdata(&argv[0]);
|
||||||
|
c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
|
||||||
|
int start_col = chunk_pos.x << self->chunk_size_log2;
|
||||||
|
int start_row = chunk_pos.y << self->chunk_size_log2;
|
||||||
|
return _chunked_array2d_view(py_retval(),
|
||||||
|
argv,
|
||||||
|
self,
|
||||||
|
start_col,
|
||||||
|
start_row,
|
||||||
|
self->chunk_size,
|
||||||
|
self->chunk_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool chunked_array2d_view_chunks(int argc, py_Ref argv) {
|
||||||
|
PY_CHECK_ARGC(4);
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_vec2i);
|
||||||
|
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||||
|
PY_CHECK_ARG_TYPE(3, tp_int);
|
||||||
|
c11_chunked_array2d* self = py_touserdata(&argv[0]);
|
||||||
|
c11_vec2i chunk_pos = py_tovec2i(&argv[1]);
|
||||||
|
int width = py_toint(&argv[2]) * self->chunk_size;
|
||||||
|
int height = py_toint(&argv[3]) * self->chunk_size;
|
||||||
|
int start_col = chunk_pos.x << self->chunk_size_log2;
|
||||||
|
int start_row = chunk_pos.y << self->chunk_size_log2;
|
||||||
|
return _chunked_array2d_view(py_retval(), argv, self, start_col, start_row, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void register_chunked_array2d(py_Ref mod) {
|
||||||
|
py_Type type =
|
||||||
|
py_newtype("chunked_array2d", tp_object, mod, (py_Dtor)c11_chunked_array2d__dtor);
|
||||||
|
pk__tp_set_marker(type, c11_chunked_array2d__mark);
|
||||||
|
assert(type == tp_chunked_array2d);
|
||||||
|
|
||||||
|
py_bind(py_tpobject(type), "__new__(cls, *args, **kwargs)", chunked_array2d__new__);
|
||||||
|
py_bind(py_tpobject(type),
|
||||||
"__init__(self, chunk_size, default=None, context_builder=None)",
|
"__init__(self, chunk_size, default=None, context_builder=None)",
|
||||||
chunked_array2d__init__);
|
chunked_array2d__init__);
|
||||||
|
|
||||||
py_bindproperty(cls, "chunk_size", chunked_array2d__chunk_size, NULL);
|
py_bindproperty(type, "chunk_size", chunked_array2d__chunk_size, NULL);
|
||||||
|
|
||||||
py_bindmagic(cls, __getitem__, chunked_array2d__getitem__);
|
py_bindmagic(type, __getitem__, chunked_array2d__getitem__);
|
||||||
py_bindmagic(cls, __setitem__, chunked_array2d__setitem__);
|
py_bindmagic(type, __setitem__, chunked_array2d__setitem__);
|
||||||
py_bindmagic(cls, __delitem__, chunked_array2d__delitem__);
|
py_bindmagic(type, __delitem__, chunked_array2d__delitem__);
|
||||||
py_bindmagic(cls, __iter__, chunked_array2d__iter__);
|
py_bindmagic(type, __iter__, chunked_array2d__iter__);
|
||||||
|
|
||||||
py_bindmethod(cls, "clear", chunked_array2d__clear);
|
py_bindmethod(type, "clear", chunked_array2d__clear);
|
||||||
py_bindmethod(cls, "world_to_chunk", chunked_array2d__world_to_chunk);
|
py_bindmethod(type, "world_to_chunk", chunked_array2d__world_to_chunk);
|
||||||
py_bindmethod(cls, "add_chunk", chunked_array2d__add_chunk);
|
py_bindmethod(type, "add_chunk", chunked_array2d__add_chunk);
|
||||||
py_bindmethod(cls, "remove_chunk", chunked_array2d__remove_chunk);
|
py_bindmethod(type, "remove_chunk", chunked_array2d__remove_chunk);
|
||||||
py_bindmethod(cls, "get_context", chunked_array2d__get_context);
|
py_bindmethod(type, "get_context", chunked_array2d__get_context);
|
||||||
|
|
||||||
|
py_bindmethod(type, "view", chunked_array2d_view);
|
||||||
|
py_bindmethod(type, "view_rect", chunked_array2d_view_rect);
|
||||||
|
py_bindmethod(type, "view_chunk", chunked_array2d_view_chunk);
|
||||||
|
py_bindmethod(type, "view_chunks", chunked_array2d_view_chunks);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pk__add_module_array2d() {
|
||||||
|
py_GlobalRef mod = py_newmodule("array2d");
|
||||||
|
|
||||||
|
register_array2d_like(mod);
|
||||||
|
register_array2d_like_iterator(mod);
|
||||||
|
register_array2d(mod);
|
||||||
|
register_array2d_view(mod);
|
||||||
|
register_chunked_array2d(mod);
|
||||||
}
|
}
|
||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "pocketpy/common/utils.h"
|
#include "pocketpy/common/utils.h"
|
||||||
#include "pocketpy/common/sstream.h"
|
#include "pocketpy/common/sstream.h"
|
||||||
|
#include "pocketpy/interpreter/vm.h"
|
||||||
#include "pocketpy/interpreter/array2d.h"
|
#include "pocketpy/interpreter/array2d.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@ -324,16 +325,16 @@ static bool pkl__write_object(PickleObject* buf, py_TValue* obj) {
|
|||||||
return true;
|
return true;
|
||||||
else {
|
else {
|
||||||
c11_array2d* arr = py_touserdata(obj);
|
c11_array2d* arr = py_touserdata(obj);
|
||||||
for(int i = 0; i < arr->numel; i++) {
|
for(int i = 0; i < arr->header.numel; i++) {
|
||||||
if(arr->data[i].is_ptr)
|
if(arr->data[i].is_ptr)
|
||||||
return TypeError(
|
return TypeError(
|
||||||
"'array2d' object is not picklable because it contains heap-allocated objects");
|
"'array2d' object is not picklable because it contains heap-allocated objects");
|
||||||
buf->used_types[arr->data[i].type] = true;
|
buf->used_types[arr->data[i].type] = true;
|
||||||
}
|
}
|
||||||
pkl__emit_op(buf, PKL_ARRAY2D);
|
pkl__emit_op(buf, PKL_ARRAY2D);
|
||||||
pkl__emit_int(buf, arr->n_cols);
|
pkl__emit_int(buf, arr->header.n_cols);
|
||||||
pkl__emit_int(buf, arr->n_rows);
|
pkl__emit_int(buf, arr->header.n_rows);
|
||||||
PickleObject__write_bytes(buf, arr->data, arr->numel * sizeof(py_TValue));
|
PickleObject__write_bytes(buf, arr->data, arr->header.numel * sizeof(py_TValue));
|
||||||
}
|
}
|
||||||
pkl__store_memo(buf, obj->_obj);
|
pkl__store_memo(buf, obj->_obj);
|
||||||
return true;
|
return true;
|
||||||
@ -651,9 +652,9 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
int n_cols = pkl__read_int(&p);
|
int n_cols = pkl__read_int(&p);
|
||||||
int n_rows = pkl__read_int(&p);
|
int n_rows = pkl__read_int(&p);
|
||||||
c11_array2d* arr = py_newarray2d(py_pushtmp(), n_cols, n_rows);
|
c11_array2d* arr = py_newarray2d(py_pushtmp(), n_cols, n_rows);
|
||||||
int total_size = arr->numel * sizeof(py_TValue);
|
int total_size = arr->header.numel * sizeof(py_TValue);
|
||||||
memcpy(arr->data, p, total_size);
|
memcpy(arr->data, p, total_size);
|
||||||
for(int i = 0; i < arr->numel; i++) {
|
for(int i = 0; i < arr->header.numel; i++) {
|
||||||
arr->data[i].type = pkl__fix_type(arr->data[i].type, type_mapping);
|
arr->data[i].type = pkl__fix_type(arr->data[i].type, type_mapping);
|
||||||
}
|
}
|
||||||
p += total_size;
|
p += total_size;
|
||||||
|
|||||||
@ -61,6 +61,11 @@ bool py_checktype(py_Ref self, py_Type type) {
|
|||||||
return TypeError("expected '%t', got '%t'", type, self->type);
|
return TypeError("expected '%t', got '%t'", type, self->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool py_checkinstance(py_Ref self, py_Type type) {
|
||||||
|
if(py_isinstance(self, type)) return true;
|
||||||
|
return TypeError("expected '%t' or its subclass, got '%t'", type, self->type);
|
||||||
|
}
|
||||||
|
|
||||||
bool py_isinstance(py_Ref obj, py_Type type) { return py_issubclass(obj->type, type); }
|
bool py_isinstance(py_Ref obj, py_Type type) { return py_issubclass(obj->type, type); }
|
||||||
|
|
||||||
bool py_issubclass(py_Type derived, py_Type base) {
|
bool py_issubclass(py_Type derived, py_Type base) {
|
||||||
|
|||||||
@ -1,10 +1,13 @@
|
|||||||
from array2d import array2d
|
from array2d import array2d
|
||||||
from linalg import vec2i
|
from linalg import vec2i
|
||||||
|
|
||||||
|
def exit_on_error():
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
|
||||||
# test error args for __init__
|
# test error args for __init__
|
||||||
try:
|
try:
|
||||||
a = array2d(0, 0)
|
a = array2d(0, 0)
|
||||||
exit(0)
|
exit_on_error()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -39,7 +42,7 @@ assert a[0, 0] == (0, 0)
|
|||||||
assert a[1, 3] == (1, 3)
|
assert a[1, 3] == (1, 3)
|
||||||
try:
|
try:
|
||||||
a[2, 0]
|
a[2, 0]
|
||||||
exit(1)
|
exit_on_error()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -51,7 +54,7 @@ a[1, 3] = 6
|
|||||||
assert a[1, 3] == 6
|
assert a[1, 3] == 6
|
||||||
try:
|
try:
|
||||||
a[0, -1] = 7
|
a[0, -1] = 7
|
||||||
exit(1)
|
exit_on_error()
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -83,21 +86,19 @@ d = c.copy()
|
|||||||
assert (d == c).all() and d is not c
|
assert (d == c).all() and d is not c
|
||||||
|
|
||||||
# test fill_
|
# test fill_
|
||||||
d.fill_(-3)
|
d[:, :] = -3 # d.fill_(-3)
|
||||||
assert (d == array2d(2, 4, default=-3)).all()
|
assert (d == array2d(2, 4, default=-3)).all()
|
||||||
|
|
||||||
# test apply_
|
# test apply
|
||||||
d.apply_(lambda x: x + 3)
|
d.apply(lambda x: x + 3)
|
||||||
assert (d == array2d(2, 4, default=0)).all()
|
assert (d == array2d(2, 4, default=0)).all()
|
||||||
|
|
||||||
# test copy_
|
# test copy_
|
||||||
a.copy_(d)
|
a[:, :] = d
|
||||||
assert (a == d).all() and a is not d
|
assert (a == d).all() and a is not d
|
||||||
x = array2d(2, 4, default=0)
|
x = array2d(2, 4, default=0)
|
||||||
x.copy_(d)
|
x[:, :] = d
|
||||||
assert (x == d).all() and x is not d
|
assert (x == d).all() and x is not d
|
||||||
x.copy_([1, 2, 3, 4, 5, 6, 7, 8])
|
|
||||||
assert x.tolist() == [[1, 2], [3, 4], [5, 6], [7, 8]]
|
|
||||||
|
|
||||||
# test alive_neighbors
|
# test alive_neighbors
|
||||||
a = array2d[int](3, 3, default=0)
|
a = array2d[int](3, 3, default=0)
|
||||||
@ -148,7 +149,7 @@ assert a.get_bounding_rect(0) == (0, 0, 5, 5)
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
a.get_bounding_rect(2)
|
a.get_bounding_rect(2)
|
||||||
exit(1)
|
exit_on_error()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -165,16 +166,10 @@ assert a == array2d(3, 2, default=3)
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
a[:, :] = array2d(1, 1)
|
a[:, :] = array2d(1, 1)
|
||||||
exit(1)
|
exit_on_error()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
|
||||||
a[:, :] = ...
|
|
||||||
exit(1)
|
|
||||||
except TypeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# test __iter__
|
# test __iter__
|
||||||
a = array2d(3, 4, default=1)
|
a = array2d(3, 4, default=1)
|
||||||
for xy, val in a:
|
for xy, val in a:
|
||||||
|
|||||||
8
tests/90_chunked_array2d.py
Normal file
8
tests/90_chunked_array2d.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from array2d import chunked_array2d
|
||||||
|
from linalg import vec2i
|
||||||
|
|
||||||
|
a = chunked_array2d(4, default=0)
|
||||||
|
|
||||||
|
a[vec2i.ONE] = 1
|
||||||
|
|
||||||
|
print(a.view().render())
|
||||||
Loading…
x
Reference in New Issue
Block a user