reading, and sorting chunks
This commit is contained in:
parent
36035058f2
commit
d22c0606c1
5 changed files with 78 additions and 15 deletions
17
TODO.md
17
TODO.md
|
|
@ -1,7 +1,7 @@
|
||||||
- [ ] file operations
|
- [x] file operations
|
||||||
- [ ] write to file (at index)
|
- [x] write to file (at index)
|
||||||
- [ ] print_file (maybe just calling cat???)
|
- [x] read file
|
||||||
- [ ] a tape type - representing a file?
|
- [x] a tape type - representing a file?
|
||||||
- [x] a record type - x1,x2,x3,x4,x5
|
- [x] a record type - x1,x2,x3,x4,x5
|
||||||
- [x] comparing records
|
- [x] comparing records
|
||||||
- [x] buffer quick sort
|
- [x] buffer quick sort
|
||||||
|
|
@ -9,5 +9,14 @@
|
||||||
- [x] cmd args - man getopts
|
- [x] cmd args - man getopts
|
||||||
- [x] file input
|
- [x] file input
|
||||||
- [x] keyboard input
|
- [x] keyboard input
|
||||||
|
- [ ] stage 1
|
||||||
|
- [x] reading chunks
|
||||||
|
- [x] sorting
|
||||||
|
- [ ] writing chunks
|
||||||
|
- [ ] stage 2
|
||||||
|
- [ ] reading chunks
|
||||||
|
- [ ] multiway merge
|
||||||
|
- [ ] writing chunks
|
||||||
|
- [ ] repeat untill done
|
||||||
- [ ] testing
|
- [ ] testing
|
||||||
- [ ] report
|
- [ ] report
|
||||||
|
|
|
||||||
36
main.c
36
main.c
|
|
@ -110,20 +110,40 @@ void write_file(options* opts)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort_file(options* opts)
|
int sort_block(tape* t, buffer* buff)
|
||||||
|
{
|
||||||
|
static int counter = 0;
|
||||||
|
int status = read_buffer(buff, t);
|
||||||
|
if (status != SUCCESS && status != EOF) {
|
||||||
|
fprintf(stderr, "Error reading file: %d\n", status);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
sort_buffer(buff);
|
||||||
|
printf("sorted the buffer %d, the results:\n", counter);
|
||||||
|
print_buffer(buff);
|
||||||
|
counter++;
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sort_file(options* opts)
|
||||||
{
|
{
|
||||||
tape a = { 0, 0, opts->input };
|
tape a = { 0, 0, opts->input };
|
||||||
buffer buff;
|
buffer buff;
|
||||||
buff.capacity = 100;
|
buff.capacity = opts->b * opts->n;
|
||||||
buff.location = malloc(sizeof(record) * buff.capacity);
|
buff.location = malloc(sizeof(record) * buff.capacity);
|
||||||
int status = read_buffer(&buff, &a);
|
int status = 0;
|
||||||
if (status != SUCCESS && status != EOF) {
|
while (status == SUCCESS) {
|
||||||
printf("Error reading file: %d\n", status);
|
status = sort_block(&a, &buff);
|
||||||
}
|
}
|
||||||
fclose(a.file);
|
if (status != EOF) {
|
||||||
a.file = stdout;
|
|
||||||
sort_buffer(&buff);
|
|
||||||
free(buff.location);
|
free(buff.location);
|
||||||
|
fclose(a.file);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
free(buff.location);
|
||||||
|
fclose(a.file);
|
||||||
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
|
|
||||||
6
makefile
6
makefile
|
|
@ -16,9 +16,9 @@ clean:
|
||||||
rm -rf build
|
rm -rf build
|
||||||
|
|
||||||
run: build/main
|
run: build/main
|
||||||
build/main -f tests/1.in
|
build/main -f tests/2.in | less
|
||||||
|
|
||||||
debug: build/main
|
debug: build/main
|
||||||
lldb -- build/main -f tests/1.in
|
lldb -- build/main -f tests/2.in
|
||||||
|
|
||||||
.PHONY: clean run
|
.PHONY: clean run debug
|
||||||
|
|
|
||||||
33
tape.c
33
tape.c
|
|
@ -60,6 +60,39 @@ int read_buffer(buffer* buff, tape* tape)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_full_buffer(buffer* buff)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < buff->length; i++) {
|
||||||
|
record* current = buff->location;
|
||||||
|
current += i;
|
||||||
|
printf("i: %d\tg: %d\t", i, g(current));
|
||||||
|
print_record(stdout, current);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_buffer(buffer* buff)
|
||||||
|
{
|
||||||
|
if (buff->length <= 10) {
|
||||||
|
print_full_buffer(buff);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
record* current = buff->location;
|
||||||
|
current += i;
|
||||||
|
printf("i: %d\tg: %d\t", i, g(current));
|
||||||
|
print_record(stdout, current);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
for (int i = buff->length - 5; i < buff->length; i++) {
|
||||||
|
record* current = buff->location;
|
||||||
|
current += i;
|
||||||
|
printf("i: %d\tg: %d\t", i, g(current));
|
||||||
|
print_record(stdout, current);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int write_buffer(buffer* buff, tape* tape)
|
int write_buffer(buffer* buff, tape* tape)
|
||||||
{
|
{
|
||||||
record* current = buff->location;
|
record* current = buff->location;
|
||||||
|
|
|
||||||
1
tape.h
1
tape.h
|
|
@ -20,5 +20,6 @@ void sort_buffer(buffer* buff);
|
||||||
int read_buffer(buffer* buff, tape* tape);
|
int read_buffer(buffer* buff, tape* tape);
|
||||||
// writes everything
|
// writes everything
|
||||||
int write_buffer(buffer* buff, tape* tape);
|
int write_buffer(buffer* buff, tape* tape);
|
||||||
|
void print_buffer(buffer* buff);
|
||||||
|
|
||||||
void clear_tape(tape* tape);
|
void clear_tape(tape* tape);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue