diff options
Diffstat (limited to 'packages/base/src/C/vector-aux.c')
-rw-r--r-- | packages/base/src/C/vector-aux.c | 90 |
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 | ||
618 | int 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 | |||
618 | int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) { | 627 | int 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 | ||
1000 | int sort_values(KDVEC(v),DVEC(r)) { | 1009 | int 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 | ||
1015 | int | ||
1016 | compare_floats (const void *a, const void *b) { | ||
1017 | return *(float*)a > *(float*)b; | ||
1018 | } | ||
1019 | |||
1020 | int 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 | |||
1026 | int | ||
1027 | compare_ints(const void *a, const void *b) { | ||
1028 | return *(int*)a > *(int*)b; | ||
1029 | } | ||
1030 | |||
1031 | int 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 | |||
1057 | typedef struct SDI { int pos; double val;} DI; | ||
1058 | |||
1059 | int compare_doubles_i (const void *a, const void *b) { | ||
1060 | return ((DI*)a)->val > ((DI*)b)->val; | ||
1061 | } | ||
1062 | |||
1063 | int sort_indexD(KDVEC(v),IVEC(r)) { | ||
1064 | SORTIDX_IMP(DI,compare_doubles_i) | ||
1065 | } | ||
1066 | |||
1067 | |||
1068 | typedef struct FI { int pos; float val;} FI; | ||
1069 | |||
1070 | int compare_floats_i (const void *a, const void *b) { | ||
1071 | return ((FI*)a)->val > ((FI*)b)->val; | ||
1072 | } | ||
1073 | |||
1074 | int sort_indexF(KFVEC(v),IVEC(r)) { | ||
1075 | SORTIDX_IMP(FI,compare_floats_i) | ||
1076 | } | ||
1077 | |||
1078 | |||
1079 | typedef struct II { int pos; int val;} II; | ||
1080 | |||
1081 | int compare_ints_i (const void *a, const void *b) { | ||
1082 | return ((II*)a)->val > ((II*)b)->val; | ||
1083 | } | ||
1084 | |||
1085 | int sort_indexI(KIVEC(v),IVEC(r)) { | ||
1086 | SORTIDX_IMP(II,compare_ints_i) | ||
1087 | } | ||
1088 | |||
1089 | |||
1006 | //////////////////////////////////////////////////////////////////////////////// | 1090 | //////////////////////////////////////////////////////////////////////////////// |
1007 | 1091 | ||
1008 | int round_vector(KDVEC(v),DVEC(r)) { | 1092 | int round_vector(KDVEC(v),DVEC(r)) { |