summaryrefslogtreecommitdiff
path: root/packages/base/src/C/vector-aux.c
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/C/vector-aux.c')
-rw-r--r--packages/base/src/C/vector-aux.c90
1 files changed, 87 insertions, 3 deletions
diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c
index 134ca7a..5662697 100644
--- a/packages/base/src/C/vector-aux.c
+++ b/packages/base/src/C/vector-aux.c
@@ -615,6 +615,15 @@ int mapValF(int code, float* pval, KFVEC(x), FVEC(r)) {
615 } 615 }
616} 616}
617 617
618int mod (int a, int b) {
619 int m = a % b;
620 if (b>0) {
621 return m >=0 ? m : m+b;
622 } else {
623 return m <=0 ? m : m+b;
624 }
625}
626
618int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) { 627int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) {
619 int k; 628 int k;
620 int val = *pval; 629 int val = *pval;
@@ -625,8 +634,8 @@ int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) {
625 OPV(1,val/xp[k]) 634 OPV(1,val/xp[k])
626 OPV(2,val+xp[k]) 635 OPV(2,val+xp[k])
627 OPV(3,val-xp[k]) 636 OPV(3,val-xp[k])
628 OPV(6,val%xp[k]) 637 OPV(6,mod(val,xp[k]))
629 OPV(7,xp[k]%val) 638 OPV(7,mod(xp[k],val))
630 default: ERROR(BAD_CODE); 639 default: ERROR(BAD_CODE);
631 } 640 }
632} 641}
@@ -997,12 +1006,87 @@ compare_doubles (const void *a, const void *b) {
997 return *(double*)a > *(double*)b; 1006 return *(double*)a > *(double*)b;
998} 1007}
999 1008
1000int sort_values(KDVEC(v),DVEC(r)) { 1009int sort_valuesD(KDVEC(v),DVEC(r)) {
1001 memcpy(rp,vp,vn*sizeof(double)); 1010 memcpy(rp,vp,vn*sizeof(double));
1002 qsort(rp,rn,sizeof(double),compare_doubles); 1011 qsort(rp,rn,sizeof(double),compare_doubles);
1003 OK 1012 OK
1004} 1013}
1005 1014
1015int
1016compare_floats (const void *a, const void *b) {
1017 return *(float*)a > *(float*)b;
1018}
1019
1020int sort_valuesF(KFVEC(v),FVEC(r)) {
1021 memcpy(rp,vp,vn*sizeof(float));
1022 qsort(rp,rn,sizeof(float),compare_floats);
1023 OK
1024}
1025
1026int
1027compare_ints(const void *a, const void *b) {
1028 return *(int*)a > *(int*)b;
1029}
1030
1031int sort_valuesI(KIVEC(v),IVEC(r)) {
1032 memcpy(rp,vp,vn*sizeof(int));
1033 qsort(rp,rn,sizeof(int),compare_ints);
1034 OK
1035}
1036
1037////////////////////////////////////////
1038
1039
1040#define SORTIDX_IMP(T,C) \
1041 T* x = (T*)malloc(sizeof(T)*vn); \
1042 int k; \
1043 for (k=0;k<vn;k++) { \
1044 x[k].pos = k; \
1045 x[k].val = vp[k]; \
1046 } \
1047 \
1048 qsort(x,vn,sizeof(T),C); \
1049 \
1050 for (k=0;k<vn;k++) { \
1051 rp[k] = x[k].pos; \
1052 } \
1053 free(x); \
1054 OK
1055
1056
1057typedef struct SDI { int pos; double val;} DI;
1058
1059int compare_doubles_i (const void *a, const void *b) {
1060 return ((DI*)a)->val > ((DI*)b)->val;
1061}
1062
1063int sort_indexD(KDVEC(v),IVEC(r)) {
1064 SORTIDX_IMP(DI,compare_doubles_i)
1065}
1066
1067
1068typedef struct FI { int pos; float val;} FI;
1069
1070int compare_floats_i (const void *a, const void *b) {
1071 return ((FI*)a)->val > ((FI*)b)->val;
1072}
1073
1074int sort_indexF(KFVEC(v),IVEC(r)) {
1075 SORTIDX_IMP(FI,compare_floats_i)
1076}
1077
1078
1079typedef struct II { int pos; int val;} II;
1080
1081int compare_ints_i (const void *a, const void *b) {
1082 return ((II*)a)->val > ((II*)b)->val;
1083}
1084
1085int sort_indexI(KIVEC(v),IVEC(r)) {
1086 SORTIDX_IMP(II,compare_ints_i)
1087}
1088
1089
1006//////////////////////////////////////////////////////////////////////////////// 1090////////////////////////////////////////////////////////////////////////////////
1007 1091
1008int round_vector(KDVEC(v),DVEC(r)) { 1092int round_vector(KDVEC(v),DVEC(r)) {