Skip to content

Commit 465057d

Browse files
committed
Add binary search with comparator
1 parent 696daa5 commit 465057d

File tree

3 files changed

+212
-16
lines changed

3 files changed

+212
-16
lines changed

src/flc_algorithm.i

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,38 @@ static void argsort_cmp(const T *DATA, size_t DATASIZE,
8585

8686
%flc_template_numeric(argsort, argsort)
8787
%flc_template_numeric(argsort_cmp, argsort)
88+
8889
/******************************
8990
* Searching
9091
******************************/
9192

92-
%inline {
93+
%{
94+
template<class T, class Compare>
95+
static int binary_search_impl(const T *data, size_t size, T value, Compare cmp) {
96+
const T *end = data + size;
97+
auto iter = std::lower_bound(data, end, value, cmp);
98+
if (iter == end || cmp(*iter, value) || cmp(value, *iter))
99+
return 0;
100+
// Index of the found item *IN FORTAN INDEXING*
101+
return (iter - data) + 1;
102+
}
103+
%}
104+
105+
%inline %{
93106
template<class T>
94-
int binary_search(const T *DATA, size_t DATASIZE, T value) {
95-
const T *end = DATA + DATASIZE;
96-
auto iter = std::lower_bound(DATA, end, value);
97-
if (iter == end || *iter != value)
98-
return 0;
99-
// Index of the found item *IN FORTAN INDEXING
100-
return (iter - DATA) + 1;
107+
static int binary_search(const T *DATA, size_t DATASIZE, T value) {
108+
return binary_search_impl(DATA, DATASIZE, value, std::less<T>());
101109
}
110+
111+
template<class T>
112+
static int binary_search_cmp(const T *DATA, size_t DATASIZE, T value,
113+
bool (*cmp)(T, T)) {
114+
return binary_search_impl(DATA, DATASIZE, value, cmp);
102115
}
116+
%}
103117

104118
%flc_template_numeric(binary_search, binary_search)
119+
%flc_template_numeric(binary_search_cmp, binary_search)
105120

106121
/******************************
107122
* Reordering

src/generated/flc_algorithm.f90

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module flc_algorithm
3333
integer(C_INT), public :: mem = SWIG_NULL
3434
end type
3535
interface binary_search
36-
module procedure swigf_binary_search__SWIG_1, swigf_binary_search__SWIG_2, swigf_binary_search__SWIG_3
36+
module procedure swigf_binary_search__SWIG_1, swigf_binary_search__SWIG_2, swigf_binary_search__SWIG_3, &
37+
swigf_binary_search__SWIG_4, swigf_binary_search__SWIG_5, swigf_binary_search__SWIG_6
3738
end interface
3839
public :: binary_search
3940
interface shuffle
@@ -241,6 +242,39 @@ function swigc_binary_search__SWIG_3(farg1, farg3) &
241242
integer(C_INT) :: fresult
242243
end function
243244

245+
function swigc_binary_search__SWIG_4(farg1, farg3, farg4) &
246+
bind(C, name="_wrap_binary_search__SWIG_4") &
247+
result(fresult)
248+
use, intrinsic :: ISO_C_BINDING
249+
import :: swigarraywrapper
250+
type(SwigArrayWrapper) :: farg1
251+
integer(C_INT32_T), intent(in) :: farg3
252+
type(C_FUNPTR), value :: farg4
253+
integer(C_INT) :: fresult
254+
end function
255+
256+
function swigc_binary_search__SWIG_5(farg1, farg3, farg4) &
257+
bind(C, name="_wrap_binary_search__SWIG_5") &
258+
result(fresult)
259+
use, intrinsic :: ISO_C_BINDING
260+
import :: swigarraywrapper
261+
type(SwigArrayWrapper) :: farg1
262+
integer(C_INT64_T), intent(in) :: farg3
263+
type(C_FUNPTR), value :: farg4
264+
integer(C_INT) :: fresult
265+
end function
266+
267+
function swigc_binary_search__SWIG_6(farg1, farg3, farg4) &
268+
bind(C, name="_wrap_binary_search__SWIG_6") &
269+
result(fresult)
270+
use, intrinsic :: ISO_C_BINDING
271+
import :: swigarraywrapper
272+
type(SwigArrayWrapper) :: farg1
273+
real(C_DOUBLE), intent(in) :: farg3
274+
type(C_FUNPTR), value :: farg4
275+
integer(C_INT) :: fresult
276+
end function
277+
244278
subroutine swigc_shuffle__SWIG_1(farg1, farg2) &
245279
bind(C, name="_wrap_shuffle__SWIG_1")
246280
use, intrinsic :: ISO_C_BINDING
@@ -781,6 +815,87 @@ function swigf_binary_search__SWIG_3(data, value) &
781815
swig_result = fresult
782816
end function
783817

818+
function swigf_binary_search__SWIG_4(data, value, cmp) &
819+
result(swig_result)
820+
use, intrinsic :: ISO_C_BINDING
821+
integer(C_INT) :: swig_result
822+
integer(C_INT32_T), dimension(:), intent(in), target :: data
823+
integer(C_INT32_T), pointer :: farg1_view
824+
integer(C_INT32_T), intent(in) :: value
825+
type(C_FUNPTR), intent(in), value :: cmp
826+
integer(C_INT) :: fresult
827+
type(SwigArrayWrapper) :: farg1
828+
integer(C_INT32_T) :: farg3
829+
type(C_FUNPTR) :: farg4
830+
831+
if (size(data) > 0) then
832+
farg1_view => data(1)
833+
farg1%data = c_loc(farg1_view)
834+
farg1%size = size(data)
835+
else
836+
farg1%data = c_null_ptr
837+
farg1%size = 0
838+
end if
839+
farg3 = value
840+
farg4 = cmp
841+
fresult = swigc_binary_search__SWIG_4(farg1, farg3, farg4)
842+
swig_result = fresult
843+
end function
844+
845+
function swigf_binary_search__SWIG_5(data, value, cmp) &
846+
result(swig_result)
847+
use, intrinsic :: ISO_C_BINDING
848+
integer(C_INT) :: swig_result
849+
integer(C_INT64_T), dimension(:), intent(in), target :: data
850+
integer(C_INT64_T), pointer :: farg1_view
851+
integer(C_INT64_T), intent(in) :: value
852+
type(C_FUNPTR), intent(in), value :: cmp
853+
integer(C_INT) :: fresult
854+
type(SwigArrayWrapper) :: farg1
855+
integer(C_INT64_T) :: farg3
856+
type(C_FUNPTR) :: farg4
857+
858+
if (size(data) > 0) then
859+
farg1_view => data(1)
860+
farg1%data = c_loc(farg1_view)
861+
farg1%size = size(data)
862+
else
863+
farg1%data = c_null_ptr
864+
farg1%size = 0
865+
end if
866+
farg3 = value
867+
farg4 = cmp
868+
fresult = swigc_binary_search__SWIG_5(farg1, farg3, farg4)
869+
swig_result = fresult
870+
end function
871+
872+
function swigf_binary_search__SWIG_6(data, value, cmp) &
873+
result(swig_result)
874+
use, intrinsic :: ISO_C_BINDING
875+
integer(C_INT) :: swig_result
876+
real(C_DOUBLE), dimension(:), intent(in), target :: data
877+
real(C_DOUBLE), pointer :: farg1_view
878+
real(C_DOUBLE), intent(in) :: value
879+
type(C_FUNPTR), intent(in), value :: cmp
880+
integer(C_INT) :: fresult
881+
type(SwigArrayWrapper) :: farg1
882+
real(C_DOUBLE) :: farg3
883+
type(C_FUNPTR) :: farg4
884+
885+
if (size(data) > 0) then
886+
farg1_view => data(1)
887+
farg1%data = c_loc(farg1_view)
888+
farg1%size = size(data)
889+
else
890+
farg1%data = c_null_ptr
891+
farg1%size = 0
892+
end if
893+
farg3 = value
894+
farg4 = cmp
895+
fresult = swigc_binary_search__SWIG_6(farg1, farg3, farg4)
896+
swig_result = fresult
897+
end function
898+
784899
subroutine swigf_shuffle__SWIG_1(g, data)
785900
use, intrinsic :: ISO_C_BINDING
786901
class(Engine), intent(in) :: g

src/generated/flc_algorithmFORTRAN_wrap.cxx

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,26 @@ static void argsort_cmp(const T *DATA, size_t DATASIZE,
322322
}
323323

324324

325+
template<class T, class Compare>
326+
static int binary_search_impl(const T *data, size_t size, T value, Compare cmp) {
327+
const T *end = data + size;
328+
auto iter = std::lower_bound(data, end, value, cmp);
329+
if (iter == end || cmp(*iter, value) || cmp(value, *iter))
330+
return 0;
331+
// Index of the found item *IN FORTAN INDEXING*
332+
return (iter - data) + 1;
333+
}
334+
335+
325336
template<class T>
326-
int binary_search(const T *DATA, size_t DATASIZE, T value) {
327-
const T *end = DATA + DATASIZE;
328-
auto iter = std::lower_bound(DATA, end, value);
329-
if (iter == end || *iter != value)
330-
return 0;
331-
// Index of the found item *IN FORTAN INDEXING
332-
return (iter - DATA) + 1;
337+
static int binary_search(const T *DATA, size_t DATASIZE, T value) {
338+
return binary_search_impl(DATA, DATASIZE, value, std::less<T>());
339+
}
340+
341+
template<class T>
342+
static int binary_search_cmp(const T *DATA, size_t DATASIZE, T value,
343+
bool (*cmp)(T, T)) {
344+
return binary_search_impl(DATA, DATASIZE, value, cmp);
333345
}
334346

335347

@@ -661,6 +673,60 @@ SWIGEXPORT int _wrap_binary_search__SWIG_3(SwigArrayWrapper *farg1, double const
661673
}
662674

663675

676+
SWIGEXPORT int _wrap_binary_search__SWIG_4(SwigArrayWrapper *farg1, int32_t const *farg3, bool (*farg4)(int32_t,int32_t)) {
677+
int fresult ;
678+
int32_t *arg1 = (int32_t *) 0 ;
679+
size_t arg2 ;
680+
int32_t arg3 ;
681+
bool (*arg4)(int32_t,int32_t) = (bool (*)(int32_t,int32_t)) 0 ;
682+
int result;
683+
684+
arg1 = static_cast< int32_t * >(farg1->data);
685+
arg2 = farg1->size;
686+
arg3 = static_cast< int32_t >(*farg3);
687+
arg4 = reinterpret_cast< bool (*)(int32_t,int32_t) >(farg4);
688+
result = (int)binary_search_cmp< int32_t >((int32_t const *)arg1,arg2,arg3,arg4);
689+
fresult = static_cast< int >(result);
690+
return fresult;
691+
}
692+
693+
694+
SWIGEXPORT int _wrap_binary_search__SWIG_5(SwigArrayWrapper *farg1, int64_t const *farg3, bool (*farg4)(int64_t,int64_t)) {
695+
int fresult ;
696+
int64_t *arg1 = (int64_t *) 0 ;
697+
size_t arg2 ;
698+
int64_t arg3 ;
699+
bool (*arg4)(int64_t,int64_t) = (bool (*)(int64_t,int64_t)) 0 ;
700+
int result;
701+
702+
arg1 = static_cast< int64_t * >(farg1->data);
703+
arg2 = farg1->size;
704+
arg3 = static_cast< int64_t >(*farg3);
705+
arg4 = reinterpret_cast< bool (*)(int64_t,int64_t) >(farg4);
706+
result = (int)binary_search_cmp< int64_t >((int64_t const *)arg1,arg2,arg3,arg4);
707+
fresult = static_cast< int >(result);
708+
return fresult;
709+
}
710+
711+
712+
SWIGEXPORT int _wrap_binary_search__SWIG_6(SwigArrayWrapper *farg1, double const *farg3, bool (*farg4)(double,double)) {
713+
int fresult ;
714+
double *arg1 = (double *) 0 ;
715+
size_t arg2 ;
716+
double arg3 ;
717+
bool (*arg4)(double,double) = (bool (*)(double,double)) 0 ;
718+
int result;
719+
720+
arg1 = static_cast< double * >(farg1->data);
721+
arg2 = farg1->size;
722+
arg3 = static_cast< double >(*farg3);
723+
arg4 = reinterpret_cast< bool (*)(double,double) >(farg4);
724+
result = (int)binary_search_cmp< double >((double const *)arg1,arg2,arg3,arg4);
725+
fresult = static_cast< int >(result);
726+
return fresult;
727+
}
728+
729+
664730
SWIGEXPORT void _wrap_shuffle__SWIG_1(SwigClassWrapper const *farg1, SwigArrayWrapper *farg2) {
665731
std::mt19937_64 *arg1 = 0 ;
666732
int32_t *arg2 = (int32_t *) 0 ;

0 commit comments

Comments
 (0)