Run-length encoding/C: Difference between revisions

m
no edit summary
m (→‎Unbuffered - Generative: minor reformat of code and comment)
mNo edit summary
Line 14:
#include <string.h>
 
const char *input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
const char *output = "12W1B12W3B24W1B14W";
 
typedef void (*YIELDCHAR)(); /* yields via yield_init */
Line 45:
#define YIELDS(type) BOOL (*yield)(type) = yield_init
 
ITERATOR gen_char_seq (const char *s){
YIELDS(char);
fflush(stdout);
Line 79:
}
 
const char *zero2nine = "0123456789";
 
ITERATOR gen_decode (GENCHAR gen_char){
Line 94:
}
 
int main(){
/* iterate through input string */
printf("Encode input: ");
Line 108:
OD;
printf("\n");
return 0;
}</lang>
Output:
Line 185 ⟶ 186:
}</lang>
 
In the following codes, encoding and decoding are implementendimplemented as "filters" which compress/decompress standard input to standard output writing ASCII strings; they will work as long as the input has no ASCII digits in it, and the compressed/original ratio of a "single group" will be less than or equal to 1 as long as the ASCII decimal representation's length of the repeat counter will be shorter than the length of the "group". It should be so except in the case the group is a single isolated character, e.g. B gives 1B (one byte against two compressed bytes)
 
'''Encoding filter'''
Line 205 ⟶ 206:
}
printf("%d%c", i, cp);
return 0;
}</lang>
 
Line 218 ⟶ 220:
for(j=0; j < i; j++) printf("%c", c);
}
return 0;
}</lang>
 
Anonymous user