Created
October 23, 2017 06:23
-
-
Save voidlizard/dddf9ef4fbfa860da1ea2ebc030b6935 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define _GNU_SOURCE | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#define CHUNK_SIZE 40000000 | |
#define LINES_CHUNK_SIZE 100000 | |
#define BUF_SIZE 160000 | |
void dump(char *buffer, int size) { | |
while (size>0) { | |
int w = write(1, buffer, size); | |
buffer+=w; | |
size-=w; | |
} | |
} | |
static inline char * dumpstr(char *buffer, char *current, char *word) { | |
int capacity = buffer+BUF_SIZE-current; | |
while (*word) { | |
if (capacity == 0) { | |
dump(buffer, current - buffer); | |
current = buffer; | |
capacity = BUF_SIZE; | |
} | |
*current = *word; | |
current++; | |
word++; | |
capacity--; | |
} | |
return current; | |
} | |
int main() { | |
// INPUT reading | |
char *buf = malloc(CHUNK_SIZE); | |
int size = CHUNK_SIZE; | |
{ | |
char *s = buf; | |
int left = CHUNK_SIZE; | |
while(1){ | |
int n = read(0, s, left); | |
if(n == 0) break; | |
s += n; left -= n; | |
if(left == 0) { | |
int tmp = size; | |
size += CHUNK_SIZE; | |
left = CHUNK_SIZE; | |
buf=realloc(buf, size); | |
s = buf + tmp; | |
} | |
} | |
*s = '\n'; | |
} | |
char **w = NULL; | |
int k = 0; | |
{ | |
char *l_start = buf; | |
while(1){ | |
if (k % LINES_CHUNK_SIZE == 0) { | |
w = realloc(w, k+LINES_CHUNK_SIZE); | |
} | |
char *l_end = memchr(l_start, '\n', buf+size-l_end); | |
if (l_end == NULL) break; | |
w[k] = l_start; | |
*l_end = '\0'; | |
char *brk = rawmemchr(l_start, ' '); | |
l_start = l_end + 1; | |
if (*(l_end-1) == '\r') *(l_end-1) = '\0'; | |
*brk = '\0'; | |
w[k+1] = brk + 1; | |
k += 2; | |
} | |
} | |
char out_buf[BUF_SIZE]; | |
char *s = out_buf; | |
for(int i = 0; i < k; i+=2) | |
for(int j = 1; j < k; j+=2){ | |
s=dumpstr(out_buf,s,w[i]); | |
s=dumpstr(out_buf,s,w[j]); | |
s=dumpstr(out_buf,s,"\n"); | |
} | |
dump(out_buf, s - out_buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment