1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Mia Herkt 327cbf6d6b
07: C: Add target size parameters 2022-12-07 22:31:12 +01:00
Mia Herkt 3e5e62fe7b
07: C: Fix some warnings 2022-12-07 22:30:30 +01:00
Mia Herkt 6060b590d3
07: C: Hash entries 2022-12-07 22:29:46 +01:00
Mia Herkt 59bf88cb6b
07: C: Reduce malloc calls 2022-12-07 21:44:06 +01:00
1 changed files with 34 additions and 14 deletions

View File

@ -1,20 +1,32 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h>
uint32_t fnv1a_32(const char *buf, size_t len)
{
uint32_t h = 0x811c9dc5U;
for (; len > 0; len--)
h = 0x01000193U * (h ^ *buf++);
return h;
}
typedef struct entry { typedef struct entry {
char *name; uint32_t id;
int isdir; int isdir;
size_t size; size_t size;
struct entry **entries;
size_t n_entries; size_t n_entries;
size_t n_alloc;
struct entry **entries;
struct entry *parent; struct entry *parent;
} entry; } entry;
entry *findent(entry *e, int isdir, const char *name) { entry *findent(entry *e, int isdir, const char *name) {
uint32_t id = fnv1a_32(name, strlen(name));
for (size_t i = 0; i < e->n_entries; i++) { for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->isdir == isdir) if (e->entries[i]->isdir == isdir)
if (!strcmp(e->entries[i]->name, name)) if (e->entries[i]->id == id)
return e->entries[i]; return e->entries[i];
} }
@ -22,37 +34,41 @@ entry *findent(entry *e, int isdir, const char *name) {
} }
entry *addent(entry *parent, int isdir, const char *name) { entry *addent(entry *parent, int isdir, const char *name) {
if (parent->n_entries) if (parent->n_entries >= parent->n_alloc) {
parent->entries = realloc(parent->entries, ++(parent->n_entries) * sizeof(entry*)); parent->n_alloc += 10;
else if (parent->n_entries)
parent->entries = malloc(++(parent->n_entries) * sizeof(entry*)); parent->entries = realloc(parent->entries, parent->n_alloc * sizeof(entry*));
else
parent->entries = malloc(parent->n_alloc * sizeof(entry*));
}
parent->n_entries++;
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry)); entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
e->id = fnv1a_32(name, strlen(name));
e->isdir = isdir; e->isdir = isdir;
e->name = malloc(strlen(name) + 1);
strcpy(e->name, name);
e->parent = parent; e->parent = parent;
return e; return e;
} }
int main(void) { int main(int argc, char **argv) {
char l[64]; char l[64];
entry *root = calloc(1, sizeof(entry)); entry *root = calloc(1, sizeof(entry));
root->isdir++; root->isdir++;
entry *current; entry *current = root;
size_t S = 0, G = 0, required = 0; size_t S = 0, G = 0, required = 0;
while (fgets(l, 64, stdin)) { while (fgets(l, 64, stdin)) {
l[strlen(l) - 1] = '\0'; l[strlen(l) - 1] = '\0';
if (l[0] == '$') { if (l[0] == '$') {
if (l[2] == 'c') if (l[2] == 'c') {
if (l[5] == '/') if (l[5] == '/')
current = root; current = root;
else if (l[5] == '.') else if (l[5] == '.')
current = current->parent; current = current->parent;
else else
current = findent(current, 1, l + 5); current = findent(current, 1, l + 5);
}
} else if (l[0] == 'd') { } else if (l[0] == 'd') {
if (!findent(current, 1, l + 4)) if (!findent(current, 1, l + 4))
addent(current, 1, l + 4); addent(current, 1, l + 4);
@ -68,17 +84,21 @@ int main(void) {
} }
current = root; current = root;
required = 30000000UL - (70000000UL - root->size); if (argc == 3)
required = strtoul(argv[2], NULL, 10) - (strtoul(argv[1], NULL, 10) - root->size);
else
required = 30000000UL - (70000000UL - root->size);
do { do {
if (current->n_entries) { if (current->n_entries) {
current->n_entries--; current->n_entries--;
current = (current->entries++)[0]; current = (current->entries++)[0];
} else { } else {
if (current->isdir) if (current->isdir) {
if (current->size < 100000) if (current->size < 100000)
S += current->size; S += current->size;
else if (current->size >= required && (current->size < G || !G)) else if (current->size >= required && (current->size < G || !G))
G = current->size; G = current->size;
}
current = current->parent; current = current->parent;
} }