Sequence Manipulator C Functions

The following sequence manipulator C functions take a variety of input sequence arguments and produce result sequences as described in the table below:

mco_seq_filter_first_pos()

Return in integer sequence result the first n positions of true elements in the boolean sequence cond.

    MCO_RET mco_seq_filter_first_pos(
        mco_seq_iterator_h result,
        mco_seq_iterator_h cond,
        mco_size_t n
    );
mco_seq_reverse_TYPE()

Reverse the elements of the input sequence.

    MCO_RET mco_seq_reverse_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_concat()

Concatenate two sequences of any type by appending sequence right to sequence left. The result sequence will be of the same type as left and right.

    MCO_RET mco_seq_concat(
        mco_seq_iterator_h result,
        mco_seq_iterator_h left,
        mco_seq_iterator_h right
    );
mco_seq_cat()

Concatenate two sequences of any type by appending sequence right to sequence left. The result sequence will be of type char.

    MCO_RET mco_seq_cat(
        mco_seq_iterator_h result,
        mco_seq_iterator_h left,
        mco_seq_iterator_h right
    );
mco_seq_limit()

Copy to the result sequence the sub-sequence from the input using sequence positions from and till.

    MCO_RET mco_seq_limit(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        mco_seq_no_t from,
        mco_seq_no_t till
    );
mco_seq_get_TYPE()

Extract logical elements from non-nullable input into buf. On entry, size specifies the buffer capacity in elements. On return, it contains the number of elements written. For nullable input, use mco_seq_get_nullable_TYPE() so positions and NULL state are preserved.

    MCO_RET mco_seq_get_TYPE(
        mco_seq_iterator_h input,
        mco_TYPE* buf,
        mco_size_t* size
    );
mco_seq_get_nullable_TYPE()

Extract logical elements from input while preserving their positions and NULL state. On entry, size specifies the capacity of buf in elements. On return, it contains the number of elements written. The function clears and fills null_bitmask; bit n is set when element n is NULL. Allocate at least (capacity + 63) / 64 words.

    MCO_RET mco_seq_get_nullable_TYPE(
        mco_seq_iterator_h input,
        mco_TYPE* buf,
        mco_size_t* size,
        mco_bitmap_word_t* null_bitmask
    );
mco_seq_unget_TYPE()

Create a lazy iterator over buf. The function does not copy the elements. Parameter buf_size is the number of elements, not the number of bytes. The buffer must remain valid and unchanged until the iterator and every pipeline that depends on it have finished. Do not pass this iterator to mco_seq_free_buffer(); the application owns the supplied buffer.

    MCO_RET mco_seq_unget_TYPE(
        mco_seq_iterator_h result,
        mco_TYPE const* buf,
        mco_size_t buf_size
    );
mco_seq_unget_nullable_TYPE()

Create a lazy iterator over buf and its NULL bitmap. Neither buffer is copied. Parameter buf_size is the number of elements, and bit n in null_bitmask marks element n as NULL. Both buffers must remain valid and unchanged until the iterator and every dependent pipeline have finished. Do not pass this iterator to mco_seq_free_buffer().

    MCO_RET mco_seq_unget_nullable_TYPE(
        mco_seq_iterator_h result,
        mco_TYPE const* buf,
        mco_size_t buf_size,
        mco_bitmap_word_t const* null_bitmask
    );
mco_seq_unique_TYPE()

Copy to the result sequence only the unique elements in ordered sequence input by "collapsing duplicates".

    MCO_RET mco_seq_unique_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_skip_nan_float(), mco_seq_skip_nan_double()

Copy the input sequence to result, skipping NaN (Not-a-Number) elements. This operation is available only for float and double. See the skip NaN functions page for details.

    MCO_RET mco_seq_skip_nan_float(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );

    MCO_RET mco_seq_skip_nan_double(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_norm_TYPE()

Return in result the normalized input sequence; i.e., divide each sequence element by the square root of the sum of squares of all elements.

    MCO_RET mco_seq_norm_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_thin_TYPE()

Copy the input sequence to result selecting elements with the specified origin and step.

    MCO_RET mco_seq_thin_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        mco_size_t origin,
        mco_size_t step
    );
mco_seq_diff_TYPE()

Return in result the differences between pairs of sequence elements in input.

    MCO_RET mco_seq_diff_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_trend_TYPE()

Return in result the "trend" by comparing pairs of sequence elements in input: the sign of the difference between pairs of non-equal sequence elements. For example, the input sequence {1,2,3,3,2,2,4,5,6,5,5} would produce the result {0,1,1,1,-1,-1,1,1,1,-1,-1}.

    MCO_RET mco_seq_trend_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input
    );
mco_seq_const_TYPE()

Construct sequence result with elements having the constant value val. There is no length argument, so the result is unbounded; a consumer must bound it explicitly and can continue calling the corresponding mco_seq_next_TYPE() indefinitely.

    MCO_RET mco_seq_const_TYPE(
        mco_seq_iterator_h result,
        TYPE val
    );
mco_seq_parse_TYPE()

Construct sequence result from the string literal str, e.g., "{1.0, -1.1, 0}".

    MCO_RET mco_seq_parse_TYPE(
        mco_seq_iterator_h result,
        char const* str
    );
mco_seq_join_TYPE()

Use a metaiterator to combine multiple sequences. (Please see the seq_join function page for a more detailed explanation.)

    MCO_RET mco_seq_join_TYPE(
        mco_seq_iterator_h result,
        mco_seq_meta_iterator_h meta_iterator
    );
mco_seq_union_TYPE()

Return in result the union of the two ordered sequences left and right using the specified order. Note that result will contain different elements from both input sequences.

    MCO_RET mco_seq_union_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h left,
        mco_seq_iterator_h right,
        mco_seq_order_t order
    );
mco_seq_tee()

Create two iterators for the same input sequence (i.e., fork the sequence iterator). (Note that these iterators should be traversed together, i.e., used in the same expression). The input sequence can be of any type.

    MCO_RET mco_seq_tee(
        mco_seq_iterator_h iterator1,
        mco_seq_iterator_h iterator2,
        mco_seq_iterator_h input
    );
mco_seq_map_TYPE()

Return in result the elements extracted from the input sequence using the positions specified in the positions sequence. Positions must be provided in ascending order. Most position-producing functions, such as mco_seq_filter_pos(), satisfy this requirement; the ranked results from mco_seq_top_pos_max_TYPE() and mco_seq_top_pos_min_TYPE() generally do not and must be reordered before use here.

    MCO_RET mco_seq_map_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        mco_seq_iterator_h positions
    );
mco_seq_aprogres_TYPE()

Construct a finite arithmetic progression of count elements. Element n is init_value + n * step. The spelling aprogres is part of the public C API.

    MCO_RET mco_seq_aprogres_TYPE(
        mco_seq_iterator_h result,
        mco_TYPE init_value,
        mco_TYPE step,
        mco_seq_no_t count
    );
mco_seq_repeat_TYPE()

Return in result each element of the input sequence repeated n_times times.

    MCO_RET mco_seq_repeat_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        int n_times
    );
mco_seq_stretch_TYPE1_TYPE2()

Return in result the elements of the values sequence stretched to the length of the ts1 sequence by repeating the elements of values while the corresponding timestamp in sequence ts2 is larger than timestamp from ts1. Use the filler values for elements where the corresponding timestamp in sequence ts2 is less than timestamp from ts1.

For example, assume ts1 = {1,2,3,4,5}, ts2 = {2,4}, values = {1.1,2.2} and filler = 1.0. With these input sequences the result will be {1.1,2.2,2.2,1.0,1.0}.

(This function can be used to calculate split adjusted price where it is necessary to revert time series of splits, calculate the cumulative product and multiply on price.)

    MCO_RET mco_seq_stretch_TYPE1_TYPE2(
        mco_seq_iterator_h result,
        mco_seq_iterator_h ts1,
        mco_seq_iterator_h ts2,
        mco_seq_iterator_h values,
        mco_TYPE2 filler
    );
mco_seq_stretch0_TYPE1_TYPE2()

Return in result the elements of the values and filler sequences by injecting the elements of values where the corresponding timestamps in sequence ts2 match the timestamp from ts1. Use the filler values for elements where the timestamp in sequence ts2 has no matching timestamp in ts1.

For example, assume ts1 = {1,2,3,5}, ts2 = {2,3,4}, values = {1.1,1.2,1.3} and filler = 0.0. With these input sequences the result will be {0.0,1.1,1.2,1.3,0.0}.

    MCO_RET mco_seq_stretch0_TYPE1_TYPE2(
        mco_seq_iterator_h result,
        mco_seq_iterator_h ts1,
        mco_seq_iterator_h ts2,
        mco_seq_iterator_h values,
        mco_TYPE2 filler
    );
mco_seq_asof_join_TYPE1_TYPE2()

Return in result the elements of the values sequence corresponding to the timestamp of ts2 closest to the timestamp of ts1.

For example, assume ts1 = {4,9}, ts2 = {1,3,6,10}, and values = {0.1,0.3,0.6,1.0}. With these input sequences the result will be {0.3,1.0}.

For example, mco_seq_asof_join_uint4_float() uses uint4 timestamp sequences and a float value sequence.

    MCO_RET mco_seq_asof_join_TYPE1_TYPE2(
        mco_seq_iterator_h result,
        mco_seq_iterator_h ts1,
        mco_seq_iterator_h ts2,
        mco_seq_iterator_h values
    );
mco_seq_cross_TYPE()

Return in result the positions in the input sequence where it crosses zero. If the integer argument first_cross_direction is positive then the result sequence starts with the first cross above zero; if negative it starts with cross below zero; if first_cross_direction is zero then it doesn't matter, i.e., the first cross can be above or below zero.

    MCO_RET mco_seq_cross_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        int first_cross_direction
    );
mco_seq_extrema_TYPE()

Return in result the positions of extrema (local minimum and maximums) in the input sequence. If the integer argument first_extremum is positive then the result sequence starts with first local maximum; if negative it starts with local minimum; if first_extremum is zero then it doesn't matter.

    MCO_RET mco_seq_extrema_TYPE(
        mco_seq_iterator_h result,
        mco_seq_iterator_h input,
        int first_extremum
    );

Example

Following is an example code snippet demonstrating a sequence manipulator function:

    {
        mco_trans_h trans;
        mco_cursor_t quote_cursor;
        Quote quote;
        mco_seq_iterator_t high_iterator, low_iterator, result_iterator;
        MCO_RET rc;
        ...

        for (rc = mco_cursor_first(trans, &quote_cursor);
             rc != MCO_S_CURSOR_END;
             rc = mco_cursor_next(trans, &quote_cursor))
        {
            Quote_from_cursor(trans, &quote_cursor, &quote);
            Quote_high_iterator(&quote, &high_iterator);
            Quote_low_iterator(&quote, &low_iterator);
            ...
            rc = mco_seq_cross_float(&result_iterator, &low_iterator, &high_iterator);

            ...
        }
        ...
    }