utils

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

commit c7e82c4030ec0c9cd10596bb83be60e00335bc5c
Author: M. Yamanaka <myamanaka@live.com>
Date:   Tue, 19 Jan 2021 23:34:26 -0500

uploading old embarrassing code

Diffstat:
ALICENSE | 22++++++++++++++++++++++
AREADME | 11+++++++++++
Autilfloat.h | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autilint.h | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autilllist.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autilopengl.h | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 383 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2021 M. Yamanaka <myamanaka@live.com> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README b/README @@ -0,0 +1,11 @@ +Utility files for C projects +"utils" +M. Yamanaka +email: myamanaka@live.com +website: csmyamanaka.com +license: MIT (See include "LICENSE" file for details) + +Description +=========== +These are files that I wrote a really long time ago mostly for testing +and to provide some utility for other projects. diff --git a/utilfloat.h b/utilfloat.h @@ -0,0 +1,60 @@ +#include <stdlib.h> + +void prntFlArr(float* a0, int a1){ + for(int i = 0; i < a1; i++){ + if(i < a1 - 1) printf("%f, ", a0[i]); + else printf("%f\n", a0[i]); + } +} + +float* flArr(int a0){ + float* ret = malloc(sizeof(float)*a0); + for(int i = 0; i < a0; i++) ret[i] = 0; + return ret; +} + +float* flRnd(int a0){ + float* ret = malloc(sizeof(float)*a0); + for(int i = 0; i < a0; i++) ret[i] = 2*((float)rand())/RAND_MAX - 1; + return ret; +} + +float** wFlArr(int a0, int a1){ + float** ret = malloc(sizeof(float*)*a0); + for(int i = 0; i < a0; i++) ret[i] = flArr(a1); + return ret; +} + +float** wFlRnd(int a0, int a1){ + float** ret = malloc(sizeof(float*)*a0); + for(int i = 0; i < a0; i++) ret[i] = flRnd(a1); + return ret; +} + +float* cpFlArr(float* a0, int a1){ + float* ret = malloc(sizeof(float)*a1); + for(int i = 0; i < a1; i++) ret[i] = a0[i]; + return ret; +} + +void pluseqFlArr(float* a0, float* a1, int a2){ + for(int i = 0; i < a2; i++) a0[i] = a0[i] + a1[i]; +} + +float* uf_arith(float* aF0, float* aF1, int aI0, int aI1, int aI2){ + //array 0, array 1, n rows, n cols, operation + float* ret = malloc(sizeof(float)*aI0*aI1); + for(int it0 = 0; it0 < aI0*aI1; it0++){ + if(aI2 == 0) ret[it0] = aF0[it0] + aF1[it0]; + else if(aI2 == 1) ret[it0] = aF0[it0] - aF1[it0]; + else if(aI2 == 2) ret[it0] = aF0[it0]*aF1[it0]; + else ret[it0] = aF0[it0]/aF1[it0]; + } + return ret; +} + +float* uf_mult(float* aF0, float* aF1, int aI0, int aI1, int aI2){ + //array 0, array 1, n row 0, n col 0 = n row 1, n col 1 + float* ret = malloc(sizeof(float)*aI0*aI2); + return ret; +} diff --git a/utilint.h b/utilint.h @@ -0,0 +1,110 @@ +/* +Utility functions pertaining to integers and integer arrays +Mao Yamanaka +Because there are no namespaces in C, all functions have the prefix" ui_". +*/ + +#pragma once + +#include <stdio.h> +#include <stdlib.h> + +//PART 1: Integer array utilities + +int ui_findpeak(int* aI0, int aI1, int aI2, int aI3){ + //ARGS: input array, length of array, lowest/highest [0/1], threshfold + //RESULT: location of peak + int tVal = aI3; + int tIdx = 0; + for(int it0 = 0; it0 < aI1; it0++){ + if((aI2 == 1 && tVal < aI0[it0]) || (aI2 == 0 && tVal > aI0[it0])){ + tVal = aI0[it0]; + tIdx = it0; + } + } + return tIdx; +} + +int ui_sum(int* aI0, int aI1){ + //ARGS: input array, length of array + //RESULT: sum of array + int ret = 0; + for(int it0 = 0; it0 < aI1; it0++) ret += aI0[it0]; + return ret; +} + +int ui_search(int* aI0, int aI1, int aI2){ + //ARGS: input array, length of array, target value + //RESULT: location of first instance of value. -1 if not found + int ret = -1; + for(int it0 = 0; it0 < aI1; it0++){ + if(aI0[it0] == aI2){ + ret = it0; + break; + } + } + return ret; +} + +void ui_print(int* aI0, int aI1){ + //ARGS: input array, length of array + //RESULT: array printed to stdout + for(int it0 = 0; it0 < aI1; it0++){ + printf("%d", aI0[it0]); + if(it0 == aI1 - 1) printf("\n"); + else printf(", "); + } +} + +void ui_printrect(int* aI0, int aI1, int aI2){ + //ARGS: input array, length of array, rectangle length + //RESULT: array printed in rectangular format to stdout + for(int it0 = 0; it0 < aI1; it0++){ + printf("%d", aI0[it0]); + if(it0 == aI1 - 1 || it0%aI2 == aI2 - 1) printf("\n"); + else printf(", "); + } +} + +int* ui_array(int aI0, int aI1){ + //ARGS: length of array, default value + //RESULT: integer array with constant values + int* ret = malloc(sizeof(int)*a0); + for(int it0 = 0; it0 < aI0; it0++) ret[it0] = aI1; + return ret; +} + +int* ui_duplicate(int* aI0, int aI1){ + //ARGS: input array, length of array + //RESULT: another instance of input array + int* ret = malloc(sizeof(int)*aI1); + for(int it0 = 0; it0 < a1; it0++) ret[it0] = a0[it0]; + return ret; +} + +int ui_nInst(int* aI0, int aI1, int aI2){ + //ARGS: input array, length of array, value of interest + //RESULT: number of instances of said value + int ret = 0; + for(int i = 0; i < a1; i++){ + if(a0[i] == a2) ret++; + } + return ret; +} + +//PART 2: Discrete math utilities + +int ui_egcd(int aI0, int aI1){ + //ARGS: any positive integer, another positive integer + //RESULT: the common divisor + if(aI1 > aI0) return ui_egcd(aI1, aI0); + else if(aI1 == 0) return aI0; + else return ui_egcd(aI1, aI0%aI1); +} + +int* ui_factors(int aI0){ + //ARGS: input positive integer + //RESULT: list of all factors + int* ret; + return ret; +} diff --git a/utilllist.h b/utilllist.h @@ -0,0 +1,86 @@ +#pragma once + +#include <stdlib.h> + +typedef struct fNode{ + struct fNode* next; + struct fNode* prev; + float val; +}fNode; + +typedef struct fLList{ + fNode* first; + fNode* last; + int n; +}fLList; + +fLList* initfLList(){ + fLList* ret = malloc(sizeof(fLList)); + ret->first = NULL; + ret->last = NULL; + ret->n = 0; + return ret; +} + +int appendfll(fLList* a0, float a1){ + fNode* temp = malloc(sizeof(fNode)); + temp->val = a1; + temp->next = NULL; + temp->prev = a0->last; + if(a0->n == 0) a0->first = temp; + else a0->last->next = temp; + a0->n = a0->n + 1; + a0->last = temp; + return 0; +} + +int prependfll(fLList* a0, float a1){ + fNode* temp = malloc(sizeof(fNode)); + temp->val = a1; + temp->next = a0->first; + temp->prev = NULL; + if(a0->n == 0) a0->last = temp; + else a0->first->prev = temp; + a0->n = a0->n + 1; + a0->first = temp; + return 0; +} + +int rmFirst(fLList* a0){ + if(a0->n == 0) return 0; + else if(a0->n == 1){ + free(a0->first); + a0->first = NULL; + a0->last = NULL; + }else{ + fNode* temp = a0->first; + a0->first = temp->next; + a0->first->prev = NULL; + temp->next = NULL; + free(temp); + } + a0->n = a0->n - 1; + return 0; +} + +int rmLast(fLList* a0){ + if(a0->n == 0) return 0; + else if(a0->n == 1){ + free(a0->first); + a0->first = NULL; + a0->last = NULL; + }else{ + fNode* temp = a0->last; + a0->last = temp->prev; + a0->last->next = NULL; + temp->prev = NULL; + free(temp); + } + a0->n = a0->n - 1; + return 0; +} + +int freefLList(fLList* a0){ + while(a0->n > 0) rmLast(a0); + return 0; +} diff --git a/utilopengl.h b/utilopengl.h @@ -0,0 +1,94 @@ +#pragma once + +#include <GL/glut.h> +#include <math.h> +#include <stdlib.h> + +//setting 3d stuff + +void setObjectT(GLfloat* aF0, GLfloat aF1, GLfloat aF2, GLfloat aF3){ + aF0[3] = aF1; + aF0[7] = aF2; + aF0[11] = aF3; +} + +void setObjectRx(GLfloat* aF0, GLfloat aF1){ + aF0[5] = cos(aF1); + aF0[6] = -sin(aF1); + aF0[9] = sin(aF1); + aF0[10] = cos(aF1); +} + +void setObjectRy(GLfloat* aF0, GLfloat aF1){ + aF0[0] = cos(aF1); + aF0[2] = sin(aF1); + aF0[8] = -sin(aF1); + aF0[10] = cos(aF1); +} + +void setObjectRz(GLfloat* aF0, GLfloat aF1){ + aF0[0] = cos(aF1); + aF0[1] = -sin(aF1); + aF0[4] = sin(aF1); + aF0[5] = cos(aF1); +} + +void scaleObject(GLfloat* aF0, GLfloat aF1){ + aF0[0] *= aF1; + aF0[5] *= aF1; + aF0[10] *= aF1; +} + +void setObjectS(GLfloat* aF0, GLfloat aF1){ + aF0[0] = aF1; + aF0[5] = aF1; + aF0[10] = aF1; +} + +//create zero matrix +GLfloat* ugl_Z(int aI0){ + GLfloat* ret = malloc(sizeof(GLfloat)*aI0); + for(int it0 = 0; it0 < aI0; it0++) ret[it0] = 0; + return ret; +} + +//create identity matrix +GLfloat* ugl_I(int aI0){ + GLfloat* ret = malloc(sizeof(GLfloat)*aI0*aI0); + for(int it0 = 0; it0 < aI0*aI0; it0++){ + if(it0%(aI0 + 1) == 0) ret[it0] = 1; + else ret[it0] = 0; + } + return ret; +} + +//create duplicate +GLfloat* ugl_duplicate(GLfloat* aF0, int aI0){ + GLfloat* ret = malloc(sizeof(GLfloat)*aI0); + for(int it0 = 0; it0 < aI0; it0++) ret[it0] = aF0[it0]; + return ret; +} + +//some arithmetics +GLfloat* ugl_mult(GLfloat* aF0, GLfloat* aF1, int aI0, int aI1, int aI2){ + //input array 0, input array 1, row 0, col 0/row1, col 1 + GLfloat* ret = ugl_Z(aI0*aI2); + for(int it0 = 0; it0 < aI0*aI2; it0++){ + for(int it1 = 0; it1 < aI1; it1++) ret[it0] += aF0[(it0/4)*aI1 + it1]*aF1[it1*aI2 + (it0%4)]; + } + return ret; +} + +//create a single transformation matrix from a list of transformation +//matrices +GLfloat* ugl_T(GLfloat** aF0){ + GLfloat* ret = ugl_mult(aF0[1], aF0[0], 4, 4, 4); + for(int it0 = 2; it0 < 5; it0++){ + GLfloat* temp = ugl_mult(aF0[it0], ret, 4, 4, 4); + free(ret); + ret = ugl_duplicate(temp, 16); + free(temp); + } + return ret; +} +

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