Skip to content

Commit

Permalink
Ready for submission
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat committed Mar 6, 2020
1 parent bcb2f58 commit b2bfe85
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
41 changes: 33 additions & 8 deletions exercises/ex05/find_track_oo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ char tracks[][80] = {
};


typedef regex_t Regex;
typedef struct {
regex_t inner_struct[1];
} Regex;


/* Returns a new Regex that matches the given pattern.
Expand All @@ -30,27 +32,50 @@ typedef regex_t Regex;
* returns: new Regex
*/
Regex *make_regex(char *pattern, int flags) {
// FILL THIS IN!
return NULL;
regex_t regex;
int ret;

ret = regcomp(&regex, pattern, REG_EXTENDED | REG_NOSUB);
if (ret) {
fprintf(stderr, "Could not compile regex\n");
exit(1);
}

Regex *r = malloc(sizeof(Regex));
r->inner_struct[0] = regex;

return r;
}

/* Checks whether a regex matches a string.
*
* regex: Regex pointer
* regex: Regex
* s: string
* returns: 1 if there's a match, 0 otherwise
*/
int regex_match(Regex *regex, char *s) {
// FILL THIS IN!
return 0;
int ret;
char msgbuf[100];

ret = regexec(&(regex->inner_struct[0]), s, 0, NULL, 0);
if (!ret) {
// printf("Track %i: '%s'\n", i, tracks[i]);
return 1;
} else if (ret == REG_NOMATCH) {
return 0;
} else {
regerror(ret, &(regex->inner_struct[0]), msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
}

/* Frees a Regex.
*
* regex: Regex pointer
* regex: Regex
*/
void regex_free(Regex *regex) {
// FILL THIS IN!
regfree(&(regex->inner_struct[0]));
}


Expand Down
36 changes: 34 additions & 2 deletions exercises/ex05/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ License: MIT License https://opensource.org/licenses/MIT
*/

#include <stdlib.h>
#include "rand.h"
#include <inttypes.h>

// generate a random float using the algorithm described
// at http://allendowney.com/research/rand
Expand Down Expand Up @@ -78,7 +80,37 @@ float my_random_float2()
// compute a random double using my algorithm
double my_random_double()
{
// TODO: fill this in
int mant;
int exp = 2047;
int mask = 1;
uint64_t x;

union {
double d;
int i;
} b;

// generate random bits until we see the first set bit
while (1) {
x = rand();
if (x == 0) {
exp -= 63;
} else {
break;
}
}

// find the location of the first set bit and compute the exponent
while (x & mask) {
mask <<= 1;
exp--;
}

// use the remaining bit as the mantissa
mant = x >> 12;
b.i = (exp << 52) | mant;

return b.d;
}

// return a constant (this is a dummy function for time trials)
Expand Down Expand Up @@ -114,7 +146,7 @@ float random_float()


// generate a random double using the standard algorithm
float random_double()
double random_double()
{
int x;
double f;
Expand Down
28 changes: 24 additions & 4 deletions exercises/ex05/time_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ double get_seconds() {
}

/* Compute the total time used by a function.
iters: number of times to call the function
func: function to call
*/
Expand All @@ -52,8 +51,24 @@ double time_func(int iters, float(*func)())
return t1 - t0;
}

double time_func_double(int iters, double(*func)())
{
int i;
double d;
double t0, t1;

srandom(time(NULL));

t0 = get_seconds();
for (i=0; i<iters; i++) {
d = func();
}
t1 = get_seconds();
return t1 - t0;
}


main(int argc, char *argv[])
int main(int argc, char *argv[])
{
double time;
int iters = 100000000;
Expand All @@ -74,6 +89,11 @@ main(int argc, char *argv[])
time = time_func(iters, my_random_float2);
printf("%f ms \t my_random_float2\n", time);

time = time_func(iters, random_float);
printf("%f ms \t random_float\n", time);
printf("\n");

time = time_func_double(iters, random_double);
printf("%f ms \t random_double\n", time);

time = time_func_double(iters, my_random_double);
printf("%f ms \t my_random_double\n", time);
}

0 comments on commit b2bfe85

Please sign in to comment.