Skip to content

Commit 5f30cf3

Browse files
committed
Set up cmake and update workflow
1 parent 286298d commit 5f30cf3

File tree

9 files changed

+50
-33
lines changed

9 files changed

+50
-33
lines changed

.github/workflows/c.yml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ name: C/C++ CI
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: ["master"]
66
pull_request:
7-
branches: [ "master" ]
7+
branches: ["master"]
88

99
jobs:
1010
build:
11-
1211
runs-on: ubuntu-latest
1312

1413
steps:
15-
- uses: actions/checkout@v4
16-
- name: configure
17-
run: wget https://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt
18-
- name: build c
19-
run: cc -Wall -Wextra -ggdb -pedantic -o bit ./main.c && ./bit ./t8.shakespeare.txt
20-
21-
- name: build c++
22-
run: g++ -Wall -Wextra -ggdb -pedantic -o hashcpp ./hash.cpp && ./hashcpp ./t8.shakespeare.txt
23-
14+
- uses: actions/checkout@v4
15+
- name: configure
16+
run: wget https://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/files/t8.shakespeare.txt
17+
- name: build c
18+
run: ./build.sh
2419

20+
- name: build c++
21+
run: g++ -Wall -Wextra -ggdb -pedantic -o hashcpp ./hash.cpp && ./hashcpp ./t8.shakespeare.txt

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ t8.shakespeare.txt
22
test.txt
33
.git
44
build
5+
CMakeFiles/
6+
CMakeCache.txt
7+
cmake_install.cmake
58

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(hash_table)
3+
4+
add_library(hash_library STATIC src/hash_table.c)
5+
6+
target_include_directories(hash_library PUBLIC ${PROJECT_SOURCE_DIR}/include)
7+
8+
add_executable(hash_binary src/main.c)
9+
10+
target_link_libraries(hash_binary PRIVATE hash_library)

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
run:
2-
cc -Wall -Wextra -ggdb -pedantic -o ./build/bit ./main.c && ./build/bit ./t8.shakespeare.txt
2+
@./build/hash_binary ./t8.shakespeare.txt
33

44
runCpp:
5-
g++ -Wall -Wextra -ggdb -pedantic -o ./build/hashcpp ./hash.cpp && ./build/hashcpp ./t8.shakespeare.txt
5+
@mkdir -p ./build && g++ -Wall -Wextra -ggdb -pedantic -o ./build/hashcpp ./hash.cpp && ./build/hashcpp ./t8.shakespeare.txt

build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
BUILD_DIR="./build"
4+
5+
mkdir -p ${BUILD_DIR} &&
6+
echo -e "Changing directory to ${BUILD_DIR}" &&
7+
cd build && echo -e ${pwd} && cmake .. && make && cd ../ && make run

hash.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ int main(int argc, char *argv[]) {
3535
return 1;
3636
}
3737

38-
std::string content((std::istreambuf_iterator<char>(ifs)),
39-
(std::istreambuf_iterator<char>()));
38+
const std::string content((std::istreambuf_iterator<char>(ifs)),
39+
(std::istreambuf_iterator<char>()));
4040

4141
ifs.close();
4242
auto start = std::chrono::high_resolution_clock::now();
4343

4444
tokenize(content, hash_table);
4545

46-
auto end = std::chrono::high_resolution_clock::now();
47-
auto duration = std::chrono::duration<double>(end - start);
46+
const auto end = std::chrono::high_resolution_clock::now();
47+
const auto duration = std::chrono::duration<double>(end - start);
4848
std::cout << "Token counts:\n";
4949
std::vector<std::pair<std::string, size_t>> sorted_tokens(hash_table.begin(),
5050
hash_table.end());

includes/hash_table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ void init_table(hash_table *hash_table);
2525
size_t hash(char *str, size_t capacity);
2626
void *tokenize(char *input, hash_table *table);
2727
void free_table(token_t *table, size_t capacity);
28-
void naive(char *input, hash_table* hash_table);
28+
void naive(char *input, hash_table *hash_table);
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "hash_table.h"
1+
#include "../includes/hash_table.h"
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
@@ -63,19 +63,19 @@ void *tokenize(char *input, hash_table *hash_table) {
6363
}
6464

6565
for (size_t i = 0; i < hash_table->capacity; i++) {
66-
token_t *old_entry = &((token_t*)hash_table->table)[i];
66+
token_t *old_entry = &((token_t *)hash_table->table)[i];
6767
if (old_entry->key != NULL) {
68-
size_t new_key = hash(old_entry->key, new_capacity);
69-
size_t original_new_key = new_key;
70-
while (new_table[new_key].key != NULL) {
71-
new_key = (new_key + 1) % new_capacity;
72-
if (new_key == original_new_key) {
73-
break;
74-
}
68+
size_t new_key = hash(old_entry->key, new_capacity);
69+
size_t original_new_key = new_key;
70+
while (new_table[new_key].key != NULL) {
71+
new_key = (new_key + 1) % new_capacity;
72+
if (new_key == original_new_key) {
73+
break;
7574
}
76-
new_table[new_key].key = old_entry->key;
77-
new_table[new_key].value = old_entry->value;
78-
new_table[new_key].hash_key = new_key;
75+
}
76+
new_table[new_key].key = old_entry->key;
77+
new_table[new_key].value = old_entry->value;
78+
new_table[new_key].hash_key = new_key;
7979
}
8080
}
8181
free(hash_table->table);
@@ -140,7 +140,7 @@ void naive(char *input, hash_table *hash_table) {
140140
if (naive_table[i].key == NULL) {
141141
naive_table[i].key = strdup(token);
142142
if (naive_table[i].key == NULL) {
143-
continue;
143+
continue;
144144
}
145145
naive_table[i].value = 1;
146146
hash_table->size++;

main.c renamed to src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "includes/hash_table.c"
1+
#include "../includes/hash_table.h"
22
#include <stddef.h>
33
#include <stdint.h>
44
#include <stdio.h>

0 commit comments

Comments
 (0)