Created
August 9, 2018 21:14
-
-
Save moazin/7cfaf4a57d01f6b15a681bc0daccd0a4 to your computer and use it in GitHub Desktop.
Resizable Int Array imiplementation along with methods like map and filter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "myint.h" | |
struct myint* createEmpty() | |
{ | |
struct myint* newArray = (struct myint*) malloc(sizeof(struct myint)); | |
newArray->fullSize = 10; | |
newArray->currentSize = 0; | |
newArray->arr = (int*) malloc(10*sizeof(int)); | |
return newArray; | |
} | |
void push(struct myint* array, int number) | |
{ | |
if(array->currentSize == (array->fullSize)) | |
{ | |
// add a new guy | |
int newSize = array->fullSize * 2; | |
int *newArray = (int*) malloc(newSize * sizeof(int)); | |
for(int i = 0; i < (array->currentSize); i++) | |
{ | |
newArray[i] = array->arr[i]; | |
} | |
newArray[array->currentSize] = number; | |
free(array->arr); | |
array->arr = newArray; | |
array->fullSize = newSize; | |
array->currentSize++; | |
} | |
else | |
{ | |
// just add simply | |
array->arr[array->currentSize++] = number; | |
} | |
} | |
struct myint* createFromIntArray(int* arr, int size) | |
{ | |
struct myint* newArray = createEmpty(); | |
for(int i = 0; i < size; i++) | |
{ | |
push(newArray, arr[i]); | |
} | |
return newArray; | |
} | |
int atIndex(struct myint* array, int index) | |
{ | |
if(index > (array->currentSize - 1)) | |
{ | |
// raise an error | |
printf("Error"); | |
} | |
else | |
{ | |
return array->arr[index]; | |
} | |
} | |
void print(struct myint* array) | |
{ | |
for(int i = 0; i < array->currentSize; i++) | |
{ | |
printf("%d, ", array->arr[i]); | |
} | |
printf("\n"); | |
} | |
struct myint* map(int (*func)(), struct myint* array) | |
{ | |
struct myint* newArray = createEmpty(); | |
for(int i = 0; i < array->currentSize; i++) | |
{ | |
push(newArray, (*func)(array->arr[i])); | |
} | |
return newArray; | |
} | |
struct myint* filter(int (*func)(), struct myint* array) | |
{ | |
struct myint* newArray = createEmpty(); | |
for(int i = 0; i < array->currentSize; i++) | |
{ | |
if((*func)(array->arr[i]) == 1) | |
push(newArray, array->arr[i]); | |
} | |
return newArray; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct myint { | |
int* arr; | |
int fullSize; // keeps track of the total space we have | |
int currentSize; // keeps track of the data that we stored currently | |
}; | |
struct myint* createEmpty(); | |
struct myint* createFromIntArray(int* arr, int size); | |
void push(struct myint* array, int number); | |
int atIndex(struct myint* array, int index); | |
void print(struct myint* array); | |
struct myint* map(int (*func)(), struct myint* array); | |
struct myint* filter(int (*func)(), struct myint* array); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "myint.h" | |
int square(int x) | |
{ | |
return x * x; | |
} | |
int isEven(int x) | |
{ | |
if (x%2==0) | |
return 1; | |
else | |
return 0; | |
} | |
int main(void) | |
{ | |
int list[] = {1, 2, 3, 4, 5}; | |
struct myint* arr = createFromIntArray(list, 5); | |
struct myint* new = filter(isEven, arr); | |
print(new); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment