diff --git a/src/runtime/c/Makefile.am b/src/runtime/c/Makefile.am index 8fcbe847f..daf178aea 100644 --- a/src/runtime/c/Makefile.am +++ b/src/runtime/c/Makefile.am @@ -20,7 +20,6 @@ guinclude_HEADERS = \ gu/fun.h \ gu/hash.h \ gu/in.h \ - gu/list.h \ gu/map.h \ gu/mem.h \ gu/out.h \ @@ -57,7 +56,6 @@ libgu_la_SOURCES = \ gu/fun.c \ gu/hash.c \ gu/in.c \ - gu/list.c \ gu/map.c \ gu/mem.c \ gu/out.c \ diff --git a/src/runtime/c/gu/list.c b/src/runtime/c/gu/list.c deleted file mode 100644 index d98a42e41..000000000 --- a/src/runtime/c/gu/list.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2010 University of Helsinki. - * - * This file is part of libgu. - * - * Libgu is free software: you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Libgu is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with libgu. If not, see . - */ - -#include -#include -#include - -static const int gu_list_empty = 0; - -void* gu_list_alloc(GuPool* pool, size_t base_size, size_t elem_size, - int n_elems, size_t alignment) -{ - gu_assert(n_elems >= 0); - if (n_elems == 0) { - return (void*) &gu_list_empty; - } - // XXX: use gu_flex_size, use offset of elems - void* p = gu_malloc_aligned(pool, base_size + elem_size * n_elems, - alignment); - *(int*) p = n_elems; - return p; -} - - -GU_DEFINE_KIND(GuList, abstract); - -// GU_DEFINE_TYPE(GuStrs, GuList, gu_type(GuStr)); -// GU_DEFINE_TYPE(GuStrsP, pointer, gu_type(GuStrs)); - -void* -gu_list_type_alloc(GuListType* ltype, int n_elems, GuPool* pool) -{ - return gu_list_alloc(pool, ltype->size, - gu_type_size(ltype->elem_type), - n_elems, ltype->align); -} - -void* -gu_list_type_index(GuListType* ltype, void* list, int i) -{ - uint8_t* p = list; - return &p[ltype->elems_offset + i * gu_type_size(ltype->elem_type)]; -} diff --git a/src/runtime/c/gu/list.h b/src/runtime/c/gu/list.h deleted file mode 100644 index f2add157f..000000000 --- a/src/runtime/c/gu/list.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2010 University of Helsinki. - * - * This file is part of libgu. - * - * Libgu is free software: you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Libgu is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with libgu. If not, see . - */ - -/** @file - * - * Lists. - */ - -#ifndef GU_LIST_H_ -#define GU_LIST_H_ - -#include - - -#define GuList(t) \ - struct { \ - const int len; \ - t elems[]; \ - } - -void* gu_list_alloc(GuPool* pool, size_t base_size, size_t elem_size, - int n_elems, size_t alignment); - -#define gu_new_list(t, pool, n) \ - ((t*) gu_list_alloc(pool, \ - sizeof(t), \ - sizeof(((t*)NULL)->elems[0]), \ - (n), \ - gu_flex_alignof(t))) - -static inline int -gu_list_length(const void* list) -{ - return *(const int*) list; -} - -#define gu_list_elems(lst) \ - ((lst)->elems) - -#define gu_list_index(lst, i) \ - (gu_list_elems(lst)[i]) - -typedef GuList(void*) GuPointers; -//typedef GuList(uint8_t) GuBytes; - -typedef GuList(int) GuInts; - - -#define GuListN(t_, len_) \ - struct { \ - int len; \ - t elems[len_]; \ - } - -#define gu_list_(qual_, t_, ...) \ - ((qual_ GuList(t_) *) \ - ((qual_ GuListN(t_, (sizeof((t_[]){__VA_ARGS__}) / sizeof(t_)))[]){ \ - __VA_ARGS__ \ - })) - -#define gu_list(t_, ...) \ - gu_list_(, t_, __VA_ARGS__) - -#define gu_clist(t_, ...) \ - gu_list_(const, t_, __VA_ARGS__) - -#define GuSList(t) \ - const struct { \ - int len; \ - t* elems; \ - } - -#define GU_SLIST_0 { .len = 0, .elems = NULL } - -#define GU_SLIST(t, ...) \ - { \ - .len = GU_ARRAY_LEN(t,GU_ID({__VA_ARGS__})), \ - .elems = ((t[]){__VA_ARGS__}) \ - } - - -#include - -// -// list -// - -typedef const struct GuListType GuListType, GuType_GuList; - -struct GuListType { - GuType_abstract abstract_base; - size_t size; - size_t align; - GuType* elem_type; - ptrdiff_t elems_offset; -}; - -#define GU_TYPE_INIT_GuList(k_, t_, elem_type_) { \ - .abstract_base = GU_TYPE_INIT_abstract(k_, t_, _), \ - .size = sizeof(t_), \ - .align = gu_alignof(t_), \ - .elem_type = elem_type_, \ - .elems_offset = offsetof(t_, elems) \ -} - -extern GU_DECLARE_KIND(GuList); - -void* -gu_list_type_alloc(GuListType* ltype, int n_elems, GuPool* pool); - -void* -gu_list_type_index(GuListType* ltype, void* list, int i); - -#include - - -typedef GuList(GuStr) GuStrs; -typedef GuStrs* GuStrsP; - -extern GU_DECLARE_TYPE(GuStrs, GuList); -extern GU_DECLARE_TYPE(GuStrsP, pointer); - - -#endif // GU_LIST_H_ diff --git a/src/runtime/c/gu/out.c b/src/runtime/c/gu/out.c index 36d523c44..439a3de72 100644 --- a/src/runtime/c/gu/out.c +++ b/src/runtime/c/gu/out.c @@ -1,6 +1,7 @@ #include #include #include +#include static bool gu_out_is_buffering(GuOut* out) diff --git a/src/runtime/c/gu/seq.c b/src/runtime/c/gu/seq.c index 5f5532013..cb6de42c9 100644 --- a/src/runtime/c/gu/seq.c +++ b/src/runtime/c/gu/seq.c @@ -1,35 +1,27 @@ #include #include #include +#include #include #include #include +struct GuSeq { + size_t len; + uint8_t data[0]; +}; struct GuBuf { - uint8_t* data; + GuSeq* seq; size_t elem_size; size_t avail_len; GuFinalizer fin; }; -GuBuf* -gu_seq_buf(GuSeq seq) -{ - gu_require(gu_tagged_tag(seq.w_) == 0); - return gu_word_ptr(seq.w_); -} - -GuSeq -gu_buf_seq(GuBuf* buf) -{ - return (GuSeq) { .w_ = gu_ptr_word(buf) }; -} - size_t -gu_buf_length(GuBuf* dyn) +gu_buf_length(GuBuf* buf) { - return (size_t)(((GuWord*)(void*)dyn)[-1] >> 1); + return buf->seq->len; } size_t @@ -38,58 +30,82 @@ gu_buf_avail(GuBuf* buf) return buf->avail_len; } - -static void -gu_buf_set_length(GuBuf* dyn, size_t new_len) -{ - ((GuWord*)(void*)dyn)[-1] = ((GuWord) new_len) << 1 | 0x1; -} - static void gu_buf_fini(GuFinalizer* fin) { GuBuf* buf = gu_container(fin, GuBuf, fin); - gu_mem_buf_free(buf->data); + if (buf->avail_len > 0) + gu_mem_buf_free(buf->seq); } GuBuf* gu_make_buf(size_t elem_size, GuPool* pool) { - GuBuf* buf = gu_new_prefixed(unsigned, GuBuf, pool); - gu_buf_set_length(buf, 0); + GuBuf* buf = gu_new(GuBuf, pool); + buf->seq = gu_empty_seq(); buf->elem_size = elem_size; - buf->data = NULL; buf->avail_len = 0; buf->fin.fn = gu_buf_fini; gu_pool_finally(pool, &buf->fin); - gu_buf_set_length(buf, 0); return buf; } -static const GuWord gu_empty_seq_[2] = {0, 0}; - -GuSeq -gu_empty_seq() { - return (GuSeq) { gu_tagged((void*)&gu_empty_seq_[1], 0) }; +size_t +gu_seq_length(GuSeq* seq) +{ + return seq->len; } -GuSeq +void* +gu_seq_data(GuSeq* seq) +{ + return seq->data; +} + +static GuSeq gu_empty_seq_ = {0}; + +GuSeq* +gu_empty_seq() { + return &gu_empty_seq_; +} + +GuSeq* gu_make_seq(size_t elem_size, size_t length, GuPool* pool) { - size_t size = elem_size * length; - if (0 < length && length <= GU_TAG_MAX) { - void* buf = gu_malloc(pool, size); - return (GuSeq) { gu_tagged(buf, length) }; - } else if (size == 0) { + GuSeq* seq = gu_malloc(pool, sizeof(GuSeq) + elem_size * length); + seq->len = length; + return seq; +} + +GuSeq* +gu_alloc_seq_(size_t elem_size, size_t length) +{ + if (length == 0) return gu_empty_seq(); - } else { - void* buf = gu_malloc_prefixed(pool, - gu_alignof(GuWord), - sizeof(GuWord), - 0, size); - ((GuWord*) buf)[-1] = ((GuWord) length) << 1; - return (GuSeq) { gu_tagged(buf, 0) }; - } + + size_t real_size; + GuSeq* seq = gu_mem_buf_alloc(sizeof(GuSeq) + elem_size * length, &real_size); + seq->len = (real_size - sizeof(GuSeq)) / elem_size; + return seq; +} + +GuSeq* +gu_realloc_seq_(GuSeq* seq, size_t elem_size, size_t length) +{ + size_t real_size; + GuSeq* new_seq = (seq == NULL || seq == gu_empty_seq()) ? + gu_mem_buf_alloc(sizeof(GuSeq) + elem_size * length, &real_size) : + gu_mem_buf_realloc(seq, sizeof(GuSeq) + elem_size * length, &real_size); + new_seq->len = (real_size - sizeof(GuSeq)) / elem_size; + return new_seq; +} + +void +gu_seq_free(GuSeq* seq) +{ + if (seq == NULL || seq == gu_empty_seq()) + return; + gu_mem_buf_free(seq); } static void @@ -98,17 +114,30 @@ gu_buf_require(GuBuf* buf, size_t req_len) if (req_len <= buf->avail_len) { return; } - size_t req_size = buf->elem_size * req_len; + + size_t req_size = sizeof(GuSeq) + buf->elem_size * req_len; size_t real_size; - buf->data = gu_mem_buf_realloc(buf->data, req_size, - &real_size); - buf->avail_len = real_size / buf->elem_size; + + if (buf->seq == NULL || buf->seq == gu_empty_seq()) { + buf->seq = gu_mem_buf_alloc(req_size, &real_size); + buf->seq->len = 0; + } else { + buf->seq = gu_mem_buf_realloc(buf->seq, req_size, &real_size); + } + + buf->avail_len = (real_size - sizeof(GuSeq)) / buf->elem_size; } void* gu_buf_data(GuBuf* buf) { - return buf->data; + return &buf->seq->data; +} + +GuSeq* +gu_buf_data_seq(GuBuf* buf) +{ + return buf->seq; } void* @@ -117,8 +146,8 @@ gu_buf_extend_n(GuBuf* buf, size_t n_elems) size_t len = gu_buf_length(buf); size_t new_len = len + n_elems; gu_buf_require(buf, new_len); - gu_buf_set_length(buf, new_len); - return &buf->data[buf->elem_size * len]; + buf->seq->len = new_len; + return &buf->seq->data[buf->elem_size * len]; } void* @@ -130,7 +159,6 @@ gu_buf_extend(GuBuf* buf) void gu_buf_push_n(GuBuf* buf, const void* data, size_t n_elems) { - void* p = gu_buf_extend_n(buf, n_elems); memcpy(p, data, buf->elem_size * n_elems); } @@ -140,8 +168,8 @@ gu_buf_trim_n(GuBuf* buf, size_t n_elems) { gu_require(n_elems <= gu_buf_length(buf)); size_t new_len = gu_buf_length(buf) - n_elems; - gu_buf_set_length(buf, new_len); - return &buf->data[buf->elem_size * new_len]; + buf->seq->len = new_len; + return &buf->seq->data[buf->elem_size * new_len]; } const void* @@ -153,7 +181,7 @@ gu_buf_trim(GuBuf* buf) void gu_buf_flush(GuBuf* buf) { - gu_buf_set_length(buf, 0); + buf->seq->len = 0; } void @@ -163,11 +191,11 @@ gu_buf_pop_n(GuBuf* buf, size_t n_elems, void* data_out) memcpy(data_out, p, buf->elem_size * n_elems); } -GuSeq +GuSeq* gu_buf_freeze(GuBuf* buf, GuPool* pool) { size_t len = gu_buf_length(buf); - GuSeq seq = gu_make_seq(buf->elem_size, len, pool); + GuSeq* seq = gu_make_seq(buf->elem_size, len, pool); void* bufdata = gu_buf_data(buf); void* seqdata = gu_seq_data(seq); memcpy(seqdata, bufdata, buf->elem_size * len); @@ -182,32 +210,32 @@ gu_quick_sort(GuBuf *buf, GuOrder *order, int left, int right) void* pivot = alloca(buf->elem_size); memcpy(pivot, - &buf->data[buf->elem_size * left], + &buf->seq->data[buf->elem_size * left], buf->elem_size); while (left < right) { - while ((order->compare(order, &buf->data[buf->elem_size * right], pivot) >= 0) && (left < right)) + while ((order->compare(order, &buf->seq->data[buf->elem_size * right], pivot) >= 0) && (left < right)) right--; if (left != right) { - memcpy(&buf->data[buf->elem_size * left], - &buf->data[buf->elem_size * right], + memcpy(&buf->seq->data[buf->elem_size * left], + &buf->seq->data[buf->elem_size * right], buf->elem_size); left++; } - while ((order->compare(order, &buf->data[buf->elem_size * left], pivot) <= 0) && (left < right)) + while ((order->compare(order, &buf->seq->data[buf->elem_size * left], pivot) <= 0) && (left < right)) left++; if (left != right) { - memcpy(&buf->data[buf->elem_size * right], - &buf->data[buf->elem_size * left], + memcpy(&buf->seq->data[buf->elem_size * right], + &buf->seq->data[buf->elem_size * left], buf->elem_size); right--; } } - memcpy(&buf->data[buf->elem_size * left], + memcpy(&buf->seq->data[buf->elem_size * left], pivot, buf->elem_size); int index = left; @@ -235,7 +263,7 @@ gu_buf_binsearch(GuBuf *buf, GuOrder *order, void *value) while (i <= j) { size_t k = (i+j) / 2; - int cmp = order->compare(order, value, &buf->data[buf->elem_size * k]); + int cmp = order->compare(order, value, &buf->seq->data[buf->elem_size * k]); if (cmp < 0) { j = k-1; @@ -243,7 +271,7 @@ gu_buf_binsearch(GuBuf *buf, GuOrder *order, void *value) i = k+1; } else { memcpy(value, - &buf->data[buf->elem_size * k], + &buf->seq->data[buf->elem_size * k], buf->elem_size); return true; } @@ -258,16 +286,16 @@ gu_heap_siftdown(GuBuf *buf, GuOrder *order, { while (pos > startpos) { int parentpos = (pos - 1) >> 1; - void *parent = &buf->data[buf->elem_size * parentpos]; + void *parent = &buf->seq->data[buf->elem_size * parentpos]; if (order->compare(order, value, parent) >= 0) break; - memcpy(&buf->data[buf->elem_size * pos], parent, buf->elem_size); + memcpy(&buf->seq->data[buf->elem_size * pos], parent, buf->elem_size); pos = parentpos; } - memcpy(&buf->data[buf->elem_size * pos], value, buf->elem_size); + memcpy(&buf->seq->data[buf->elem_size * pos], value, buf->elem_size); } static void @@ -282,13 +310,13 @@ gu_heap_siftup(GuBuf *buf, GuOrder *order, int rightpos = childpos + 1; if (rightpos < endpos && order->compare(order, - &buf->data[buf->elem_size * childpos], - &buf->data[buf->elem_size * rightpos]) >= 0) { + &buf->seq->data[buf->elem_size * childpos], + &buf->seq->data[buf->elem_size * rightpos]) >= 0) { childpos = rightpos; } - memcpy(&buf->data[buf->elem_size * pos], - &buf->data[buf->elem_size * childpos], buf->elem_size); + memcpy(&buf->seq->data[buf->elem_size * pos], + &buf->seq->data[buf->elem_size * childpos], buf->elem_size); pos = childpos; childpos = 2*pos + 1; } @@ -309,7 +337,7 @@ gu_buf_heap_pop(GuBuf *buf, GuOrder *order, void* data_out) const void* last = gu_buf_trim(buf); // raises an error if empty if (gu_buf_length(buf) > 0) { - memcpy(data_out, buf->data, buf->elem_size); + memcpy(data_out, buf->seq->data, buf->elem_size); gu_heap_siftup(buf, order, last, 0); } else { memcpy(data_out, last, buf->elem_size); @@ -321,7 +349,7 @@ gu_buf_heap_replace(GuBuf *buf, GuOrder *order, void *value, void *data_out) { gu_require(gu_buf_length(buf) > 0); - memcpy(data_out, buf->data, buf->elem_size); + memcpy(data_out, buf->seq->data, buf->elem_size); gu_heap_siftup(buf, order, value, 0); } @@ -332,7 +360,7 @@ gu_buf_heapify(GuBuf *buf, GuOrder *order) void *value = alloca(buf->elem_size); for (size_t i = 0; i < middle; i++) { - memcpy(value, &buf->data[buf->elem_size * i], buf->elem_size); + memcpy(value, &buf->seq->data[buf->elem_size * i], buf->elem_size); gu_heap_siftup(buf, order, value, i); } } @@ -370,7 +398,7 @@ gu_buf_outbuf_begin(GuOutStream* stream, size_t req, size_t* sz_out, GuExn* err) size_t avail = buf->avail_len; gu_assert(len < avail); *sz_out = esz * (avail - len); - return &buf->data[len * esz]; + return &buf->seq->data[len * esz]; } static void @@ -383,7 +411,7 @@ gu_buf_outbuf_end(GuOutStream* stream, size_t sz, GuExn* err) size_t elem_size = buf->elem_size; gu_require(sz % elem_size == 0); gu_require(sz < elem_size * (len - buf->avail_len)); - gu_buf_set_length(buf, len + (sz / elem_size)); + buf->seq->len = len + (sz / elem_size); } GuOut* @@ -398,23 +426,16 @@ gu_buf_out(GuBuf* buf, GuPool* pool) return gu_new_out(&bout->stream, pool); } -const GuSeq -gu_null_seq = GU_NULL_SEQ; - - #include GU_DEFINE_KIND(GuSeq, GuOpaque); GU_DEFINE_KIND(GuBuf, abstract); -GU_DEFINE_TYPE(GuChars, GuSeq, gu_type(char)); -GU_DEFINE_TYPE(GuBytes, GuSeq, gu_type(uint8_t)); - char* -gu_chars_str(GuChars chars, GuPool* pool) +gu_char_buf_str(GuCharBuf* chars, GuPool* pool) { - size_t len = gu_seq_length(chars); - char* data = gu_seq_data(chars); + size_t len = gu_buf_length(chars); + char* data = gu_buf_data(chars); char* str = gu_new_str(len, pool); memcpy(str, data, len); return str; diff --git a/src/runtime/c/gu/seq.h b/src/runtime/c/gu/seq.h index 41f99ee28..9af95bed8 100644 --- a/src/runtime/c/gu/seq.h +++ b/src/runtime/c/gu/seq.h @@ -2,54 +2,40 @@ #define GU_SEQ_H_ #include -#include - typedef struct GuBuf GuBuf; -typedef GuOpaque() GuSeq; +typedef struct GuSeq GuSeq; -GuSeq +GuSeq* gu_empty_seq(); -GuSeq +GuSeq* gu_make_seq(size_t elem_size, size_t len, GuPool* pool); #define gu_new_seq(T, N, POOL) \ gu_make_seq(sizeof(T), (N), (POOL)) -static inline size_t -gu_seq_length(GuSeq seq) -{ - GuWord w = seq.w_; - size_t tag = gu_tagged_tag(w); - if (tag == 0) { - GuWord* p = gu_tagged_ptr(w); - return (size_t) (p[-1] >> 1); - } - return tag; -} +GuSeq* +gu_alloc_seq_(size_t elem_size, size_t length); -static inline void* -gu_seq_data(GuSeq seq) -{ - GuWord w = seq.w_; - int tag = gu_tagged_tag(w); - void* ptr = gu_tagged_ptr(w); - if (tag == 0) { - GuWord* p = ptr; - if (p[-1] & 0x1) { - return *(uint8_t**) ptr; - } - } - return ptr; -} +#define gu_alloc_seq(T, N) \ + gu_alloc_seq_(sizeof(T), (N)) -static inline bool -gu_seq_is_null(GuSeq seq) -{ - return (gu_tagged_ptr(seq.w_)) == NULL; -} +GuSeq* +gu_realloc_seq_(GuSeq* seq, size_t elem_size, size_t length); + +#define gu_realloc_seq(S, T, N) \ + gu_realloc_seq_(S, sizeof(T), (N)) + +void +gu_seq_free(GuSeq* seq); + +size_t +gu_seq_length(GuSeq* seq); + +void* +gu_seq_data(GuSeq* seq); #define gu_seq_index(SEQ, T, I) \ @@ -64,14 +50,6 @@ gu_seq_is_null(GuSeq seq) GU_END - - -GuBuf* -gu_seq_buf(GuSeq seq); - -GuSeq -gu_buf_seq(GuBuf* buf); - GuBuf* gu_make_buf(size_t elem_size, GuPool* pool); @@ -87,6 +65,9 @@ gu_buf_avail(GuBuf* buf); void* gu_buf_data(GuBuf* buf); +GuSeq* +gu_buf_data_seq(GuBuf* buf); + #define gu_buf_index(BUF, T, I) \ (&((T*)gu_buf_data(BUF))[I]) @@ -149,31 +130,14 @@ gu_buf_heap_replace(GuBuf *buf, GuOrder *order, void *value, void *data_out); void gu_buf_heapify(GuBuf *buf, GuOrder *order); -#if 0 -void -gu_buf_resize_head(GuBuf* buf, ptrdiff_t change); - -void -gu_buf_unshift(GuBuf* buf, const void* data, size_t size); - -void -gu_buf_shift(GuBuf* buf, size_t size, void* data_out); -#endif - -GuSeq +GuSeq* gu_buf_freeze(GuBuf* buf, GuPool* pool); -extern const GuSeq gu_null_seq; - -#define GU_NULL_SEQ { .w_ = (GuWord)(void*)NULL } - -typedef GuSeq GuChars; -typedef GuSeq GuBytes; typedef GuBuf GuCharBuf; typedef GuBuf GuByteBuf; char* -gu_chars_str(GuChars chars, GuPool* pool); +gu_char_buf_str(GuCharBuf* chars, GuPool* pool); #endif // GU_SEQ_H_ @@ -216,8 +180,5 @@ struct GuBufType { .elem_type = ELEM_T \ } -extern GU_DECLARE_TYPE(GuChars, GuSeq); -extern GU_DECLARE_TYPE(GuBytes, GuSeq); - #endif diff --git a/src/runtime/c/gu/string.c b/src/runtime/c/gu/string.c index c8b443daf..26b4f1f80 100644 --- a/src/runtime/c/gu/string.c +++ b/src/runtime/c/gu/string.c @@ -504,5 +504,4 @@ GuHasher gu_string_hasher[1] = { GU_DEFINE_TYPE(GuString, GuOpaque, _); -GU_DEFINE_TYPE(GuStrings, GuSeq, gu_type(GuString)); GU_DEFINE_KIND(GuStringMap, GuMap); diff --git a/src/runtime/c/gu/string.h b/src/runtime/c/gu/string.h index a24fe3068..4d309d20b 100644 --- a/src/runtime/c/gu/string.h +++ b/src/runtime/c/gu/string.h @@ -103,11 +103,6 @@ gu_string_cmp(GuString s1, GuString s2); extern GU_DECLARE_TYPE(GuString, GuOpaque); # endif -# if defined(GU_SEQ_H_) && !defined(GU_STRING_H_SEQ_TYPE_) -# define GU_STRING_H_SEQ_TYPE_ -extern GU_DECLARE_TYPE(GuStrings, GuSeq); -# endif - # if defined(GU_MAP_H_TYPE_) && !defined(GU_STRING_H_MAP_TYPE_) # define GU_STRING_H_MAP_TYPE_ diff --git a/src/runtime/c/gu/type.h b/src/runtime/c/gu/type.h index 777b2e1f9..b7d0e6dc9 100644 --- a/src/runtime/c/gu/type.h +++ b/src/runtime/c/gu/type.h @@ -201,7 +201,7 @@ typedef GuType_alias GuType_referenced; -#include +#include // // struct @@ -221,7 +221,10 @@ struct GuMember { struct GuStructRepr { GuType_repr repr_base; const char* name; - GuSList(GuMember) members; + struct { + int len; + GuMember* elems; + } members; }; extern GU_DECLARE_KIND(struct); @@ -259,7 +262,10 @@ extern GU_DECLARE_KIND(struct); #define GU_TYPE_INIT_struct(k_, t_, ...) { \ .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ .name = #t_, \ - .members = GU_SLIST(GuMember, __VA_ARGS__) \ + .members = { \ + .len = GU_ARRAY_LEN(GuMember,GU_ID({__VA_ARGS__})), \ + .elems = ((GuMember[]){__VA_ARGS__}) \ + } \ } bool @@ -367,7 +373,10 @@ typedef const struct GuEnumType GuEnumType, GuType_enum; struct GuEnumType { GuType_repr repr_base; - GuSList(GuEnumConstant) constants; + struct { + int len; + GuEnumConstant* elems; + } constants; }; #define GU_ENUM_C(t_, x) { \ @@ -378,7 +387,10 @@ struct GuEnumType { #define GU_TYPE_INIT_enum(k_, t_, ...) { \ .repr_base = GU_TYPE_INIT_repr(k_, t_, _), \ - .constants = GU_SLIST(GuEnumConstant, __VA_ARGS__) \ + .constants = { \ + .len = GU_ARRAY_LEN(GuEnumConstant,GU_ID({__VA_ARGS__})), \ + .elems = ((GuEnumConstant[]){__VA_ARGS__}) \ + } \ } GuEnumConstant* @@ -402,14 +414,22 @@ struct GuTypeTableEntry { typedef const struct GuTypeTable GuTypeTable; struct GuTypeTable { - GuSList(const GuTypeTable*) parents; - GuSList(GuTypeTableEntry) entries; + struct { + int len; + GuTypeTable** elems; + } parents; + struct { + int len; + GuTypeTableEntry* elems; + } entries; }; #define GU_TYPETABLE(parents_, ...) { \ .parents = parents_, \ - .entries = GU_SLIST(GuTypeTableEntry, \ - __VA_ARGS__) \ + .entries = { \ + .len = GU_ARRAY_LEN(GuTypeTableEntry,GU_ID({__VA_ARGS__})), \ + .elems = ((GuTypeTableEntry[]){__VA_ARGS__}) \ + } \ } typedef struct GuTypeMap GuTypeMap; diff --git a/src/runtime/c/gu/variant.h b/src/runtime/c/gu/variant.h index a9bb10a4a..ab1265ff1 100644 --- a/src/runtime/c/gu/variant.h +++ b/src/runtime/c/gu/variant.h @@ -148,7 +148,10 @@ struct GuConstructor { -typedef GuSList(GuConstructor) GuConstructors; +typedef struct { + int len; + GuConstructor* elems; +} GuConstructors; typedef const struct GuVariantType GuVariantType, GuType_GuVariant; @@ -159,7 +162,10 @@ struct GuVariantType { #define GU_TYPE_INIT_GuVariant(k_, t_, ...) { \ .repr_base = GU_TYPE_INIT_repr(k_, GuVariant, _), \ - .ctors = GU_SLIST(GuConstructor, __VA_ARGS__) \ + .ctors = { \ + .len = GU_ARRAY_LEN(GuConstructor,GU_ID({__VA_ARGS__})), \ + .elems = ((GuConstructor[]){__VA_ARGS__}) \ + } \ } extern GU_DECLARE_KIND(GuVariant); diff --git a/src/runtime/c/pgf/data.c b/src/runtime/c/pgf/data.c index 0146d6ad6..905857e1b 100644 --- a/src/runtime/c/pgf/data.c +++ b/src/runtime/c/pgf/data.c @@ -5,7 +5,7 @@ #include bool -pgf_tokens_equal(PgfTokens t1, PgfTokens t2) +pgf_tokens_equal(PgfTokens* t1, PgfTokens* t2) { size_t len1 = gu_seq_length(t1); size_t len2 = gu_seq_length(t2); @@ -36,7 +36,8 @@ GU_DEFINE_TYPE(PgfDummyVariant, GuVariant); GU_DEFINE_TYPE(PgfFlags, GuStringMap, gu_type(PgfDummyVariant), &gu_null_variant); -GU_DEFINE_TYPE(PgfProductionSeq, GuSeq, gu_type(PgfDummyVariant)); +GU_DEFINE_TYPE(PgfProductionSeq, abstract); +GU_DEFINE_TYPE(PgfProductionBuf, abstract); GU_DEFINE_TYPE(PgfAbsFun, abstract); diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index ef5d1a780..7717f89f9 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -1,7 +1,6 @@ #ifndef PGF_DATA_H_ #define PGF_DATA_H_ -#include #include #include #include @@ -12,7 +11,7 @@ typedef struct PgfCCat PgfCCat; extern GU_DECLARE_TYPE(PgfCCat, abstract); -typedef GuList(PgfCCat*) PgfCCats; +typedef GuSeq PgfCCats; #define PgfCIdMap GuStringMap typedef PgfCIdMap PgfFlags; // PgfCId -> PgfLiteral @@ -74,7 +73,7 @@ typedef struct { PgfCId name; PgfType* type; int arity; - PgfEquations defns; // maybe null + PgfEquations* defns; // maybe null PgfExprProb ep; void* predicate; } PgfAbsFun; @@ -86,7 +85,7 @@ extern GU_DECLARE_TYPE(PgfMetaChildMap, GuMap); typedef struct { PgfCId name; - PgfHypos context; + PgfHypos* context; prob_t meta_prob; prob_t meta_token_prob; @@ -130,15 +129,13 @@ typedef struct { extern GU_DECLARE_TYPE(PgfCncCat, abstract); bool -pgf_tokens_equal(PgfTokens t1, PgfTokens t2); - -typedef GuList(GuString) GuStringL; +pgf_tokens_equal(PgfTokens* t1, PgfTokens* t2); typedef struct { - PgfTokens form; + PgfTokens* form; /**< The form of this variant as a list of tokens. */ - GuStringL* prefixes; + GuStrings* prefixes; /**< The prefixes of the following symbol that trigger this * form. */ } PgfAlternative; @@ -189,14 +186,14 @@ typedef struct { typedef PgfSymbolIdx PgfSymbolCat, PgfSymbolLit, PgfSymbolVar; typedef struct { - PgfTokens tokens; + PgfTokens* tokens; } PgfSymbolKS; typedef struct PgfSymbolKP /** A prefix-dependent symbol. The form that this symbol takes * depends on the form of a prefix of the following symbol. */ { - PgfTokens default_form; + PgfTokens* default_form; /**< Default form that this symbol takes if none of of the * variant forms is triggered. */ @@ -210,17 +207,17 @@ typedef struct { } PgfSymbolNE; typedef GuSeq PgfSequence; // -> PgfSymbol -typedef GuList(PgfSequence) PgfSequences; +typedef GuSeq PgfSequences; typedef struct { PgfAbsFun* absfun; PgfExprProb *ep; int funid; GuLength n_lins; - PgfSequence lins[]; + PgfSequence* lins[]; } PgfCncFun; -typedef GuList(PgfCncFun*) PgfCncFuns; +typedef GuSeq PgfCncFuns; struct PgfConcr { PgfCId name; @@ -261,7 +258,7 @@ typedef GuSeq PgfPArgs; typedef struct { PgfCncFun* fun; - PgfPArgs args; + PgfPArgs* args; } PgfProductionApply; typedef struct PgfProductionCoerce @@ -275,26 +272,30 @@ typedef struct PgfProductionCoerce typedef struct { PgfLiteralCallback *callback; PgfExprProb *ep; - GuSeq lins; + GuSeq* lins; } PgfProductionExtern; typedef struct { PgfExprProb *ep; - PgfPArgs args; + PgfPArgs* args; } PgfProductionMeta; -typedef GuSeq PgfProductionSeq; -extern GU_DECLARE_TYPE(PgfProductionSeq, GuSeq); +typedef GuSeq PgfProductionSeq; +extern GU_DECLARE_TYPE(PgfProductionSeq, abstract); + +typedef GuBuf PgfProductionBuf; +extern GU_DECLARE_TYPE(PgfProductionBuf, abstract); struct PgfCCat { PgfCncCat* cnccat; PgfCncFuns* lindefs; size_t n_synprods; - PgfProductionSeq prods; + PgfProductionSeq* prods; float viterbi_prob; int fid; PgfItemConts* conts; struct PgfAnswers* answers; + GuFinalizer fin[0]; }; #endif diff --git a/src/runtime/c/pgf/expr.c b/src/runtime/c/pgf/expr.c index 4b2644931..d7f23c207 100644 --- a/src/runtime/c/pgf/expr.c +++ b/src/runtime/c/pgf/expr.c @@ -91,20 +91,12 @@ GU_DEFINE_TYPE(PgfHypo, struct, GU_MEMBER(PgfHypo, cid, PgfCId), GU_MEMBER_P(PgfHypo, type, PgfType)); -GU_DEFINE_TYPE(PgfHypos, GuSeq, gu_type(PgfHypo)); - -GU_DEFINE_TYPE(PgfType, struct, - GU_MEMBER(PgfType, hypos, PgfHypos), - GU_MEMBER(PgfType, cid, PgfCId), - GU_MEMBER(PgfType, n_exprs, GuLength), - GU_FLEX_MEMBER(PgfType, exprs, PgfExpr)); - GU_DEFINE_TYPE( PgfExpr, GuVariant, GU_CONSTRUCTOR_S( PGF_EXPR_ABS, PgfExprAbs, GU_MEMBER(PgfExprAbs, bind_type, PgfBindType), - GU_MEMBER(PgfExprAbs, id, GuStr), + GU_MEMBER(PgfExprAbs, id, GuString), GU_MEMBER(PgfExprAbs, body, PgfExpr)), GU_CONSTRUCTOR_S( PGF_EXPR_APP, PgfExprApp, @@ -160,7 +152,7 @@ struct PgfExprParser { GuPool* expr_pool; GuPool* tmp_pool; PGF_TOKEN_TAG token_tag; - GuChars token_value; + GuCharBuf* token_value; int ch; }; @@ -182,7 +174,7 @@ pgf_expr_parser_token(PgfExprParser* parser) } parser->token_tag = PGF_TOKEN_UNKNOWN; - parser->token_value = gu_null_seq; + parser->token_value = NULL; switch (parser->ch) { case EOF: @@ -250,7 +242,7 @@ pgf_expr_parser_token(PgfExprParser* parser) pgf_expr_parser_getc(parser); } parser->token_tag = PGF_TOKEN_IDENT; - parser->token_value = gu_buf_seq(chars); + parser->token_value = chars; } else if (isdigit(parser->ch)) { while (isdigit(parser->ch)) { gu_buf_push(chars, char, parser->ch); @@ -266,10 +258,10 @@ pgf_expr_parser_token(PgfExprParser* parser) pgf_expr_parser_getc(parser); } parser->token_tag = PGF_TOKEN_FLT; - parser->token_value = gu_buf_seq(chars); + parser->token_value = chars; } else { parser->token_tag = PGF_TOKEN_INT; - parser->token_value = gu_buf_seq(chars); + parser->token_value = chars; } } else if (parser->ch == '"') { pgf_expr_parser_getc(parser); @@ -282,7 +274,7 @@ pgf_expr_parser_token(PgfExprParser* parser) if (parser->ch == '"') { pgf_expr_parser_getc(parser); parser->token_tag = PGF_TOKEN_STR; - parser->token_value = gu_buf_seq(chars); + parser->token_value = chars; } } break; @@ -350,7 +342,7 @@ pgf_expr_parser_term(PgfExprParser* parser) } case PGF_TOKEN_IDENT: { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); PgfCId id = gu_str_string(str, parser->expr_pool); pgf_expr_parser_token(parser); return gu_new_variant_i(parser->expr_pool, @@ -360,7 +352,7 @@ pgf_expr_parser_term(PgfExprParser* parser) } case PGF_TOKEN_INT: { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); int n = atoi(str); pgf_expr_parser_token(parser); PgfLiteral lit = @@ -375,7 +367,7 @@ pgf_expr_parser_term(PgfExprParser* parser) } case PGF_TOKEN_STR: { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); GuString s = gu_str_string(str, parser->expr_pool); pgf_expr_parser_token(parser); PgfLiteral lit = @@ -390,7 +382,7 @@ pgf_expr_parser_term(PgfExprParser* parser) } case PGF_TOKEN_FLT: { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); double d = atof(str); pgf_expr_parser_token(parser); PgfLiteral lit = @@ -451,7 +443,7 @@ pgf_expr_parser_bind(PgfExprParser* parser, GuBuf* binds) for (;;) { if (parser->token_tag == PGF_TOKEN_IDENT) { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); var = gu_str_string(str, parser->expr_pool); pgf_expr_parser_token(parser); } else if (parser->token_tag == PGF_TOKEN_WILD) { @@ -571,7 +563,7 @@ pgf_expr_parser_hypos(PgfExprParser* parser, GuBuf* hypos) if (parser->token_tag == PGF_TOKEN_IDENT) { char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); var = gu_str_string(str, parser->expr_pool); pgf_expr_parser_token(parser); } else if (parser->token_tag == PGF_TOKEN_WILD) { @@ -612,7 +604,7 @@ pgf_expr_parser_atom(PgfExprParser* parser) return NULL; char* str = - gu_chars_str(parser->token_value, parser->tmp_pool); + gu_char_buf_str(parser->token_value, parser->tmp_pool); PgfCId cid = gu_str_string(str, parser->expr_pool); pgf_expr_parser_token(parser); @@ -712,7 +704,7 @@ pgf_expr_parser_type(PgfExprParser* parser) } } - type->hypos = gu_buf_seq(hypos); + type->hypos = gu_buf_data_seq(hypos); return type; } diff --git a/src/runtime/c/pgf/expr.h b/src/runtime/c/pgf/expr.h index 485464207..08200a716 100644 --- a/src/runtime/c/pgf/expr.h +++ b/src/runtime/c/pgf/expr.h @@ -14,8 +14,6 @@ typedef GuVariant PgfExpr; extern GU_DECLARE_TYPE(PgfExpr, GuVariant); -typedef GuList(PgfExpr) PgfExprs; - typedef struct PgfHypo PgfHypo; typedef struct PgfType PgfType; @@ -63,10 +61,9 @@ struct PgfHypo { }; typedef GuSeq PgfHypos; -extern GU_DECLARE_TYPE(PgfHypos, GuSeq); struct PgfType { - PgfHypos hypos; + PgfHypos* hypos; PgfCId cid; /// XXX: resolve to PgfCat*? size_t n_exprs; PgfExpr exprs[]; diff --git a/src/runtime/c/pgf/graphviz.c b/src/runtime/c/pgf/graphviz.c index 005de6ec4..5190d2fee 100644 --- a/src/runtime/c/pgf/graphviz.c +++ b/src/runtime/c/pgf/graphviz.c @@ -116,7 +116,7 @@ typedef struct { } PgfBracketLznState; static void -pgf_bracket_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks) +pgf_bracket_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens* toks) { PgfBracketLznState* state = gu_container(funcs, PgfBracketLznState, funcs); diff --git a/src/runtime/c/pgf/lexer.c b/src/runtime/c/pgf/lexer.c index 5f3867af3..48ab6eb70 100644 --- a/src/runtime/c/pgf/lexer.c +++ b/src/runtime/c/pgf/lexer.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/src/runtime/c/pgf/linearizer.c b/src/runtime/c/pgf/linearizer.c index f73c650d8..0a29db824 100644 --- a/src/runtime/c/pgf/linearizer.c +++ b/src/runtime/c/pgf/linearizer.c @@ -237,7 +237,7 @@ pgf_lzn_resolve_def(PgfLzn* lzn, PgfCncFuns* lindefs, GuString s, GuPool* pool) return lit; int index = - gu_choice_next(lzn->ch, gu_list_length(lindefs)); + gu_choice_next(lzn->ch, gu_seq_length(lindefs)); if (index < 0) { return ret; } @@ -245,7 +245,7 @@ pgf_lzn_resolve_def(PgfLzn* lzn, PgfCncFuns* lindefs, GuString s, GuPool* pool) gu_new_flex_variant(PGF_CNC_TREE_APP, PgfCncTreeApp, args, 1, &ret, pool); - capp->fun = gu_list_index(lindefs, index); + capp->fun = gu_seq_get(lindefs, PgfCncFun*, index); capp->fid = lzn->fid++; capp->n_args = 1; capp->args[0] = lit; @@ -472,7 +472,7 @@ pgf_lzr_linearize(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, PgfLinFuncs } gu_require(lin_idx < fun->n_lins); - PgfSequence seq = fun->lins[lin_idx]; + PgfSequence* seq = fun->lins[lin_idx]; size_t nsyms = gu_seq_length(seq); PgfSymbol* syms = gu_seq_data(seq); for (size_t i = 0; i < nsyms; i++) { @@ -572,7 +572,7 @@ struct PgfSimpleLin { }; static void -pgf_file_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks) +pgf_file_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens* toks) { PgfSimpleLin* flin = gu_container(funcs, PgfSimpleLin, funcs); if (!gu_ok(flin->err)) { diff --git a/src/runtime/c/pgf/linearizer.h b/src/runtime/c/pgf/linearizer.h index 85a28ef41..bd143c1c2 100644 --- a/src/runtime/c/pgf/linearizer.h +++ b/src/runtime/c/pgf/linearizer.h @@ -51,7 +51,7 @@ typedef struct PgfLinFuncs PgfLinFuncs; struct PgfLinFuncs { /// Output tokens - void (*symbol_tokens)(PgfLinFuncs** self, PgfTokens toks); + void (*symbol_tokens)(PgfLinFuncs** self, PgfTokens* toks); /// Output literal void (*expr_literal)(PgfLinFuncs** self, PgfLiteral lit); diff --git a/src/runtime/c/pgf/literals.c b/src/runtime/c/pgf/literals.c index b8e35ea64..a11097781 100644 --- a/src/runtime/c/pgf/literals.c +++ b/src/runtime/c/pgf/literals.c @@ -18,7 +18,7 @@ pgf_match_string_lit(PgfConcr* concr, PgfItem* item, PgfToken tok, GuPool* tmp_pool = gu_new_pool(); size_t lin_idx; - PgfSequence seq; + PgfSequence* seq; pgf_item_sequence(item, &lin_idx, &seq, tmp_pool); gu_assert(lin_idx == 0); @@ -66,7 +66,7 @@ pgf_match_int_lit(PgfConcr* concr, PgfItem* item, PgfToken tok, GuPool* tmp_pool = gu_new_pool(); size_t lin_idx; - PgfSequence seq; + PgfSequence* seq; pgf_item_sequence(item, &lin_idx, &seq, tmp_pool); gu_assert(lin_idx == 0); @@ -123,7 +123,7 @@ pgf_match_float_lit(PgfConcr* concr, PgfItem* item, PgfToken tok, GuPool* tmp_pool = gu_new_pool(); size_t lin_idx; - PgfSequence seq; + PgfSequence* seq; pgf_item_sequence(item, &lin_idx, &seq, tmp_pool); gu_assert(lin_idx == 0); @@ -180,7 +180,7 @@ pgf_match_name_lit(PgfConcr* concr, PgfItem* item, PgfToken tok, GuPool* tmp_pool = gu_new_pool(); size_t lin_idx; - PgfSequence seq; + PgfSequence* seq; pgf_item_sequence(item, &lin_idx, &seq, tmp_pool); gu_assert(lin_idx == 0); diff --git a/src/runtime/c/pgf/parser.c b/src/runtime/c/pgf/parser.c index 4408b4336..5c66edc96 100644 --- a/src/runtime/c/pgf/parser.c +++ b/src/runtime/c/pgf/parser.c @@ -24,8 +24,8 @@ struct PgfItemConts { static GU_DEFINE_TYPE(PgfItemConts, abstract, _); -typedef GuList(PgfItemConts*) PgfItemContss; -static GU_DEFINE_TYPE(PgfItemContss, abstract, _); +typedef GuSeq PgfItemContss; +static GU_DEFINE_TYPE(PgfItemContss, abstract); typedef GuMap PgfContsMap; static GU_DEFINE_TYPE(PgfContsMap, GuMap, @@ -71,7 +71,7 @@ extern GuHasher pgf_cfcat_hasher; GU_DEFINE_TYPE(PgfProductionIdx, GuMap, gu_type(PgfCFCat), &pgf_cfcat_hasher, - gu_type(PgfProductionSeq), &gu_null_seq); + gu_ptr_type(PgfProductionBuf), &gu_null_struct); typedef struct PgfTokenState PgfTokenState; @@ -110,7 +110,7 @@ typedef struct PgfAnswers { typedef struct { PgfAnswers* answers; PgfExprProb ep; - PgfPArgs args; + PgfPArgs* args; size_t arg_idx; } PgfExprState; @@ -130,7 +130,7 @@ struct PgfItem { }; PgfProduction prod; - PgfPArgs args; + PgfPArgs* args; PgfSymbol curr_sym; uint16_t seq_idx; uint8_t tok_idx; @@ -183,10 +183,10 @@ pgf_item_sequence_length(PgfItem* item) } case PGF_PRODUCTION_EXTERN: { PgfProductionExtern* pext = i.data; - PgfSequence seq; + PgfSequence* seq; - if (!gu_seq_is_null(pext->lins) && - !gu_seq_is_null(seq = gu_seq_get(pext->lins,PgfSequence,item->conts->lin_idx))) { + if (pext->lins != NULL && + (seq = gu_seq_get(pext->lins,PgfSequence*,item->conts->lin_idx)) != NULL) { return gu_seq_length(seq); } else { int seq_len = 0; @@ -215,12 +215,12 @@ pgf_item_sequence_length(PgfItem* item) } } -static PgfSequence +static PgfSequence* pgf_extern_seq_get(PgfItem* item, GuPool* pool) { int seq_len = pgf_item_sequence_length(item); - PgfSequence seq = + PgfSequence* seq = gu_new_seq(PgfSymbol, seq_len, pool); PgfSymbol sym = item->curr_sym; while (!gu_variant_is_null(sym)) { @@ -233,7 +233,7 @@ pgf_extern_seq_get(PgfItem* item, GuPool* pool) void pgf_item_sequence(PgfItem* item, - size_t* lin_idx, PgfSequence* seq, + size_t* lin_idx, PgfSequence** seq, GuPool* pool) { *lin_idx = item->conts->lin_idx; @@ -249,15 +249,15 @@ pgf_item_sequence(PgfItem* item, gu_new_variant_i(pool, PGF_SYMBOL_CAT, PgfSymbolCat, .d = 0, .r = item->conts->lin_idx); - *seq = gu_new_seq(PgfSequence, 1, pool); + *seq = gu_new_seq(PgfSequence*, 1, pool); gu_seq_set(*seq, PgfSymbol, 0, sym); break; } case PGF_PRODUCTION_EXTERN: { PgfProductionExtern* pext = i.data; - if (gu_seq_is_null(pext->lins) || - gu_seq_is_null(*seq = gu_seq_get(pext->lins, PgfSequence, item->conts->lin_idx))) { + if (pext->lins == NULL || + (*seq = gu_seq_get(pext->lins, PgfSequence*, item->conts->lin_idx)) == NULL) { *seq = pgf_extern_seq_get(item, pool); } break; @@ -284,10 +284,10 @@ pgf_print_production_args(PgfPArgs args, PgfPArg arg = gu_seq_get(args, PgfPArg, j); if (arg.hypos != NULL && - gu_list_length(arg.hypos) > 0) { - size_t n_hypos = gu_list_length(arg.hypos); + gu_seq_length(arg.hypos) > 0) { + size_t n_hypos = gu_seq_length(arg.hypos); for (size_t k = 0; k < n_hypos; k++) { - PgfCCat *hypo = gu_list_index(arg.hypos, k); + PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k); gu_printf(out,err,"C%d ",hypo->fid); } gu_printf(out,err,"-> "); @@ -501,11 +501,11 @@ static PgfItemContss* pgf_parsing_get_contss(PgfContsMap* conts_map, PgfCCat* cat, GuPool *pool) { PgfItemContss* contss = gu_map_get(conts_map, cat, PgfItemContss*); - if (!contss) { + if (contss == NULL) { size_t n_lins = cat->cnccat->n_lins; - contss = gu_new_list(PgfItemContss, pool, n_lins); + contss = gu_new_seq(PgfItemConts*, n_lins, pool); for (size_t i = 0; i < n_lins; i++) { - gu_list_index(contss, i) = NULL; + gu_seq_set(contss, PgfItemConts*, i, NULL); } gu_map_put(conts_map, cat, PgfItemContss*, contss); } @@ -521,7 +521,7 @@ pgf_parsing_get_conts(PgfContsMap* conts_map, gu_require(lin_idx < ccat->cnccat->n_lins); PgfItemContss* contss = pgf_parsing_get_contss(conts_map, ccat, pool); - PgfItemConts* conts = gu_list_index(contss, lin_idx); + PgfItemConts* conts = gu_seq_get(contss, PgfItemConts*, lin_idx); if (!conts) { conts = gu_new(PgfItemConts, pool); conts->ccat = ccat; @@ -530,7 +530,7 @@ pgf_parsing_get_conts(PgfContsMap* conts_map, conts->items = gu_new_buf(PgfItem*, pool); conts->outside_prob = 0; conts->ref_count = 0; - gu_list_index(contss, lin_idx) = conts; + gu_seq_get(contss, PgfItemConts*, lin_idx) = conts; #ifdef PGF_COUNTS_DEBUG if (state != NULL) { @@ -541,19 +541,30 @@ pgf_parsing_get_conts(PgfContsMap* conts_map, return conts; } +static void +gu_ccat_fini(GuFinalizer* fin) +{ + PgfCCat* cat = gu_container(fin, PgfCCat, fin); + if (cat->prods != NULL) + gu_seq_free(cat->prods); +} + static PgfCCat* pgf_parsing_create_completed(PgfParseState* state, PgfItemConts* conts, prob_t viterbi_prob) { - PgfCCat* cat = gu_new(PgfCCat, state->ps->pool); + PgfCCat* cat = gu_new_flex(state->ps->pool, PgfCCat, fin, 1); cat->cnccat = conts->ccat->cnccat; cat->viterbi_prob = viterbi_prob; cat->fid = state->ps->max_fid++; cat->conts = conts; cat->answers = NULL; - cat->prods = gu_buf_seq(gu_new_buf(PgfProduction, state->ps->pool)); + cat->prods = NULL; cat->n_synprods = 0; gu_map_put(state->generated_cats, conts, PgfCCat*, cat); + + cat->fin[0].fn = gu_ccat_fini; + gu_pool_finally(state->ps->pool, cat->fin); #ifdef PGF_COUNTS_DEBUG state->ps->ccat_full_count++; @@ -562,6 +573,15 @@ pgf_parsing_create_completed(PgfParseState* state, PgfItemConts* conts, return cat; } +static void +pgf_parsing_add_production(PgfCCat* ccat, PgfProduction prod) +{ + if (ccat->prods == NULL || ccat->n_synprods >= gu_seq_length(ccat->prods)) { + ccat->prods = gu_realloc_seq(ccat->prods, PgfProduction, ccat->n_synprods+1); + } + gu_seq_set(ccat->prods, PgfProduction, ccat->n_synprods++, prod); +} + static PgfCCat* pgf_parsing_get_completed(PgfParseState* state, PgfItemConts* conts) { @@ -577,7 +597,7 @@ pgf_item_set_curr_symbol(PgfItem* item, GuPool* pool) PgfProductionApply* papp = i.data; PgfCncFun* fun = papp->fun; gu_assert(item->conts->lin_idx < fun->n_lins); - PgfSequence seq = fun->lins[item->conts->lin_idx]; + PgfSequence* seq = fun->lins[item->conts->lin_idx]; gu_assert(item->seq_idx <= gu_seq_length(seq)); if (item->seq_idx == gu_seq_length(seq)) { item->curr_sym = gu_null_variant; @@ -868,16 +888,15 @@ pgf_parsing_new_production(PgfItem* item, PgfExprProb *ep, GuPool *pool) PgfProductionCoerce, &prod, pool); PgfPArg* parg = gu_seq_index(item->args, PgfPArg, 0); - gu_assert(!parg->hypos || !parg->hypos->len); new_pcoerce->coerce = parg->ccat; break; } case PGF_PRODUCTION_EXTERN: { PgfProductionExtern* pext = i.data; - if (gu_seq_is_null(pext->lins) || - gu_seq_is_null(gu_seq_get(pext->lins,PgfSequence,item->conts->lin_idx))) { - PgfSequence seq = + if (pext->lins == NULL || + gu_seq_get(pext->lins,PgfSequence*,item->conts->lin_idx) == NULL) { + PgfSequence* seq = pgf_extern_seq_get(item, pool); size_t n_lins = item->conts->ccat->cnccat->n_lins; @@ -888,20 +907,20 @@ pgf_parsing_new_production(PgfItem* item, PgfExprProb *ep, GuPool *pool) &prod, pool); new_pext->callback = pext->callback; new_pext->ep = ep; - new_pext->lins = gu_new_seq(PgfSequence, n_lins, pool); + new_pext->lins = gu_new_seq(PgfSequence*, n_lins, pool); - if (gu_seq_is_null(pext->lins)) { + if (pext->lins == NULL) { for (size_t i = 0; i < n_lins; i++) { - gu_seq_set(new_pext->lins,PgfSequence,i, - gu_null_seq); + gu_seq_set(new_pext->lins,PgfSequence*,i, + NULL); } } else { for (size_t i = 0; i < n_lins; i++) { - gu_seq_set(new_pext->lins,PgfSequence,i, - gu_seq_get(pext->lins,PgfSequence,i)); + gu_seq_set(new_pext->lins,PgfSequence*,i, + gu_seq_get(pext->lins,PgfSequence*,i)); } } - gu_seq_set(new_pext->lins,PgfSequence,item->conts->lin_idx,seq); + gu_seq_set(new_pext->lins,PgfSequence*,item->conts->lin_idx,seq); } else { prod = item->prod; } @@ -940,9 +959,7 @@ pgf_parsing_complete(PgfParseState* before, PgfParseState* after, item->inside_prob); } - GuBuf* prodbuf = gu_seq_buf(cat->prods); - gu_buf_push(prodbuf, PgfProduction, prod); - cat->n_synprods++; + pgf_parsing_add_production(cat, prod); #ifdef PGF_PARSER_DEBUG GuPool* tmp_pool = gu_new_pool(); @@ -962,9 +979,9 @@ pgf_parsing_complete(PgfParseState* before, PgfParseState* after, if (tmp_cat != NULL) { PgfItemContss* contss = pgf_parsing_get_contss(before->conts_map, cat, before->ps->pool); - size_t n_contss = gu_list_length(contss); + size_t n_contss = gu_seq_length(contss); for (size_t i = 0; i < n_contss; i++) { - PgfItemConts* conts2 = gu_list_index(contss, i); + PgfItemConts* conts2 = gu_seq_get(contss, PgfItemConts*, i); /* If there are continuations for * linearization index i, then (cat, i) has * already been predicted. Add the new @@ -981,9 +998,9 @@ pgf_parsing_complete(PgfParseState* before, PgfParseState* after, while (state != NULL) { PgfItemContss* contss = pgf_parsing_get_contss(state->conts_map, cat, state->ps->pool); - size_t n_contss = gu_list_length(contss); + size_t n_contss = gu_seq_length(contss); for (size_t i = 0; i < n_contss; i++) { - PgfItemConts* conts2 = gu_list_index(contss, i); + PgfItemConts* conts2 = gu_seq_get(contss, PgfItemConts*, i); /* If there are continuations for * linearization index i, then (cat, i) has * already been predicted. Add the new @@ -1034,7 +1051,7 @@ pgf_parsing_td_predict(PgfParseState* before, PgfParseState* after, // we don't know the current token. // probably we just compute the list of completions - if (lexicon_idx == NULL) + if (lexicon_idx == NULL && ccat->fid < after->ps->concr->total_cats) n_prods = gu_seq_length(ccat->prods); } @@ -1049,14 +1066,14 @@ pgf_parsing_td_predict(PgfParseState* before, PgfParseState* after, if (lexicon_idx != NULL) { PgfCFCat cfc = {ccat, lin_idx}; - PgfProductionSeq tok_prods = - gu_map_get(lexicon_idx, &cfc, PgfProductionSeq); + PgfProductionBuf* tok_prods = + gu_map_get(lexicon_idx, &cfc, PgfProductionBuf*); - if (!gu_seq_is_null(tok_prods)) { - size_t n_prods = gu_seq_length(tok_prods); + if (tok_prods != NULL) { + size_t n_prods = gu_buf_length(tok_prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = - gu_seq_get(tok_prods, PgfProduction, i); + gu_buf_get(tok_prods, PgfProduction, i); pgf_parsing_production(before, conts, prod); } @@ -1065,14 +1082,14 @@ pgf_parsing_td_predict(PgfParseState* before, PgfParseState* after, // Bottom-up prediction for epsilon rules PgfCFCat cfc = {ccat, lin_idx}; - PgfProductionSeq eps_prods = - gu_map_get(before->ps->concr->epsilon_idx, &cfc, PgfProductionSeq); + PgfProductionBuf* eps_prods = + gu_map_get(before->ps->concr->epsilon_idx, &cfc, PgfProductionBuf*); - if (!gu_seq_is_null(eps_prods)) { - size_t n_prods = gu_seq_length(eps_prods); + if (eps_prods != NULL) { + size_t n_prods = gu_buf_length(eps_prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = - gu_seq_get(eps_prods, PgfProduction, i); + gu_buf_get(eps_prods, PgfProduction, i); pgf_parsing_production(before, conts, prod); } @@ -1145,10 +1162,10 @@ pgf_parsing_meta_predict(GuMapItor* fn, const void* key, void* value, GuExn* err if (cnccat == NULL) return; - size_t n_cats = gu_list_length(cnccat->cats); + size_t n_cats = gu_seq_length(cnccat->cats); for (size_t i = 0; i < n_cats; i++) { - PgfCCat* ccat = gu_list_index(cnccat->cats, i); - if (gu_seq_is_null(ccat->prods)) { + PgfCCat* ccat = gu_seq_get(cnccat->cats, PgfCCat*, i); + if (ccat->prods == NULL) { // empty category continue; } @@ -1188,9 +1205,8 @@ pgf_parsing_symbol(PgfParseState* before, PgfParseState* after, case PGF_SYMBOL_CAT: { PgfSymbolCat* scat = gu_variant_data(sym); PgfPArg* parg = gu_seq_index(item->args, PgfPArg, scat->d); - gu_assert(!parg->hypos || !parg->hypos->len); - if (gu_seq_is_null(parg->ccat->prods)) { + if (parg->ccat->prods == NULL) { // empty category pgf_item_free(before, after, item); return; @@ -1234,11 +1250,11 @@ pgf_parsing_symbol(PgfParseState* before, PgfParseState* after, for (size_t i = 0; i < skp->n_forms; i++) { // XXX: do nubbing properly - PgfTokens toks = skp->forms[i].form; - PgfTokens toks2 = skp->default_form; + PgfTokens* toks = skp->forms[i].form; + PgfTokens* toks2 = skp->default_form; bool skip = pgf_tokens_equal(toks, toks2); for (size_t j = 0; j < i; j++) { - PgfTokens toks2 = skp->forms[j].form; + PgfTokens* toks2 = skp->forms[j].form; skip |= pgf_tokens_equal(toks, toks2); } if (!skip) { @@ -1264,7 +1280,7 @@ pgf_parsing_symbol(PgfParseState* before, PgfParseState* after, pgf_parsing_add_transition(before, after, tok, item); } else { gu_assert(alt <= skp->n_forms); - PgfTokens toks = skp->forms[alt - 1].form; + PgfTokens* toks = skp->forms[alt - 1].form; PgfToken tok = gu_seq_get(toks, PgfToken, idx); item->tok_idx++; if (item->tok_idx == gu_seq_length(toks)) { @@ -1280,7 +1296,6 @@ pgf_parsing_symbol(PgfParseState* before, PgfParseState* after, if (after != NULL) { PgfSymbolLit* slit = gu_variant_data(sym); PgfPArg* parg = gu_seq_index(item->args, PgfPArg, slit->d); - gu_assert(!parg->hypos || !parg->hypos->len); if (parg->ccat->fid > 0 && parg->ccat->fid >= before->ps->concr->total_cats) { @@ -1310,7 +1325,7 @@ pgf_parsing_symbol(PgfParseState* before, PgfParseState* after, &prod, before->ps->pool); pext->callback = callback; pext->ep = NULL; - pext->lins = gu_null_seq; + pext->lins = NULL; pgf_parsing_production(before, conts, prod); } @@ -1366,7 +1381,7 @@ pgf_parsing_item(PgfParseState* before, PgfParseState* after, PgfItem* item) case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = i.data; PgfCncFun* fun = papp->fun; - PgfSequence seq = fun->lins[item->conts->lin_idx]; + PgfSequence* seq = fun->lins[item->conts->lin_idx]; if (item->seq_idx == gu_seq_length(seq)) { pgf_parsing_complete(before, after, item, NULL); pgf_item_free(before, after, item); @@ -1379,7 +1394,7 @@ pgf_parsing_item(PgfParseState* before, PgfParseState* after, PgfItem* item) PgfProductionCoerce* pcoerce = i.data; switch (item->seq_idx) { case 0: - if (gu_seq_is_null(pcoerce->coerce->prods)) { + if (pcoerce->coerce->prods == NULL) { // empty category pgf_item_free(before, after, item); return; @@ -1401,9 +1416,9 @@ pgf_parsing_item(PgfParseState* before, PgfParseState* after, PgfItem* item) case PGF_PRODUCTION_EXTERN: { PgfProductionExtern* pext = i.data; - PgfSequence seq; - if (!gu_seq_is_null(pext->lins) && - !gu_seq_is_null(seq = gu_seq_get(pext->lins,PgfSequence,item->conts->lin_idx))) { + PgfSequence* seq; + if (pext->lins != NULL && + (seq = gu_seq_get(pext->lins,PgfSequence*,item->conts->lin_idx)) != NULL) { if (item->seq_idx == gu_seq_length(seq)) { pgf_parsing_complete(before, after, item, NULL); pgf_item_free(before, after, item); @@ -1626,15 +1641,15 @@ static void pgf_parser_compute_lexicon_prob(GuMapItor* fn, const void* key, void* value, GuExn* err) { PgfTokenState* ts = ((PgfLexiconFn*) fn)->ts; - PgfProductionSeq prods = *((PgfProductionSeq*) value); - - if (gu_seq_is_null(prods)) + PgfProductionBuf* prods = *((PgfProductionBuf**) value); + + if (prods == NULL) return; - size_t n_prods = gu_seq_length(prods); + size_t n_prods = gu_buf_length(prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = - gu_seq_get(prods, PgfProduction, i); + gu_buf_get(prods, PgfProduction, i); GuVariantInfo pi = gu_variant_open(prod); switch (pi.tag) { @@ -1740,7 +1755,7 @@ typedef struct { } PgfPrefixTokenState; static GuString -pgf_get_tokens(PgfSequence seq, +pgf_get_tokens(PgfSequence* seq, uint16_t seq_idx, uint8_t tok_idx, GuPool* pool) { @@ -1791,7 +1806,7 @@ pgf_prefix_match_token(PgfTokenState* ts0, PgfToken tok, PgfItem* item) if (gu_string_is_prefix(ts->prefix, tok)) { size_t lin_idx; - PgfSequence seq; + PgfSequence* seq; pgf_item_sequence(item, &lin_idx, &seq, ts->pool); uint16_t seq_idx = item->seq_idx; @@ -1911,8 +1926,7 @@ pgf_result_production(PgfParsing* ps, PgfProductionCoerce* pcoerce = pi.data; PgfCCat* ccat = pcoerce->coerce; - size_t n_prods = gu_seq_length(ccat->prods); - for (size_t i = 0; i < n_prods; i++) { + for (size_t i = 0; i < ccat->n_synprods; i++) { PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i); pgf_result_production(ps, answers, prod); @@ -1978,12 +1992,11 @@ pgf_result_predict(PgfParsing* ps, gu_buf_push(answers->conts, PgfExprState*, cont); if (gu_buf_length(answers->conts) == 1) { - if (gu_seq_is_null(ccat->prods)) + if (ccat->prods == NULL) return; // Generation - size_t n_prods = gu_seq_length(ccat->prods); - for (size_t i = 0; i < n_prods; i++) { + for (size_t i = 0; i < ccat->n_synprods; i++) { PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i); pgf_result_production(ps, answers, prod); @@ -2239,11 +2252,11 @@ pgf_parser_init_state(PgfConcr* concr, PgfCId cat, size_t lin_idx, PgfParseState* state = pgf_new_parse_state(ps, NULL, NULL, pool); - size_t n_ccats = gu_list_length(cnccat->cats); + size_t n_ccats = gu_seq_length(cnccat->cats); for (size_t i = 0; i < n_ccats; i++) { - PgfCCat* ccat = gu_list_index(cnccat->cats, i); + PgfCCat* ccat = gu_seq_get(cnccat->cats, PgfCCat*, i); if (ccat != NULL) { - if (gu_seq_is_null(ccat->prods)) { + if (ccat->prods == NULL) { // Empty category continue; } @@ -2295,7 +2308,7 @@ pgf_parser_add_literal(PgfConcr *concr, PgfCId cat, typedef struct { GuMapItor fn; - PgfTokens tokens; + PgfTokens* tokens; PgfMorphoCallback* callback; } PgfMorphoFn; @@ -2304,27 +2317,27 @@ pgf_morpho_iter(GuMapItor* fn, const void* key, void* value, GuExn* err) { PgfMorphoFn* clo = (PgfMorphoFn*) fn; PgfCFCat cfc = *((PgfCFCat*) key); - PgfProductionSeq prods = *((PgfProductionSeq*) value); + PgfProductionBuf* prods = *((PgfProductionBuf**) value); - if (gu_seq_is_null(prods)) + if (prods == NULL) return; GuString analysis = cfc.ccat->cnccat->labels[cfc.lin_idx]; - size_t n_prods = gu_seq_length(prods); + size_t n_prods = gu_buf_length(prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = - gu_seq_get(prods, PgfProduction, i); + gu_buf_get(prods, PgfProduction, i); GuVariantInfo i = gu_variant_open(prod); switch (i.tag) { case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = i.data; - if (!gu_seq_is_null(clo->tokens)) { + if (clo->tokens != NULL) { // match the tokens with the production size_t pos = 0; - PgfSequence seq = papp->fun->lins[cfc.lin_idx]; + PgfSequence* seq = papp->fun->lins[cfc.lin_idx]; size_t len = gu_seq_length(seq); for (size_t i = 0; i < len; i++) { PgfSymbol sym = gu_seq_get(seq, PgfSymbol, i); @@ -2392,7 +2405,7 @@ pgf_lookup_morpho(PgfConcr *concr, PgfLexer *lexer, tok = pgf_lexer_read_token(lexer, lex_err); } while (!gu_exn_is_raised(lex_err)); - PgfMorphoFn clo = { { pgf_morpho_iter }, gu_buf_seq(tokens), callback }; + PgfMorphoFn clo = { { pgf_morpho_iter }, gu_buf_data_seq(tokens), callback }; gu_map_iter(lexicon_idx, &clo.fn, err); gu_pool_free(tmp_pool); @@ -2414,22 +2427,22 @@ pgf_fullform_iter(GuMapItor* fn, const void* key, void* value, GuExn* err) { PgfFullFormState* st = gu_container(fn, PgfFullFormState, fn); PgfCFCat cfc = *((PgfCFCat*) key); - PgfProductionSeq prods = *((PgfProductionSeq*) value); + PgfProductionBuf* prods = *((PgfProductionBuf**) value); - if (gu_seq_is_null(prods)) + if (prods == NULL) return; - size_t n_prods = gu_seq_length(prods); + size_t n_prods = gu_buf_length(prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = - gu_seq_get(prods, PgfProduction, i); + gu_buf_get(prods, PgfProduction, i); GuVariantInfo i = gu_variant_open(prod); switch (i.tag) { case PGF_PRODUCTION_APPLY: { PgfProductionApply* papp = i.data; - PgfSequence seq = papp->fun->lins[cfc.lin_idx]; + PgfSequence* seq = papp->fun->lins[cfc.lin_idx]; GuString tokens = pgf_get_tokens(seq, 0, 0, st->pool); // create a new production index with keys that @@ -2441,14 +2454,14 @@ pgf_fullform_iter(GuMapItor* fn, const void* key, void* value, GuExn* err) gu_map_put(st->new_idx, &tokens, PgfProductionIdx*, lexicon_idx); } - PgfProductionSeq prods = - gu_map_get(lexicon_idx, &cfc, PgfProductionSeq); - if (gu_seq_is_null(prods)) { - prods = gu_buf_seq(gu_new_buf(PgfProduction, st->pool)); - gu_map_put(lexicon_idx, &cfc, PgfProductionSeq, prods); + PgfProductionBuf* prods = + gu_map_get(lexicon_idx, &cfc, PgfProductionBuf*); + if (prods == NULL) { + prods = gu_new_buf(PgfProduction, st->pool); + gu_map_put(lexicon_idx, &cfc, PgfProductionBuf*, prods); } - gu_buf_push(gu_seq_buf(prods), PgfProduction, prod); + gu_buf_push(prods, PgfProduction, prod); } } } @@ -2512,13 +2525,13 @@ pgf_fullform_get_analyses(PgfFullFormEntry* entry, PgfMorphoCallback* callback, GuExn* err) { PgfProductionIdx* lexicon_idx = *((PgfProductionIdx**) entry->value); - PgfMorphoFn clo = { { pgf_morpho_iter }, gu_null_seq, callback }; + PgfMorphoFn clo = { { pgf_morpho_iter }, NULL, callback }; gu_map_iter(lexicon_idx, &clo.fn, err); } static void pgf_parser_index_token(PgfConcr* concr, - PgfTokens tokens, + PgfTokens* tokens, PgfCCat* ccat, size_t lin_idx, PgfProduction prod, GuPool *pool) { @@ -2532,13 +2545,13 @@ pgf_parser_index_token(PgfConcr* concr, } PgfCFCat cfc = {ccat, lin_idx}; - PgfProductionSeq prods = gu_map_get(set, &cfc, PgfProductionSeq); - if (gu_seq_is_null(prods)) { - prods = gu_buf_seq(gu_new_buf(PgfProduction, pool)); - gu_map_put(set, &cfc, PgfProductionSeq, prods); + PgfProductionBuf* prods = gu_map_get(set, &cfc, PgfProductionBuf*); + if (prods == NULL) { + prods = gu_new_buf(PgfProduction, pool); + gu_map_put(set, &cfc, PgfProductionBuf*, prods); } - gu_buf_push(gu_seq_buf(prods), PgfProduction, prod); + gu_buf_push(prods, PgfProduction, prod); } static void @@ -2547,14 +2560,14 @@ pgf_parser_index_epsilon(PgfConcr* concr, GuPool *pool) { PgfCFCat cfc = {ccat, lin_idx}; - PgfProductionSeq prods = - gu_map_get(concr->epsilon_idx, &cfc, PgfProductionSeq); - if (gu_seq_is_null(prods)) { - prods = gu_buf_seq(gu_new_buf(PgfProduction, pool)); - gu_map_put(concr->epsilon_idx, &cfc, PgfProductionSeq, prods); + PgfProductionBuf* prods = + gu_map_get(concr->epsilon_idx, &cfc, PgfProductionBuf*); + if (prods == NULL) { + prods = gu_new_buf(PgfProduction, pool); + gu_map_put(concr->epsilon_idx, &cfc, PgfProductionBuf*, prods); } - gu_buf_push(gu_seq_buf(prods), PgfProduction, prod); + gu_buf_push(prods, PgfProduction, prod); } void @@ -2571,7 +2584,7 @@ pgf_parser_index(PgfConcr* concr, if (gu_seq_length(papp->args) > 0) break; - PgfSequence seq = papp->fun->lins[lin_idx]; + PgfSequence* seq = papp->fun->lins[lin_idx]; if (gu_seq_length(seq) > 0) { GuVariantInfo i = gu_variant_open(gu_seq_get(seq, PgfSymbol, 0)); switch (i.tag) { @@ -2630,7 +2643,7 @@ pgf_ccat_set_viterbi_prob(PgfCCat* ccat) { if (ccat->viterbi_prob == 0) { // uninitialized ccat->viterbi_prob = INFINITY; // set to infinity to avoid loops - if (gu_seq_is_null(ccat->prods)) + if (ccat->prods == NULL) return INFINITY; prob_t viterbi_prob = INFINITY; diff --git a/src/runtime/c/pgf/parser.h b/src/runtime/c/pgf/parser.h index b49cba868..5e88b0742 100644 --- a/src/runtime/c/pgf/parser.h +++ b/src/runtime/c/pgf/parser.h @@ -112,7 +112,7 @@ pgf_item_lin_idx(PgfItem* item); void pgf_item_sequence(PgfItem* item, - size_t* lin_idx, PgfSequence* seq, + size_t* lin_idx, PgfSequence** seq, GuPool* pool); int diff --git a/src/runtime/c/pgf/parseval.c b/src/runtime/c/pgf/parseval.c index eed216b82..84d93b346 100644 --- a/src/runtime/c/pgf/parseval.c +++ b/src/runtime/c/pgf/parseval.c @@ -19,7 +19,7 @@ typedef struct { } PgfMetricsLznState; static void -pgf_metrics_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks) +pgf_metrics_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens* toks) { PgfMetricsLznState* state = gu_container(funcs, PgfMetricsLznState, funcs); diff --git a/src/runtime/c/pgf/printer.c b/src/runtime/c/pgf/printer.c index e9365d0fc..79aac5afd 100644 --- a/src/runtime/c/pgf/printer.c +++ b/src/runtime/c/pgf/printer.c @@ -60,7 +60,7 @@ pgf_print_absfun(GuMapItor* fn, const void* key, void* value, PgfAbsFun *fun = *((PgfAbsFun **) value); GuOut *out = clo->out; - gu_puts(gu_seq_is_null(fun->defns) ? " data " : " fun ", out, err); + gu_puts((fun->defns == NULL) ? " data " : " fun ", out, err); gu_string_write(name, out, err); gu_puts(" : ", out, err); pgf_print_type(fun->type, NULL, 0, out, err); @@ -94,7 +94,7 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value, PgfCCat* ccat = *((PgfCCat**) value); GuOut *out = clo->out; - if (!gu_seq_is_null(ccat->prods)) { + if (ccat->prods != NULL) { size_t n_prods = gu_seq_length(ccat->prods); for (size_t i = 0; i < n_prods; i++) { PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i); @@ -114,11 +114,11 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value, PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j); if (arg.hypos != NULL) { - size_t n_hypos = gu_list_length(arg.hypos); + size_t n_hypos = gu_seq_length(arg.hypos); for (size_t k = 0; k < n_hypos; k++) { if (k > 0) gu_putc(' ',out,err); - PgfCCat *hypo = gu_list_index(arg.hypos, k); + PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k); gu_printf(out,err,"C%d",hypo->fid); } } @@ -152,11 +152,11 @@ pgf_print_lindefs(GuMapItor* fn, const void* key, void* value, if (ccat->lindefs != NULL) { gu_printf(out,err," C%d -> ",fid); - size_t n_lindefs = gu_list_length(ccat->lindefs); + size_t n_lindefs = gu_seq_length(ccat->lindefs); for (size_t i = 0; i < n_lindefs; i++) { if (i > 0) gu_putc(' ', out, err); - PgfCncFun* fun = gu_list_index(ccat->lindefs, i); + PgfCncFun* fun = gu_seq_get(ccat->lindefs, PgfCncFun*, i); gu_printf(out,err,"F%d",fun->funid); } @@ -165,19 +165,19 @@ pgf_print_lindefs(GuMapItor* fn, const void* key, void* value, } static void -pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences *sequences, +pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences* sequences, GuOut *out, GuExn *err) { gu_printf(out,err," F%d := (", cncfun->funid); - size_t n_seqs = gu_list_length(sequences); + size_t n_seqs = gu_seq_length(sequences); for (size_t i = 0; i < cncfun->n_lins; i++) { if (i > 0) gu_putc(',', out, err); - PgfSequence seq = cncfun->lins[i]; + PgfSequence* seq = cncfun->lins[i]; for (size_t seqid = 0; seqid < n_seqs; seqid++) { - if (gu_seq_data(gu_list_index(sequences, seqid)) == gu_seq_data(seq)) { + if (gu_seq_data(gu_seq_get(sequences, PgfSequence*, seqid)) == gu_seq_data(seq)) { gu_printf(out,err,"S%d", seqid); break; } @@ -196,7 +196,7 @@ pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences *sequences, } static void -pgf_print_tokens(PgfTokens tokens, GuOut *out, GuExn *err) +pgf_print_tokens(PgfTokens* tokens, GuOut *out, GuExn *err) { gu_putc('"', out, err); size_t n_toks = gu_seq_length(tokens); @@ -234,11 +234,11 @@ pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err) pgf_print_tokens(skp->forms[i].form, out, err); gu_puts(" / ", out, err); - size_t n_prefixes = gu_list_length(skp->forms[i].prefixes); + size_t n_prefixes = gu_seq_length(skp->forms[i].prefixes); for (size_t j = 0; j < n_prefixes; j++) { if (j > 0) gu_putc(' ', out, err); - GuString prefix = gu_list_index(skp->forms[i].prefixes, j); + GuString prefix = gu_seq_get(skp->forms[i].prefixes, GuString, j); gu_putc('"', out, err); gu_string_write(prefix, out, err); gu_putc('"', out, err); @@ -268,7 +268,7 @@ pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err) } static void -pgf_print_sequence(size_t seqid, PgfSequence seq, GuOut *out, GuExn *err) +pgf_print_sequence(size_t seqid, PgfSequence* seq, GuOut *out, GuExn *err) { gu_printf(out,err," S%d := ", seqid); @@ -296,8 +296,8 @@ pgf_print_cnccat(GuMapItor* fn, const void* key, void* value, gu_string_write(name, out, err); gu_puts(" :=\n", out, err); - PgfCCat *start = gu_list_index(cnccat->cats, 0); - PgfCCat *end = gu_list_index(cnccat->cats, gu_list_length(cnccat->cats)-1); + PgfCCat *start = gu_seq_get(cnccat->cats, PgfCCat*, 0); + PgfCCat *end = gu_seq_get(cnccat->cats, PgfCCat*, gu_seq_length(cnccat->cats)-1); gu_printf(out, err, " range [C%d..C%d]\n", start->fid, end->fid); @@ -332,16 +332,16 @@ pgf_print_concrete(PgfCId cncname, PgfConcr* concr, gu_map_iter(concr->ccats, &clo3.fn, err); gu_puts(" lin\n", out, err); - size_t n_funs = gu_list_length(concr->cncfuns); + size_t n_funs = gu_seq_length(concr->cncfuns); for (size_t i = 0; i < n_funs; i++) { - PgfCncFun* cncfun = gu_list_index(concr->cncfuns, i); + PgfCncFun* cncfun = gu_seq_get(concr->cncfuns, PgfCncFun*, i); pgf_print_cncfun(cncfun, concr->sequences, out, err); } gu_puts(" sequences\n", out, err); - size_t n_seqs = gu_list_length(concr->sequences); + size_t n_seqs = gu_seq_length(concr->sequences); for (size_t i = 0; i < n_seqs; i++) { - PgfSequence seq = gu_list_index(concr->sequences, i); + PgfSequence* seq = gu_seq_get(concr->sequences, PgfSequence*, i); pgf_print_sequence(i, seq, out, err); } diff --git a/src/runtime/c/pgf/reader.c b/src/runtime/c/pgf/reader.c index 00e5061da..0eb72386a 100644 --- a/src/runtime/c/pgf/reader.c +++ b/src/runtime/c/pgf/reader.c @@ -286,7 +286,7 @@ pgf_read_type_(PgfReader* rdr) { size_t n_hypos = pgf_read_len(rdr); gu_return_on_exn(rdr->err, NULL); - GuSeq hypos = gu_new_seq(PgfHypo, n_hypos, rdr->opool); + GuSeq* hypos = gu_new_seq(PgfHypo, n_hypos, rdr->opool); for (size_t i = 0; i < n_hypos; i++) { PgfHypo* hypo = gu_seq_index(hypos, PgfHypo, i); pgf_read_hypo(rdr, hypo); @@ -415,7 +415,7 @@ pgf_read_absfun(PgfReader* rdr) gu_return_on_exn(rdr->err, NULL); switch (tag) { case 0: - absfun->defns = gu_null_seq; + absfun->defns = NULL; break; case 1: { GuLength length = pgf_read_len(rdr); @@ -586,16 +586,16 @@ pgf_read_printnames(PgfReader* rdr) return printnames; } -static PgfTokens +static PgfTokens* pgf_read_tokens(PgfReader* rdr) { size_t len = pgf_read_len(rdr); - gu_return_on_exn(rdr->err, gu_null_seq); + gu_return_on_exn(rdr->err, NULL); - PgfTokens tokens = gu_new_seq(PgfToken, len, rdr->opool); + PgfTokens* tokens = gu_new_seq(PgfToken, len, rdr->opool); for (size_t i = 0; i < len; i++) { PgfToken token = pgf_read_string(rdr); - gu_return_on_exn(rdr->err, gu_null_seq); + gu_return_on_exn(rdr->err, NULL); gu_seq_set(tokens, PgfToken, i, token); } @@ -612,12 +612,12 @@ pgf_read_alternative(PgfReader* rdr, PgfAlternative* alt) size_t n_prefixes = pgf_read_len(rdr); gu_return_on_exn(rdr->err,); - alt->prefixes = gu_new_list(GuStringL, rdr->opool, n_prefixes); + alt->prefixes = gu_new_seq(GuString, n_prefixes, rdr->opool); for (size_t i = 0; i < n_prefixes; i++) { GuString prefix = pgf_read_string(rdr); gu_return_on_exn(rdr->err,); - gu_list_index(alt->prefixes, i) = prefix; + gu_seq_set(alt->prefixes, GuString, i, prefix); } } @@ -677,7 +677,7 @@ pgf_read_symbol(PgfReader* rdr) break; } case PGF_SYMBOL_KP: { - PgfTokens default_form = pgf_read_tokens(rdr); + PgfTokens* default_form = pgf_read_tokens(rdr); gu_return_on_exn(rdr->err, gu_null_variant); size_t n_forms = pgf_read_len(rdr); @@ -710,17 +710,17 @@ pgf_read_symbol(PgfReader* rdr) return sym; } -static PgfSequence +static PgfSequence* pgf_read_sequence(PgfReader* rdr) { size_t len = pgf_read_len(rdr); - gu_return_on_exn(rdr->err, gu_null_seq); + gu_return_on_exn(rdr->err, NULL); - PgfSequence seq = gu_new_seq(PgfSymbol, len, rdr->opool); + PgfSequence* seq = gu_new_seq(PgfSymbol, len, rdr->opool); for (size_t i = 0; i < len; i++) { PgfSymbol sym = pgf_read_symbol(rdr); - gu_return_on_exn(rdr->err, gu_null_seq); + gu_return_on_exn(rdr->err, NULL); gu_seq_set(seq, PgfSymbol, i, sym); } @@ -733,12 +733,12 @@ pgf_read_sequences(PgfReader* rdr) size_t len = pgf_read_len(rdr); gu_return_on_exn(rdr->err, NULL); - PgfSequences* seqs = gu_new_list(PgfSequences, rdr->opool, len); + PgfSequences* seqs = gu_new_seq(PgfSequence*, len, rdr->opool); for (size_t i = 0; i < len; i++) { - PgfSequence seq = pgf_read_sequence(rdr); + PgfSequence* seq = pgf_read_sequence(rdr); gu_return_on_exn(rdr->err, NULL); - gu_list_index(seqs, i) = seq; + gu_seq_set(seqs, PgfSequence*, i, seq); } return seqs; @@ -763,15 +763,15 @@ pgf_read_cncfun(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, int funid) cncfun->n_lins = len; for (size_t i = 0; i < len; i++) { - int seqid = pgf_read_int(rdr); + size_t seqid = pgf_read_int(rdr); gu_return_on_exn(rdr->err, NULL); - if (seqid < 0 || seqid >= gu_list_length(concr->sequences)) { + if (seqid >= gu_seq_length(concr->sequences)) { gu_raise(rdr->err, PgfReadExn); return NULL; } - cncfun->lins[i] = gu_list_elems(concr->sequences)[seqid]; + cncfun->lins[i] = gu_seq_get(concr->sequences, PgfSequence*, seqid); } return cncfun; @@ -783,13 +783,13 @@ pgf_read_cncfuns(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr) size_t len = pgf_read_len(rdr); gu_return_on_exn(rdr->err, NULL); - PgfCncFuns* cncfuns = gu_new_list(PgfCncFuns, rdr->opool, len); + PgfCncFuns* cncfuns = gu_new_seq(PgfCncFun*, len, rdr->opool); for (size_t funid = 0; funid < len; funid++) { PgfCncFun* cncfun = pgf_read_cncfun(rdr, abstr, concr, funid); gu_return_on_exn(rdr->err, NULL); - gu_list_index(cncfuns, funid) = cncfun; + gu_seq_set(cncfuns, PgfCncFun*, funid, cncfun); } return cncfuns; @@ -807,7 +807,7 @@ pgf_read_fid(PgfReader* rdr, PgfConcr* concr) ccat->cnccat = NULL; ccat->lindefs = NULL; ccat->n_synprods = 0; - ccat->prods = gu_null_seq; + ccat->prods = NULL; ccat->viterbi_prob = 0; ccat->fid = fid; ccat->conts = NULL; @@ -822,15 +822,15 @@ pgf_read_fid(PgfReader* rdr, PgfConcr* concr) static PgfCncFun* pgf_read_funid(PgfReader* rdr, PgfConcr* concr) { - int32_t funid = pgf_read_int(rdr); + size_t funid = pgf_read_int(rdr); gu_return_on_exn(rdr->err, NULL); - if (funid < 0 || funid >= gu_list_length(concr->cncfuns)) { + if (funid >= gu_seq_length(concr->cncfuns)) { gu_raise(rdr->err, PgfReadExn); return NULL; } - return gu_list_elems(concr->cncfuns)[funid]; + return gu_seq_get(concr->cncfuns, PgfCncFun*, funid); } static void @@ -845,10 +845,10 @@ pgf_read_lindefs(PgfReader* rdr, PgfConcr* concr) size_t n_funs = pgf_read_len(rdr); gu_return_on_exn(rdr->err, ); - ccat->lindefs = gu_new_list(PgfCncFuns, rdr->opool, n_funs); + ccat->lindefs = gu_new_seq(PgfCncFun*, n_funs, rdr->opool); for (size_t j = 0; j < n_funs; j++) { PgfCncFun* fun = pgf_read_funid(rdr, concr); - gu_list_index(ccat->lindefs, j) = fun; + gu_seq_set(ccat->lindefs, PgfCncFun*, j, fun); } } } @@ -859,9 +859,9 @@ pgf_read_parg(PgfReader* rdr, PgfConcr* concr, PgfPArg* parg) size_t n_hoas = pgf_read_len(rdr); gu_return_on_exn(rdr->err, ); - parg->hypos = gu_new_list(PgfCCats, rdr->opool, n_hoas); + parg->hypos = gu_new_seq(PgfCCat*, n_hoas, rdr->opool); for (size_t i = 0; i < n_hoas; i++) { - gu_list_index(parg->hypos, i) = pgf_read_fid(rdr, concr); + gu_seq_set(parg->hypos, PgfCCat*, i, pgf_read_fid(rdr, concr)); gu_return_on_exn(rdr->err, ); } @@ -869,13 +869,13 @@ pgf_read_parg(PgfReader* rdr, PgfConcr* concr, PgfPArg* parg) gu_return_on_exn(rdr->err, ); } -static PgfPArgs +static PgfPArgs* pgf_read_pargs(PgfReader* rdr, PgfConcr* concr) { size_t len = pgf_read_len(rdr); - gu_return_on_exn(rdr->err, gu_null_seq); + gu_return_on_exn(rdr->err, NULL); - PgfPArgs pargs = gu_new_seq(PgfPArg, len, rdr->opool); + PgfPArgs* pargs = gu_new_seq(PgfPArg, len, rdr->opool); for (size_t i = 0; i < len; i++) { PgfPArg* parg = gu_seq_index(pargs, PgfPArg, i); pgf_read_parg(rdr, concr, parg); @@ -967,7 +967,7 @@ pgf_read_cnccat(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, PgfCId name) gu_assert(cnccat->abscat != NULL); int len = last + 1 - first; - cnccat->cats = gu_new_list(PgfCCats, rdr->opool, len); + cnccat->cats = gu_new_seq(PgfCCat*, len, rdr->opool); for (int i = 0; i < len; i++) { int fid = first + i; @@ -977,7 +977,7 @@ pgf_read_cnccat(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, PgfCId name) ccat->cnccat = NULL; ccat->lindefs = NULL; ccat->n_synprods = 0; - ccat->prods = gu_null_seq; + ccat->prods = NULL; ccat->viterbi_prob = 0; ccat->fid = fid; ccat->conts = NULL; @@ -985,7 +985,7 @@ pgf_read_cnccat(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, PgfCId name) gu_map_put(concr->ccats, &fid, PgfCCat*, ccat); } - gu_list_index(cnccat->cats, i) = ccat; + gu_seq_set(cnccat->cats, PgfCCat*, i, ccat); ccat->cnccat = cnccat; } @@ -1079,7 +1079,7 @@ pgf_read_ccat_cb(GuMapItor* fn, const void* key, void* value, GuExn* err) PgfConcr *concr = clo->concr; GuPool *pool = clo->pool; - if (gu_seq_is_null(ccat->prods)) + if (ccat->prods == NULL) return; size_t n_prods = gu_seq_length(ccat->prods); diff --git a/src/ui/android/AndroidManifest.xml b/src/ui/android/AndroidManifest.xml index 6f48b1186..9fef8e112 100644 --- a/src/ui/android/AndroidManifest.xml +++ b/src/ui/android/AndroidManifest.xml @@ -2,7 +2,7 @@ + android:versionName="1.0"> + android:theme="@style/AppTheme"> diff --git a/src/ui/android/jni/Android.mk b/src/ui/android/jni/Android.mk index 8cf877fd7..213426974 100644 --- a/src/ui/android/jni/Android.mk +++ b/src/ui/android/jni/Android.mk @@ -4,7 +4,7 @@ include $(CLEAR_VARS) jni_c_files := jpgf.c pgf_c_files := data.c expr.c graphviz.c lexer.c linearizer.c literals.c parser.c parseval.c pgf.c printer.c reader.c reasoner.c jit.c -gu_c_files := assert.c choice.c exn.c fun.c in.c list.c map.c out.c str.c type.c utf8.c \ +gu_c_files := assert.c choice.c exn.c fun.c in.c map.c out.c str.c type.c utf8.c \ bits.c defs.c enum.c file.c hash.c mem.c prime.c seq.c string.c ucs.c variant.c LOCAL_MODULE := jpgf