Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 1a58b036a6c25f30ad0d4db5995fa5798f61b0d3 @spott committed Feb 10, 2009
Showing with 287 additions and 0 deletions.
  1. 0 README
  2. +56 −0 hash.c
  3. +62 −0 hash.h
  4. +21 −0 smem_check.h
  5. +31 −0 spottlib.c
  6. +25 −0 spottlib.h
  7. +92 −0 tests.c
0 README
No changes.
56 hash.c
@@ -0,0 +1,56 @@
+#include "hash.h"
+#include <stdlib.h>
+
+
+/* This should be as simple as just creating a new hash table and then
+ * returning a pointer to it, right?
+ */
+hash *new_hash_table(int table_size) {
+ hash *h;
+ h.count = table_size;
+ h.hash_node = (hash_node *) calloc(table_size, sizeof(hash_node));
+
+ free_hash_table(h);
+
+ return h;
+}
+
+
+void free_hash_table(hash *ht) {
+ int i = 0;
+ while (i < ht.count) {
+ (ht->entry + i)-> value = (void *)0;
+ i++;
+ }
+}
+
+int hash_table_add_i(hash *ht, int key, void *value) {
+
+ int lk = hash( key , ht.count );
+
+ if ((ht + lk)->value == 0){
+ (ht + lk)->key = key;
+ (ht + lk)->value = value;
+ return 0;
+ }
+ else {
+ return 1;
+ }
+}
+
+int hash_table_find(hash_table *ht, int key, int *dest_value, int do_remove) {
+ int lk = hash(key,size);
+ int v = (ht + lk)->value;
+ // check for collisions
+ if ((ht + lk)->key != key)
+ return 0;
+
+ if (v) { //does hash entry exist?
+ if (do_remove) //check if you should remove the value
+ (ht + lk)->value = 0; //remove value
+ return *dest_value = v; //return value
+ }
+ else
+ return (ht + lk)->value;
+}
+
62 hash.h
@@ -0,0 +1,62 @@
+/* hash.h
+ * Andrew Spott
+ *
+ * create a hash table for recording what
+ * locations in the code write to memory,
+ * and how much memory they write.
+ */
+
+#ifndef HASH_H
+#define HASH_H
+
+#define BIGPRIME 982451653
+
+#define hash(A,B) BIGPRIME % A % B
+
+
+typedef struct hash_node_ {
+ int keyi;
+ float keyf;
+ char *keys;
+ void *value;
+ hash_node *next, *prev;
+} hash_node;
+
+typedef struct hash_ {
+ int count;
+ hash_node **entry;
+} hash
+
+/* initial memory used should be linearly
+ * proportional to table_size. (maybe 2 times
+ * table size.
+ */
+hash *new_hash_table(int table_size);
+
+/* dispose of all data, replacing values
+ * with zero
+ */
+void free_hash_table(hash *ht);
+
+/* associate value with key. return nonzero
+ * if key is already in use and then don't
+ * add entry. (I guess a linked list for each
+ * value would be a bad thing?
+ */
+int hash_add_i(hash *ht, int key, void *value);
+int hash_add_f(hash *ht, float key, void *value);
+int hash_add_s(hash *ht, char *key, void *value);
+
+
+
+/* resolve key and place value in dest_value.
+ * If do_remove is nonzero then remove the hash
+ * entry as well if found. return nonzero if key
+ * was found. if key was not found, return zero
+ * and do not alter *dest_value.
+ */
+int hash_find_i(hash *ht, int key, void *dest_value, int do_remove);
+int hash_find_f(hash *ht, float key, void *dest_value, int do_remove);
+int hash_find_s(hash *ht, char *key, void *dest_value, int do_remove);
+
+#endif
21 smem_check.h
@@ -0,0 +1,21 @@
+#ifdef MEM_CHECK
+#include <stdlib.h>
+
+/* get the line number (__LINE__) and filename (__FILE__) using macros,
+ * then connect them and put them in a hash with the number of bytes allocated
+ * then use malloc to aquire A + 20 bytes and return a pointer 10 bytes into the
+ * allocation. I think I can do this by adding 10 to the pointer. These 20 bytes
+ * should have a pattern filled in ("000000" works I think...)
+ */
+#define smalloc(A, N) void *p = (calloc(1,(sizeof(A)*N)+20) + 10); hash_table_add_s(ht, (int)p, (void *)(sizeof(A)*N + 20))
+
+#define sfree(A) free(A); hash_table_find(ht, (int)A, dummy, 1)
+
+#else /* MEM_CHECK */
+
+#define smalloc(A) malloc(A)
+#define sfree(A) free(A)
+#define srealloc(A) realloc(A)
+
+#endif /* MEM_CHECK */
+
31 spottlib.c
@@ -0,0 +1,31 @@
+#include "spottlib.h";
+
+void reverse(char *s) {
+ int c, i ,j;
+
+ for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+}
+
+//from K&R pg 64:
+void itoa(int n, char *s) {
+ int i, sign;
+
+ if ((sign = n) < 0)
+ n = -n;
+ i = 0;
+
+ do { /*generate digits in reverse order */
+ s[i++] = n % 10 + '0';
+ } while ((n /= 10) > 0);
+
+ if (sign < 0)
+ s[i++] = '-';
+ s[i] = '\0';
+ reverse(s);
+}
+
+
25 spottlib.h
@@ -0,0 +1,25 @@
+/*
+ * spottlib.h
+ *
+ * Basic lib with some usefull functions,
+ *
+ * Created by Andrew Spott on 12/4/08.
+ * Copyright 2008 Andrew Spott. All rights reserved.
+ *
+ */
+#include<string.h>
+
+#ifndef SPOTTLIB_H
+#define SPOTTLIB_H
+
+
+/* creates a character array that is ascii for the value of the
+ * int
+ */
+void itoa(int, char[]);
+
+/* reverses a character array
+ */
+void reverse(char *);
+
+#endif
92 tests.c
@@ -0,0 +1,92 @@
+/* tests.c
+ * Andrew Spott
+ *
+ * tests the libraries that they do what they are supposed to do.
+ */
+
+#include "libraries/hash.h"
+#include "libraries/spottlib.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int test_spottlib();
+int test_hash();
+
+int main() {
+
+ int ispottlib = test_spottlib();
+ int ihashlib = test_hash();
+
+ printf("spottlib.h returned %d positive functions\n", ispottlib);
+ printf("hash.h returned %d positive functions\n", ihashlib);
+
+}
+
+int test_hash() {
+ hash_table *t = new_hash_table(1000);
+ int i = 0;
+ int b = 0;
+ int d = 0;
+ int tests = 0;
+
+ while (!hash_table_add(t, i, b)){
+ //printf("%d, %d\n", i, b);
+ i++;
+ b++;
+ tests++;
+ }
+ i--;
+
+
+ while (hash_table_find(t, i, &d, 1)) {
+ if (d == i) {
+ //printf("%d, %d\n",d,i);
+ tests++;
+ i--;
+ }
+ else
+ printf("key didn't match value");
+ }
+
+ return tests;
+}
+
+int test_spottlib() {
+ int tests = 0;
+
+ /* test itoa(int,char[]), by using atoi */
+ int iitoa = 95047;
+ char citoa[6];
+ char fitoa[6];
+
+ itoa(iitoa, citoa);
+ sprintf(fitoa,"%d",iitoa);
+ printf("%s - %s - %d\n",fitoa,citoa,iitoa);
+
+ if ( strcmp(fitoa, citoa) == 0)
+ tests++;
+
+ /* test reverse (tested in itoa as well) */
+ char ctest[106] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
+ int b = 0;
+ int t = 0;
+ char creverse[106] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
+
+ reverse( creverse );
+ printf("%s\n%s\n", ctest, creverse);
+
+ while(b < 52) {
+ int j = 103 - b;
+ printf("%d - %c, %c\n",b ,creverse[b],ctest[j]);
+ if (creverse[b] == ctest[j])
+ t++;
+ b++;
+ }
+ printf("tests: %d\n",t);
+ if (t == 52)
+ tests++;
+
+ return tests;
+}
+
+

0 comments on commit 1a58b03

Please sign in to comment.
Something went wrong with that request. Please try again.