utils

old (and slightly embarrassing) C utility scripts
Log | Files | Refs | README | LICENSE

utilint.h (3272B)


      1 /*
      2 Utility functions pertaining to integers and integer arrays
      3 Mao Yamanaka
      4 Because there are no namespaces in C, all functions have the prefix" ui_".
      5 */
      6 
      7 #pragma once
      8 
      9 #include <math.h>
     10 #include <stdio.h>
     11 #include <stdlib.h>
     12 
     13 //PART 1: Integer array utilities
     14 
     15 int ui_findpeak(int* aI0, int aI1, int aI2, int aI3){
     16   //ARGS: input array, length of array, lowest/highest [0/1], threshfold
     17   //RESULT: location of peak
     18   int tVal = aI3;
     19   int tIdx = 0;
     20   for(int it0 = 0; it0 < aI1; it0++){
     21     if((aI2 == 1 && tVal < aI0[it0]) || (aI2 == 0 && tVal > aI0[it0])){
     22       tVal = aI0[it0];
     23       tIdx = it0;
     24     }
     25   }
     26   return tIdx;
     27 }
     28 
     29 int ui_sum(int* aI0, int aI1){
     30   //ARGS: input array, length of array
     31   //RESULT: sum of array
     32   int ret = 0;
     33   for(int it0 = 0; it0 < aI1; it0++) ret += aI0[it0];
     34   return ret;
     35 }
     36 
     37 int ui_search(int* aI0, int aI1, int aI2){
     38   //ARGS: input array, length of array, target value
     39   //RESULT: location of first instance of value. -1 if not found
     40   int ret = -1;
     41   for(int it0 = 0; it0 < aI1; it0++){
     42     if(aI0[it0] == aI2){
     43       ret = it0;
     44       break;
     45     }
     46   }
     47   return ret;
     48 }
     49 
     50 void ui_print(int* aI0, int aI1){
     51   //ARGS: input array, length of array
     52   //RESULT: array printed to stdout
     53   for(int it0 = 0; it0 < aI1; it0++){
     54     printf("%d", aI0[it0]);
     55     if(it0 == aI1 - 1) printf("\n");
     56     else printf(", ");
     57   }
     58 }
     59 
     60 void ui_printrect(int* aI0, int aI1, int aI2){
     61   //ARGS: input array, length of array, rectangle length
     62   //RESULT: array printed in rectangular format to stdout
     63   for(int it0 = 0; it0 < aI1; it0++){
     64     printf("%d", aI0[it0]);
     65     if(it0 == aI1 - 1 || it0%aI2 == aI2 - 1) printf("\n");
     66     else printf(", ");
     67   }
     68 }
     69 
     70 int* ui_array(int aI0, int aI1){
     71   //ARGS: length of array, default value
     72   //RESULT: integer array with constant values
     73   int* ret = malloc(sizeof(int)*aI0);
     74   for(int it0 = 0; it0 < aI0; it0++) ret[it0] = aI1;
     75   return ret;
     76 }
     77 
     78 int* ui_duplicate(int* aI0, int aI1){
     79   //ARGS: input array, length of array
     80   //RESULT: another instance of input array
     81   int* ret = malloc(sizeof(int)*aI1);
     82   for(int it0 = 0; it0 < aI1; it0++) ret[it0] = aI0[it0];
     83   return ret;
     84 }
     85 
     86 int ui_nInst(int* aI0, int aI1, int aI2){
     87   //ARGS: input array, length of array, value of interest
     88   //RESULT: number of instances of said value
     89   int ret = 0;
     90   for(int i = 0; i < aI1; i++){
     91     if(aI0[i] == aI2) ret++;
     92   }
     93   return ret;
     94 }
     95 
     96 int* ui_uniform(int aI0, int aI1, int aI2){
     97   //ARGS: number of elements, lowest value, highest value
     98   //RESULT: array of uniformly divided array given a range
     99   int* ret = malloc(sizeof(int)*aI0);
    100   float ddx = ((float)(aI2 - aI1))/aI0;
    101   for(int i = 0; i < aI0; i++) ret[i] = aI1 + i*ddx;
    102   return ret;
    103 }
    104 
    105 float ui_error(int* aI0, int* aI1, int aI2){
    106   //ARGS: first array, second array, length of arrays
    107   float ret = 0;
    108   for(int i = 0; i < aI2; i++) ret += pow(aI0[i] - aI1[i], 2);
    109   return sqrt(ret);
    110 }
    111 
    112 //PART 2: Discrete math utilities
    113 
    114 int ui_egcd(int aI0, int aI1){
    115   //ARGS: any positive integer, another positive integer
    116   //RESULT: the common divisor
    117   if(aI1 > aI0) return ui_egcd(aI1, aI0);
    118   else if(aI1 == 0) return aI0;
    119   else return ui_egcd(aI1, aI0%aI1);
    120 }
    121 
    122 int* ui_factors(int aI0){
    123   //ARGS: input positive integer
    124   //RESULT: list of all factors
    125   int* ret;
    126   return ret;
    127 }

Generated using stagit (https://codemadness.org/stagit.html)