Created
April 12, 2016 17:48
-
-
Save kloetzl/84465b0981eb19201f0f16f99c7935a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
struct line { | |
char *foo, *bar, * baz; | |
long derp, herp; | |
}; | |
int main(int argc, char const *argv[]) | |
{ | |
long LOWER = atol(argv[1]); | |
long UPPER = atol(argv[2]); | |
long MAX_LINES = 1000000; | |
FILE* input = stdin; | |
if (argc == 4 && strcmp(argv[3],"-") != 0) { | |
input = fopen(argv[3], "r"); | |
} | |
struct line *lines = malloc(MAX_LINES * sizeof(*lines)); | |
struct line *fillptr, *left, *right_upper, *right_lower; | |
fillptr = left = right_upper = right_lower = lines; | |
while ( 5 == fscanf( input, "%ms %li %ms %li %ms\n", &fillptr->foo, &fillptr->derp, &fillptr->bar, &fillptr->herp, &fillptr->baz)) { | |
fillptr++; | |
} | |
// assumes lines are sorted by `herp`. | |
while ( left < fillptr) { | |
while (right_lower->herp < left->herp + LOWER && right_lower < fillptr) { | |
right_lower++; | |
} | |
while (right_upper->herp < left->herp + UPPER && right_upper < fillptr) { | |
right_upper++; | |
} | |
struct line *ptr = right_lower; | |
while (ptr < right_upper) { | |
printf("%s\t%li\t%s\t%li\t%s", left->foo, left->derp, left->bar, left->herp, left->baz); | |
printf("%s\t%li\t%s\t%li\t%s\n", ptr->foo, ptr->derp, ptr->bar, ptr->herp, ptr->baz); | |
ptr++; | |
} | |
free(left->foo); | |
free(left->bar); | |
free(left->baz); | |
left++; | |
} | |
free(lines); | |
fclose(input); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment