Miscellaneous C Sequence Functions

Histogram

mco_seq_histogram_TYPE() counts values in n_intervals equal-width intervals over the half-open range [min_value, max_value). The result is a uint4 sequence.

    MCO_RET mco_seq_histogram_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_TYPE min_value,
            mco_TYPE max_value,
            mco_size_t n_intervals);

Permutation-Based Ordering

mco_seq_sort_TYPE() builds a permutation for an application array. It does not construct an iterator and does not rearrange elems. The order argument is MCO_SEQ_ASC_ORDER or MCO_SEQ_DESC_ORDER.

    MCO_RET mco_seq_sort_TYPE(
            mco_TYPE const* elems,
            mco_seq_no_t* permutation,
            mco_size_t n_elems,
            mco_seq_order_t order);

mco_seq_order_by_TYPE() creates a lazy iterator that reads elements in the supplied permutation order. If data is NULL, the function materializes input on first use and the resulting temporary buffer must later be released with mco_seq_free_buffer(). If data is not NULL, the iterator uses that application buffer; keep it alive and do not release it with mco_seq_free_buffer().

    MCO_RET mco_seq_order_by_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_seq_no_t const* permutation,
            mco_size_t n_elems,
            mco_TYPE* data);

The fixed-width character counterpart is mco_seq_order_by_char(). See Fixed-Width Character Sequences.

Application Callbacks

The callback families apply application code lazily as the result is traversed. The callback and its ctx object must remain valid until traversal is complete. Unary functions return a value of the same type; binary functions receive one value from each input; conditions return mco_seq_bool.

    typedef mco_TYPE (*mco_seq_func_TYPE_ptr_t)(mco_TYPE arg);
    typedef mco_TYPE (*mco_seq_func2_TYPE_ptr_t)(
            mco_TYPE arg1, mco_TYPE arg2);
    typedef mco_seq_bool (*mco_seq_cond_TYPE_ptr_t)(mco_TYPE arg);

    MCO_RET mco_seq_func_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_seq_func_TYPE_ptr_t func);

    MCO_RET mco_seq_func2_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h left,
            mco_seq_iterator_h right,
            mco_seq_func2_TYPE_ptr_t func);

    MCO_RET mco_seq_cond_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_seq_cond_TYPE_ptr_t func);

The context variants add a final void* argument to both the constructor and callback:

    typedef mco_TYPE (*mco_seq_func_ctx_TYPE_ptr_t)(
            mco_TYPE arg, void* ctx);
    typedef mco_TYPE (*mco_seq_func2_ctx_TYPE_ptr_t)(
            mco_TYPE arg1, mco_TYPE arg2, void* ctx);
    typedef mco_seq_bool (*mco_seq_cond_ctx_TYPE_ptr_t)(
            mco_TYPE arg, void* ctx);

    MCO_RET mco_seq_func_ctx_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_seq_func_ctx_TYPE_ptr_t func,
            void* ctx);

    MCO_RET mco_seq_func2_ctx_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h left,
            mco_seq_iterator_h right,
            mco_seq_func2_ctx_TYPE_ptr_t func,
            void* ctx);

    MCO_RET mco_seq_cond_ctx_TYPE(
            mco_seq_iterator_h result,
            mco_seq_iterator_h input,
            mco_seq_cond_ctx_TYPE_ptr_t func,
            void* ctx);

For binary callbacks, traversal stops when the shorter input is exhausted. NULL input elements propagate as NULL output elements without invoking the value callback.

Custom Text Parser

mco_seq_custom_parse() creates an iterator over a sequence literal and delegates conversion of each element to parser. The callback stores the parsed payload in tile position i and returns MCO_YES on success. Parsing and allocation errors can be reported only when the iterator is traversed.

    typedef mco_bool (*mco_parse_function_t)(
            char const* str,
            mco_seq_tile_t* tile,
            int i);

    MCO_RET mco_seq_custom_parse(
            mco_seq_iterator_h result,
            char const* input,
            int elem_type,
            int elem_size,
            mco_parse_function_t parser);

Use the element-type constants and sizes defined by mcoseq.h. A parser callback must not retain the temporary str pointer or tile pointer after it returns.

Related Topics