From ee522d965ddf7b826b028941922d21ac1f14cf3b Mon Sep 17 00:00:00 2001 From: AlbertoEAF Date: Sat, 7 Nov 2020 18:57:03 +0000 Subject: [PATCH 1/3] [refactor] SWIG - Split pointer manipulation to individual .i file --- swig/lightgbmlib.i | 98 +-------------------------------- swig/pointer_manipulation.i | 107 ++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 97 deletions(-) create mode 100644 swig/pointer_manipulation.i diff --git a/swig/lightgbmlib.i b/swig/lightgbmlib.i index 0a27427d0969..057d5c5b3a3f 100644 --- a/swig/lightgbmlib.i +++ b/swig/lightgbmlib.i @@ -279,102 +279,6 @@ } %} -%pointer_functions(int, intp) -%pointer_functions(long, longp) -%pointer_functions(double, doublep) -%pointer_functions(float, floatp) -%pointer_functions(int64_t, int64_tp) -%pointer_functions(int32_t, int32_tp) - -%pointer_cast(int64_t *, long *, int64_t_to_long_ptr) -%pointer_cast(int64_t *, double *, int64_t_to_double_ptr) -%pointer_cast(int32_t *, int *, int32_t_to_int_ptr) -%pointer_cast(long *, int64_t *, long_to_int64_t_ptr) -%pointer_cast(double *, int64_t *, double_to_int64_t_ptr) -%pointer_cast(int *, int32_t *, int_to_int32_t_ptr) - -%pointer_cast(double *, void *, double_to_voidp_ptr) -%pointer_cast(float *, void *, float_to_voidp_ptr) -%pointer_cast(int *, void *, int_to_voidp_ptr) -%pointer_cast(int32_t *, void *, int32_t_to_voidp_ptr) -%pointer_cast(int64_t *, void *, int64_t_to_voidp_ptr) - -/* Custom pointer manipulation template */ -%define %pointer_manipulation(TYPE, NAME) -%{ - static TYPE *new_##NAME() { %} - %{ TYPE* NAME = new TYPE; return NAME; %} - %{} - - static void delete_##NAME(TYPE *self) { %} - %{ if (self) delete self; %} - %{} - %} - -TYPE *new_##NAME(); -void delete_##NAME(TYPE *self); - -%enddef - -%define %pointer_dereference(TYPE, NAME) -%{ - static TYPE NAME ##_value(TYPE *self) { - TYPE NAME = *self; - return NAME; - } -%} - -TYPE NAME##_value(TYPE *self); - -%enddef - -%define %pointer_handle(TYPE, NAME) -%{ - static TYPE* NAME ##_handle() { %} - %{ TYPE* NAME = new TYPE; *NAME = (TYPE)operator new(sizeof(int*)); return NAME; %} - %{} -%} - -TYPE *NAME##_handle(); - -%enddef - -%define %long_array_functions(TYPE,NAME) -%{ - static TYPE *new_##NAME(int64_t nelements) { %} - %{ return new TYPE[nelements](); %} - %{} - - static void delete_##NAME(TYPE *ary) { %} - %{ delete [] ary; %} - %{} - - static TYPE NAME##_getitem(TYPE *ary, int64_t index) { - return ary[index]; - } - static void NAME##_setitem(TYPE *ary, int64_t index, TYPE value) { - ary[index] = value; - } - %} - -TYPE *new_##NAME(int64_t nelements); -void delete_##NAME(TYPE *ary); -TYPE NAME##_getitem(TYPE *ary, int64_t index); -void NAME##_setitem(TYPE *ary, int64_t index, TYPE value); - -%enddef - -%long_array_functions(double, doubleArray) -%long_array_functions(float, floatArray) -%long_array_functions(int, intArray) -%long_array_functions(long, longArray) - -%pointer_manipulation(void*, voidpp) - -/* Allow dereferencing of void** to void* */ -%pointer_dereference(void*, voidpp) - -/* Allow retrieving handle to void** */ -%pointer_handle(void*, voidpp) +%include "pointer_manipulation.i" %include "StringArray_API_extensions.i" diff --git a/swig/pointer_manipulation.i b/swig/pointer_manipulation.i new file mode 100644 index 000000000000..3bc81aecbfad --- /dev/null +++ b/swig/pointer_manipulation.i @@ -0,0 +1,107 @@ +/*! + * Copyright (c) 2018 Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See LICENSE file in the project root for license information. + */ +/** + * This SWIG interface extension provides support to + * the pointer manipulation methods present in the standard + * SWIG wrappers, but with support for int64 pointers. + */ + +%pointer_functions(int, intp) +%pointer_functions(long, longp) +%pointer_functions(double, doublep) +%pointer_functions(float, floatp) +%pointer_functions(int64_t, int64_tp) +%pointer_functions(int32_t, int32_tp) + +%pointer_cast(int64_t *, long *, int64_t_to_long_ptr) +%pointer_cast(int64_t *, double *, int64_t_to_double_ptr) +%pointer_cast(int32_t *, int *, int32_t_to_int_ptr) +%pointer_cast(long *, int64_t *, long_to_int64_t_ptr) +%pointer_cast(double *, int64_t *, double_to_int64_t_ptr) +%pointer_cast(int *, int32_t *, int_to_int32_t_ptr) + +%pointer_cast(double *, void *, double_to_voidp_ptr) +%pointer_cast(float *, void *, float_to_voidp_ptr) +%pointer_cast(int *, void *, int_to_voidp_ptr) +%pointer_cast(int32_t *, void *, int32_t_to_voidp_ptr) +%pointer_cast(int64_t *, void *, int64_t_to_voidp_ptr) + +/* Custom pointer manipulation template */ +%define %pointer_manipulation(TYPE, NAME) +%{ + static TYPE *new_##NAME() { %} + %{ TYPE* NAME = new TYPE; return NAME; %} + %{} + + static void delete_##NAME(TYPE *self) { %} + %{ if (self) delete self; %} + %{} + %} + +TYPE *new_##NAME(); +void delete_##NAME(TYPE *self); + +%enddef + +%define %pointer_dereference(TYPE, NAME) +%{ + static TYPE NAME ##_value(TYPE *self) { + TYPE NAME = *self; + return NAME; + } +%} + +TYPE NAME##_value(TYPE *self); + +%enddef + +%define %pointer_handle(TYPE, NAME) +%{ + static TYPE* NAME ##_handle() { %} + %{ TYPE* NAME = new TYPE; *NAME = (TYPE)operator new(sizeof(int*)); return NAME; %} + %{} +%} + +TYPE *NAME##_handle(); + +%enddef + +%define %long_array_functions(TYPE,NAME) +%{ + static TYPE *new_##NAME(int64_t nelements) { %} + %{ return new TYPE[nelements](); %} + %{} + + static void delete_##NAME(TYPE *ary) { %} + %{ delete [] ary; %} + %{} + + static TYPE NAME##_getitem(TYPE *ary, int64_t index) { + return ary[index]; + } + static void NAME##_setitem(TYPE *ary, int64_t index, TYPE value) { + ary[index] = value; + } + %} + +TYPE *new_##NAME(int64_t nelements); +void delete_##NAME(TYPE *ary); +TYPE NAME##_getitem(TYPE *ary, int64_t index); +void NAME##_setitem(TYPE *ary, int64_t index, TYPE value); + +%enddef + +%long_array_functions(double, doubleArray) +%long_array_functions(float, floatArray) +%long_array_functions(int, intArray) +%long_array_functions(long, longArray) + +%pointer_manipulation(void*, voidpp) + +/* Allow dereferencing of void** to void* */ +%pointer_dereference(void*, voidpp) + +/* Allow retrieving handle to void** */ +%pointer_handle(void*, voidpp) \ No newline at end of file From 973968d024d136a6e8f715a3373169b511a759c3 Mon Sep 17 00:00:00 2001 From: AlbertoEAF Date: Sat, 7 Nov 2020 19:06:20 +0000 Subject: [PATCH 2/3] Update module docstring --- swig/pointer_manipulation.i | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/swig/pointer_manipulation.i b/swig/pointer_manipulation.i index 3bc81aecbfad..81e3ee40be22 100644 --- a/swig/pointer_manipulation.i +++ b/swig/pointer_manipulation.i @@ -5,7 +5,14 @@ /** * This SWIG interface extension provides support to * the pointer manipulation methods present in the standard - * SWIG wrappers, but with support for int64 pointers. + * SWIG wrappers, but with support for larger arrays. + * + * SWIG provides this in https://github.com/swig/swig/blob/master/Lib/carrays.i + * but the standard methods only provide arrays with up to + * max(int32_t) elements. + * + * The `long_array_functions` wrappers extend this + * to arrays of size max(int64_t) instead of max(int32_t). */ %pointer_functions(int, intp) From 670eed212af48c05474b984e3883d6eaf0331b11 Mon Sep 17 00:00:00 2001 From: AlbertoEAF Date: Sat, 7 Nov 2020 19:10:51 +0000 Subject: [PATCH 3/3] space --- swig/pointer_manipulation.i | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swig/pointer_manipulation.i b/swig/pointer_manipulation.i index 81e3ee40be22..d7e7b31e3dc5 100644 --- a/swig/pointer_manipulation.i +++ b/swig/pointer_manipulation.i @@ -2,7 +2,7 @@ * Copyright (c) 2018 Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See LICENSE file in the project root for license information. */ -/** +/*! * This SWIG interface extension provides support to * the pointer manipulation methods present in the standard * SWIG wrappers, but with support for larger arrays. @@ -111,4 +111,4 @@ void NAME##_setitem(TYPE *ary, int64_t index, TYPE value); %pointer_dereference(void*, voidpp) /* Allow retrieving handle to void** */ -%pointer_handle(void*, voidpp) \ No newline at end of file +%pointer_handle(void*, voidpp)