Skip to content

Commit a0f730a

Browse files
committed
initial commit
0 parents  commit a0f730a

File tree

12 files changed

+648
-0
lines changed

12 files changed

+648
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests/heap_t_tests.dSYM/
2+
build/
3+
tests/tests.log
4+
*.o
5+
*xcode*

Makefile

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS)
2+
LIBS=-ldl $(OPTLIBS)
3+
PREFIX?=/usr/local
4+
5+
SOURCES=$(wildcard src/**/*.c src/*.c)
6+
OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
7+
8+
TEST_SRC=$(wildcard tests/*_tests.c)
9+
TESTS=$(patsubst %.c,%,$(TEST_SRC))
10+
11+
TARGET=build/libsketch.a
12+
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
13+
14+
BINARY=cmsketch
15+
# The Target Build
16+
all: $(TARGET) $(SO_TARGET) $(BINARY) tests
17+
18+
dev: CFLAGS=-g -Wall -Isrc -Wall -Wextra $(OPTFLAGS)
19+
dev: all
20+
21+
$(TARGET): CFLAGS += -fPIC
22+
$(TARGET): build $(OBJECTS)
23+
ar rcs $@ $(OBJECTS)
24+
ranlib $@
25+
26+
$(SO_TARGET): $(TARGET) $(OBJECTS)
27+
$(CC) -shared -o $@ $(OBJECTS)
28+
29+
$(BINARY): $(SO_TARGET)
30+
$(CC) $(CFLAGS) -v -o build/$(BINARY) $(TARGET)
31+
32+
build:
33+
@mkdir -p build
34+
@mkdir -p bin
35+
36+
# The Unit Tests
37+
.PHONY: tests
38+
tests: CFLAGS += $(TARGET)
39+
tests: $(TESTS)
40+
sh ./tests/runtests.sh
41+
42+
valgrind:
43+
VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE)
44+
45+
# The Cleaner
46+
clean:
47+
rm -rf build $(OBJECTS) $(TESTS)
48+
rm -f tests/tests.log
49+
find . -name "*.gc*" -exec rm {} \;
50+
rm -rf `find . -name "*.dSYM" -print`
51+
52+
# The Install
53+
install: all
54+
install -d $(DESTDIR)/$(PREFIX)/lib/
55+
install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/
56+
57+
# The Checker
58+
BADFUNCS='[^_.>a-zA-Z0-9](str(n?cpy|n?cat|xfrm|n?dup|str|pbrk|tok|_)|stpn?cpy|a?sn?printf|byte_)'
59+
check:
60+
@echo Files with potentially dangerous functions.
61+
@egrep $(BADFUNCS) $(SOURCES) || true

include/dbg.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// dbg.h
3+
// cmsketch
4+
//
5+
// Created by Chris Guiney on 4/18/14.
6+
// Courtousy of Zed Shaw's Learn C the Hard Way
7+
//
8+
9+
#ifndef cmsketch_dbg_h
10+
#define cmsketch_dbg_h
11+
12+
#ifndef __dbg_h__
13+
#define __dbg_h__
14+
15+
#include <stdio.h>
16+
#include <errno.h>
17+
#include <string.h>
18+
19+
#ifdef NDEBUG
20+
#define debug(M, ...)
21+
#else
22+
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
23+
#endif
24+
25+
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
26+
27+
#define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
28+
29+
#define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
30+
31+
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
32+
33+
#define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
34+
35+
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
36+
37+
#define check_mem(A) check((A), "Out of memory.")
38+
39+
#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }
40+
41+
#endif
42+
43+
44+
#endif

include/heap_t.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// heap_t.h
3+
// cmsketch
4+
//
5+
// Created by Chris Guiney on 4/18/14.
6+
// Copyright (c) 2014 Chris Guiney. All rights reserved.
7+
//
8+
9+
#ifndef cmsketch_heap_t_h
10+
#define cmsketch_heap_t_h
11+
12+
typedef int (*comparator) (void* item, void* item2);
13+
14+
typedef struct {
15+
comparator cmp; // Comparator function
16+
int size; // Max size
17+
int items; // Items currently in heap
18+
size_t item_size; // Size of item
19+
void **list; // Array of items
20+
} heap_t;
21+
22+
heap_t* heap_create(int size, size_t type_size, comparator cmp);
23+
void heap_clear(heap_t* heap);
24+
void heap_destroy(heap_t* heap);
25+
void heap_push(heap_t* heap, void* item);
26+
void* heap_pop(heap_t* heap);
27+
void* heap_pushpop(heap_t* heap, void* item);
28+
void heap_sort(heap_t* heap);
29+
#endif

include/sketch_t.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// cmsketch.h
3+
// cmsketch
4+
//
5+
// Created by Chris Guiney on 4/18/14.
6+
// Copyright (c) 2014 Chris Guiney. All rights reserved.
7+
//
8+
#include "heap_t.h"
9+
#ifndef cmsketch_sketch_t_h
10+
#define cmsketch_sketch_t_h
11+
12+
typedef struct {
13+
int k; // Number of top-k items to keep track of
14+
int width; // Width of field
15+
int depth; // Depth of field
16+
int *field; // Int field -- width * depth length
17+
heap_t *heap; // Heap for sorting
18+
} sketch_t;
19+
20+
sketch_t* sketch_create(size_t data_size,
21+
double delta,
22+
double epsilon,
23+
int k,
24+
comparator cmp);
25+
void sketch_destroy(sketch_t *s);
26+
27+
#endif

include/util.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// util.h
3+
// cmsketch
4+
//
5+
// Created by Chris Guiney on 4/18/14.
6+
// Copyright (c) 2014 Chris Guiney. All rights reserved.
7+
//
8+
9+
#ifndef cmsketch_util_h
10+
#define cmsketch_util_h
11+
12+
void swap(void* a, void* b);
13+
14+
#endif

0 commit comments

Comments
 (0)