Four is the number of letters in the ...: Difference between revisions

C - use GLib
(C - reduce memory usage)
(C - use GLib)
Line 51:
 
=={{header|C}}==
{{libheader|GLib}}
<lang c>#include <ctype.h>
#include <locale.h>
Line 56 ⟶ 57:
#include <stdio.h>
#include <stdint.h>
#include <stdlibglib.h>
#include <string.h>
 
typedef uint64_t integer;
Line 115:
}
return &named_numbers[names_len - 1];
 
void fatal(const char* message) {
fprintf(stderr, "%s\n", message);
exit(1);
}
 
void* xmalloc(size_t n) {
void* ptr = malloc(n);
if (ptr == NULL)
fatal("Out of memory");
return ptr;
}
 
void* xrealloc(void* p, size_t n) {
void* ptr = realloc(p, n);
if (ptr == NULL)
fatal("Out of memory");
return ptr;
}
 
typedef struct string_buffer_tag {
size_t size;
size_t capacity;
char* string;
} string_buffer;
 
void string_buffer_create(string_buffer* buffer, size_t capacity) {
buffer->size = 0;
buffer->capacity = capacity;
buffer->string = xmalloc(capacity);
}
 
void string_buffer_destroy(string_buffer* buffer) {
free(buffer->string);
buffer->string = NULL;
buffer->size = 0;
buffer->capacity = 0;
}
 
void string_buffer_clear(string_buffer* buffer) {
buffer->size = 0;
}
 
void string_buffer_append(string_buffer* buffer, const char* str, size_t len) {
size_t min_capacity = buffer->size + len + 1;
if (buffer->capacity < min_capacity) {
size_t new_capacity = 5*buffer->capacity/4;
if (new_capacity < min_capacity)
new_capacity = min_capacity;
buffer->string = xrealloc(buffer->string, new_capacity);
buffer->capacity = new_capacity;
}
memcpy(buffer->string + buffer->size, str, len + 1);
buffer->size += len;
}
 
Line 175 ⟶ 120:
size_t offset;
size_t length;
} wordword_t;
 
typedef struct word_list_tag {
size_tGArray* sizewords;
size_tGString* capacitystr;
word* words;
string_buffer str;
} word_list;
 
void word_list_create(word_list* words, size_t capacity) {
words->sizewords = 0g_array_new(FALSE, FALSE, sizeof(word_t));
words->capacitystr = capacityg_string_new(NULL);
words->words = xmalloc(capacity * sizeof(word));
string_buffer_create(&words->str, capacity*8);
}
 
void word_list_destroy(word_list* words) {
string_buffer_destroyg_string_free(&words->str, TRUE);
freeg_array_free(words->words, TRUE);
words->words = NULL;
words->size = 0;
words->capacity = 0;
}
 
void word_list_clear(word_list* words) {
string_buffer_clearg_string_truncate(&words->str, 0);
g_array_set_size(words->size =words, 0);
}
 
void word_list_append(word_list* words, const char* str) {
size_t offset = words->str.size->len;
size_t len = strlen(str);
string_buffer_appendg_string_append_len(&words->str, str, len);
word_t word;
size_t min_capacity = words->size + 1;
w->word.offset = offset;
if (words->capacity < min_capacity) {
buffer->sizeword.length += len;
size_t new_capacity = (words->capacity * 5)/4;
g_array_append_val(words->words, = NULLword);
if (new_capacity < min_capacity)
new_capacity = min_capacity;
 
words->words = xrealloc(words->words, new_capacity * sizeof(word));
word_t* word_list_get(word_list* words, size_t index) {
words->capacity = new_capacity;
return &g_array_index(words->words, word_t, index);
}
word* w = &words->words[words->size++];
w->offset = offset;
w->length = len;
}
 
void word_list_extend(word_list* words, const char* str) {
wordword_t* wword = &word_list_get(words, words->words[words->sizelen - 1]);
size_t len = strlen(str);
wword->length += len;
string_buffer_appendg_string_append_len(&words->str, str, len);
}
 
Line 258 ⟶ 193:
}
 
size_t count_letters(const word_list* words, size_t index) {
const wordword_t* wword = &word_list_get(words->words[, index]);
size_t letters = 0;
const char* s = words->str.string->str + wword->offset;
for (size_t i = 0, n = wword->length; i < n; ++i) {
if (isalpha((unsigned char)s[i]))
++letters;
Line 290 ⟶ 225:
 
size_t sentence_length(const word_list* words) {
size_t n = words->sizewords->len;
if (n == 0)
return 0;
return words->str.size->len + n - 1;
}
 
Line 300 ⟶ 235:
size_t n = 201;
word_list result = { 0 };
word_list_create(&result, 1024);
sentence(&result, n);
printf("Number of letters in first %'lu words in the sequence:\n", n);
Line 311 ⟶ 246:
for (n = 1000; n <= 10000000; n *= 10) {
sentence(&result, n);
const wordword_t* wword = word_list_get(&result.words[, n - 1]);
const char* s = result.str.string->str + wword->offset;
printf("The %'luth word is '%.*s' and has %lu letters. ", n, (int)w->length, s,
(int)word->length, s, count_letters(&result, n - 1));
printf("Sentence length: %'lu\n" , sentence_length(&result));
}
1,777

edits