algo

Overview

Collection of code generation functions that are designed to be used on slices.

Code Generation Macros

check

CMP_SLICE_C_

#define CMP_SLICE_C_(                                                          \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that compares two slices by comparing all the elements with the CmpFunc function.

FuncName

the name of the generated function

SliceType

the type of the slices

ValueType

the type of the elements in the slices

CmpFunc

compare function that takes the slice elements

DoDeref

can be "*" or do_deref_c_ to dereference the pointer of the slice elements for the compare function

The generated function returns a compare function result and has the following signature:

int FuncName( SliceType a, SliceType b );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   expect_eq_c_( cmp_chars_c( c_c( "Hello world!" ),
                              c_c( "Hello world!" ) ) );

   expect_lt_c_( cmp_chars_c( c_c( "Hello" ),
                              c_c( "Hello world!" ) ) );
   expect_gt_c_( cmp_chars_c( c_c( "Hello world!" ),
                              c_c( "Hello" ) ) );

   expect_lt_c_( cmp_chars_c( c_c( "Hello world!" ),
                              c_c( "world" ) ) );
   expect_gt_c_( cmp_chars_c( c_c( "world" ),
                              c_c( "Hello world!" ) ) );

   return finish_tap_c_();
}

COUNT_EQ_C_

#define COUNT_EQ_C_(                                                           \
   FuncName, SliceType, ValueType, SearchType, CmpFunc, DoDeref                \
)

Generates a function that counts the number of elements that are equal the searched value val.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

SearchType

the type of the search value

CmpFunc

compare function that takes the slice element as the first and search value as the second parameter

DoDeref

can be "*" or do_deref_c_ to dereference the pointer for the element in the slice for the compare function

The generated function returns the number of elements that match and has the following signature:

int64_t FuncName( SliceType slice, SearchType val );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars cheese = c_c( "cheese" );
   expect_c_( count_eq_char_c( cheese, 'e' ) == 3 );
   expect_c_( count_eq_char_c( cheese, 'x' ) == 0 );

   cChars text = c_c( "The quick brown Fox jumps over the lazy Dog." );
   expect_c_( count_eq_char_c( text, ' ' ) == 8 );
   expect_c_( count_eq_char_c( text, 'o' ) == 4 );
   expect_c_( count_eq_char_c( text, 'T' ) == 1 );
   expect_c_( count_eq_char_c( text, 't' ) == 1 );

   return finish_tap_c_();
}

ENDS_WITH_C_

#define ENDS_WITH_C_(                                                          \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that checks if a slice ends with the elements of a specified slice.

FuncName

the name of the generated function

SliceType

the type of the slices

ValueType

the type of the elements in the slices

CmpFunc

compare function that takes the slice elements

DoDeref

can be "*" or do_deref_c_ to dereference the pointer of the slice elements for the compare function

The generated function returns true if the slice ends with the elements from the needle and has the following signature:

bool FuncName( SliceType slice, SliceType needle );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "James Bond" );
   expect_c_(  chars_ends_with_c_( chars, "Bond" ) );
   expect_c_( !chars_ends_with_c_( chars, "James" ) );

   expect_c_( !chars_ends_with_c_( chars, "bond" ) );

   expect_c_(  chars_ends_with_c(  chars, chars ) );

   return finish_tap_c_();
}

STARTS_WITH_C_

#define STARTS_WITH_C_(                                                        \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that checks if a slice starts with the elements of a specified slice.

FuncName

the name of the generated function

SliceType

the type of the slices

ValueType

the type of the elements in the slices

CmpFunc

compare function that takes the slice elements

DoDeref

can be "*" or do_deref_c_ to dereference the pointer of the slice elements for the compare function

The generated function returns true if the slice starts with the elements from the needle and has the following signature:

bool FuncName( SliceType slice, SliceType needle );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "James Bond" );
   expect_c_(  chars_starts_with_c_( chars, "James" ) );
   expect_c_( !chars_starts_with_c_( chars, "Bond" ) );

   expect_c_( !chars_starts_with_c_( chars, "james" ) );

   expect_c_(  chars_starts_with_c(  chars, chars ) );

   return finish_tap_c_();
}

find

BSEARCH_C_

#define BSEARCH_C_(                                                            \
   FuncName, SliceType, ValueType, SearchType, CmpFunc, DoDeref                \
)

Generates a function that searches through a sorted slice for a value that is equal the searched value val. It is an abstract implementation of the binary search algorithm.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

SearchType

the type of the search value

CmpFunc

compare function that takes the slice element as the first and search value as the second parameter

DoDeref

can be "*" or do_deref_c_ to dereference the pointer for the element in the slice for the compare function

The generated function returns a pointer to the matching element and has the following common signature:

Type* FuncName( SliceType slice, SearchType val );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/int32.h" // bsearch_int32_c

int main( void )
{
   init_tap_c_();

   tap_note_c( "even slice test" );
   {
      cInt32Slice evenSlice = slice_c_( int32_t, -30, -20, -10, 0, 10, 20, 30, 40 );
      int32_t val = -30;
      for ( int64_t index = 0; index < evenSlice.s; ++index )
      {
         int32_t const* ptr = bsearch_int32_c( evenSlice, val );
         expect_c_( index_of_c_( evenSlice, ptr ) == index );
         val += 10;
      }
   }

   tap_note_c( "odd slice test" );
   {
      cInt32Slice oddSlice = slice_c_( int32_t, -30, -20, -10, 0, 10, 20, 30 );
      int32_t val = -30;
      for ( int64_t index = 0; index < oddSlice.s; ++index )
      {
         int32_t const* ptr = bsearch_int32_c( oddSlice, val );
         expect_c_( index_of_c_( oddSlice, ptr ) == index );
         val += 10;
      }
   }

   return finish_tap_c_();
}

FIND_MAX_C_

#define FIND_MAX_C_(                                                           \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that selects the greatest element in a slice. The function returns a pointer to the greatest element in the slice. If several elements in the slice are equivalent to the greatest element, returns the function a pointer to the first such element. Returns a NULL pointer if the slice is empty.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

CmpFunc

compare function that takes the slice element as the first and search value as the second parameter

DoDeref

can be "*" or do_deref_c_ to dereference the pointer for the element in the slice for the compare function

The generated function has the following common signature:

ValueType* FuncName( SliceType slice );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/int32.h"

int main( void )
{
   init_tap_c_();

   cInt32Slice slice = slice_c_( int32_t, 50, 123456, 99, 123456, -65 );

   int32_t const* max = max_int32_c( slice );
   expect_c_( index_of_c_( slice, max ) == 1 );
   expect_c_( *max == 123456 );

   return finish_tap_c_();
}

FIND_MIN_C_

#define FIND_MIN_C_(                                                           \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that selects the greatest element in a slice. The function returns a pointer to the smallest element in the slice. If several elements in the slice are equivalent to the smallest element, returns the function a pointer to the first such element. Returns a NULL pointer if the slice empty.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

CmpFunc

compare function that takes the slice element as the first and search value as the second parameter

DoDeref

can be "*" or do_deref_c_ to dereference the pointer for the element in the slice for the compare function

The generated function has the following common signature:

ValueType* FuncName( SliceType slice );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/uint32.h"

int main( void )
{
   init_tap_c_();

   cUint32Slice slice = slice_c_( uint32_t, 123456, 5648, 658, 79456, 658 );

   uint32_t const* min = min_uint32_c( slice );
   expect_c_( index_of_c_( slice, min ) == 2 );
   expect_c_( *min == 658 );

   return finish_tap_c_();
}

FIND_VAL_C_

#define FIND_VAL_C_(                                                           \
   FuncName, SliceType, ValueType, SearchType, CmpFunc, DoDeref                \
)

Generates a function that select the first element that is equal the searched value val. The function returns a pointer to the first element that matches and a NULL pointer if the searched value does not exist in the slice.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

SearchType

the type of the search value

CmpFunc

compare function that takes the slice element as the first and search value as the second parameter

DoDeref

can be "*" or do_deref_c_ to dereference the pointer for the element in the slice for the compare function

The generated function has the following common signature:

ValueType* FuncName( SliceType slice, SearchType val );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "abcdefgh" );
   char const* ptr = NULL;

   ptr = find_char_c( chars, 'a' ); // --------------------------------------- a
   expect_c_( *ptr == 'a' );
   expect_c_( index_of_c_( chars, ptr ) == 0 );

   ptr = find_char_c( chars, 'b' ); // --------------------------------------- b
   expect_c_( *ptr == 'b' );
   expect_c_( index_of_c_( chars, ptr ) == 1 );

   ptr = find_char_c( chars, 'e' ); // --------------------------------------- e
   expect_c_( *ptr == 'e' );
   expect_c_( index_of_c_( chars, ptr ) == 4 );

   ptr = find_char_c( chars, 'h' ); // --------------------------------------- h
   expect_c_( *ptr == 'h' );
   expect_c_( index_of_c_( chars, ptr ) == 7 );

   ptr = find_char_c( chars, 'x' ); // --------------------------------------- x
   expect_c_( ptr == NULL );
   expect_c_( not points_into_c_( chars, ptr ) );

   return finish_tap_c_();
}

INDEX_OF_SLICE_C_

#define INDEX_OF_SLICE_C_(                                                     \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that returns the index where the sequence of the sub slice starts.

FuncName

the name of the generated function

SliceType

the type of the slices

ValueType

the type of the elements in the slices

CmpFunc

compare function that takes the slice elements

DoDeref

can be "*" or do_deref_c_ to dereference the pointer of the slice elements for the compare function

The generated function returns a positive index if the sub slice exist, otherwise returns the function -1.

int64_t FuncName( SliceType slice, SliceType sub );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "abcdefgh" );
   int64_t idx = 0;

   idx = index_of_chars_c( chars, c_c( "abc" ) ); // ---------------------- abc
   expect_c_( idx == 0 );

   idx = index_of_chars_c_( chars, "cdef" ); // --------------------------- cdef
   expect_c_( idx == 2 );

   idx = index_of_chars_c_( chars, "defgh" ); // ------------------------- defgh
   expect_c_( idx == 3 );

   idx = index_of_chars_c_( chars, "h" ); // --------------------------------- h
   expect_c_( idx == 7 );

   idx = index_of_chars_c_( chars, "BCD" ); // ----------------------------- BCD
   expect_c_( idx == -1 );

   idx = index_of_chars_c_( chars, "ghij" ); // --------------------------- ghij
   expect_c_( idx == -1 );

   return finish_tap_c_();
}

manipulate

INSERT_SLICE_C_

#define INSERT_SLICE_C_(                                                       \
   FuncName, VarSliceType, SliceType                                           \
)

Generates a function that allows to insert the elements from the source slice into the destination slice. The existing values will be moved, the overhang values at the end will be removed.

FuncName

the name of the generated function

VarSliceType

the type of the destination slice

SliceType

the type of the source slice

The generated function returns 1 if the value is inserted, otherwise 0.

int64_t FuncName( VarSliceType dst, int64_t index, SliceType src );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

#define expect_( VarChars, Exp )                                               \
   expect_c_(                                                                  \
      chars_is_c( as_chars_c( VarChars ), (Exp) )                              \
   )

int main( void )
{
   init_tap_c_();

   cVarChars allChars = scalars_c_( 14, char );
   set_chars_c_( allChars, "#abcdefghijkl#" );

   cVarChars subChars = sub_var_chars_c( allChars, 1, 13 );

   // ----------------------------------------------------- insert in the middle
   expect_c_( insert_chars_c( subChars, 1, c_c( "AA" ) ) == 2 );
   expect_( allChars, "#aAAbcdefghij#" );

   expect_c_( insert_chars_c_( subChars, 4, "BBB" ) == 3 );
   expect_( allChars, "#aAAbBBBcdefg#" );

   expect_c_( insert_chars_c_( subChars, 8, "CCCC" ) == 4 );
   expect_( allChars, "#aAAbBBBcCCCC#" );

   // ---------------------------------------------------- insert at the corners
   expect_c_( insert_chars_c_( subChars, 0, "_" ) == 1 );
   expect_( allChars, "#_aAAbBBBcCCC#" );

   expect_c_( insert_chars_c_( subChars, 10, "d___" ) == 2 );
   expect_( allChars, "#_aAAbBBBcCd_#" );

   // ---------------------------------------------------- try to insert outside
   expect_c_( insert_chars_c_( subChars, -1, "xxx" ) == 0 );
   expect_( allChars, "#_aAAbBBBcCd_#" );

   expect_c_( insert_chars_c_( subChars, 12, "xxx" ) == 0 );
   expect_( allChars, "#_aAAbBBBcCd_#" );

   return finish_tap_c_();
}

INSERT_VAL_C_

#define INSERT_VAL_C_(                                                         \
   FuncName, VarSliceType, ValueType, InsertType, DoDeref                      \
)

Generates a function that allows to insert a value into a slice. The existing values will be moved, the overhang value at the end will be removed.

FuncName

the name of the generated function

VarSliceType

the type of the slice

ValueType

the type of the elements in the slice

InsertType

the type of the parameter

DoDeref

can be "*" to dereference the pointer of the InsertType

The generated function returns the number of elements that are inserted into the dst slice, otherwise 0.

int64_t FuncName( VarSliceType slice, int64_t index, InsertType val );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

#define expect_( VarChars, Exp )                                               \
   expect_c_(                                                                  \
      chars_is_c( as_chars_c( VarChars ), (Exp) )                              \
   )

int main( void )
{
   init_tap_c_();

   cVarChars allChars = scalars_c_( 8, char );
   set_chars_c_( allChars, "#abcdef#" );

   cVarChars subChars = sub_var_chars_c( allChars, 1, 7 );

   // ----------------------------------------------------- insert in the middle
   expect_c_( insert_char_c( subChars, 1, '+' ) == 1 );
   expect_( allChars, "#a+bcde#" );

   expect_c_( insert_char_c( subChars, 3, '-' ) == 1 );
   expect_( allChars, "#a+b-cd#" );

   // ---------------------------------------------------- insert at the corners
   expect_c_( insert_char_c( subChars, 0, '$' ) == 1 );
   expect_( allChars, "#$a+b-c#" );

   expect_c_( insert_char_c( subChars, 5, 'X' ) == 1 );
   expect_( allChars, "#$a+b-X#" );

   // ---------------------------------------------------- try to insert outside
   expect_c_( insert_char_c( subChars, -1, '_' ) == 0 );
   expect_( allChars, "#$a+b-X#" );

   expect_c_( insert_char_c( subChars, 6, '_' ) == 0 );
   expect_( allChars, "#$a+b-X#" );

   return finish_tap_c_();
}

QSORT_C_

#define QSORT_C_(                                                              \
   FuncName, SliceType, ValueType, CmpFunc, DoDeref                            \
)

Generates a function that sorts the elements in the slice into ascending order. Equivalent elements are not guaranteed to keep their original relative order.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

CmpFunc

compare function that takes the slice elements

DoDeref

can be "*" or do_deref_c_ to dereference the pointer of the slice elements for the compare function

The generated function has the following common signature:

void FuncName( SliceType slice );
Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/char.h"

#define expect_( VarChars, Exp )                                               \
   expect_c_(                                                                  \
      chars_is_c( as_chars_c( VarChars ), (Exp) )                              \
   )

int main( void )
{
   init_tap_c_();

   cVarChars chars = scalars_c_( 128, char );

   //---------------------------------------------------------- sort short slice

   cVarChars quicksort = chars;
   quicksort.s = set_chars_c_( quicksort, "quicksort" );

   qsort_chars_c( quicksort );

   expect_( quicksort, "cikoqrstu" );

   //----------------------------------------- sort slice with many equal values

   cRecorder* rec = &make_recorder_c_( chars.s, chars.v );
   times_c_( 4, xyz )
   {
      record_chars_c_( rec, "abcdefghijklmnopqrstuvwxyz" );
   }
   cVarChars abcx4 = recorded_var_chars_c( rec );

   qsort_chars_c( abcx4 );

   expect_c_( recorded_is_c( rec, "aaaabbbbccccddddeeeeffffgggghhhhiiii"
                                  "jjjjkkkkllllmmmmnnnnooooppppqqqq"
                                  "rrrrssssttttuuuuvvvvwwwwxxxxyyyyzzzz" ) );

   printf( "res: %s\n", quicksort.v );

   return finish_tap_c_();
}

REVERSE_C_

#define REVERSE_C_(                                                            \
   FuncName, SliceType, ValueType                                              \
)

Generates a function that reverses the order of the elements in the slice.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

The generated function has the following common signature:

void FuncName( SliceType slice );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

#define expect_( VarChars, Exp )                                               \
   expect_c_(                                                                  \
      chars_is_c( as_chars_c( VarChars ), (Exp) )                              \
   )

int main( void )
{
   init_tap_c_();

   cVarChars a = slice_c_( char, 'a' );
   reverse_chars_c( a );
   expect_( a, "a" );

   cVarChars ab = slice_c_( char, 'a', 'b' );
   reverse_chars_c( ab );
   expect_( ab, "ba" );

   cVarChars abc = slice_c_( char, 'a', 'b', 'c' );
   reverse_chars_c( abc );
   expect_( abc, "cba" );

   cVarChars abcd = slice_c_( char, 'a', 'b', 'c', 'd' );
   reverse_chars_c( abcd );
   expect_( abcd, "dcba" );

   return finish_tap_c_();
}

ROTATE_C_

#define ROTATE_C_(                                                             \
   FuncName, SliceType, ValueType                                              \
)

Generates a function that allows to rotate the elements in the slice. A positive value rotates the elements to the right, a negative value rotates the elements to the left.

FuncName

the name of the generated function

SliceType

the type of the slice

ValueType

the type of the elements in the slice

The generated function has the following common signature:

void FuncName( SliceType slice, int64_t distance );
Example
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

#define expect_( VarChars, Exp )                                               \
   expect_c_(                                                                  \
      chars_is_c( as_chars_c( VarChars ), (Exp) )                              \
   )

int main( void )
{
   init_tap_c_();

   cVarChars chars = slice_c_( char, 'a', 'B', 'c', 'D', 'e', 'F' );

   // rotate left and right
   rotate_chars_c( chars, -2 );
   expect_( chars, "cDeFaB" );

   rotate_chars_c( chars, 2 );
   expect_( chars, "aBcDeF" );

   // handle full rotations correct
   rotate_chars_c( chars, 0 );
   expect_( chars, "aBcDeF" );

   rotate_chars_c( chars, 6 );
   expect_( chars, "aBcDeF" );

   // handle multiple time rotations right
   rotate_chars_c( chars, 9 );
   expect_( chars, "DeFaBc" );

   rotate_chars_c( chars, -27 );
   expect_( chars, "aBcDeF" );

   return finish_tap_c_();
}

Functions

do

do_deref_c_

#define do_deref_c_( Ptr )

Macro function that dereferences a pointer.

do_ref_c_

#define do_ref_c_( Val )

Macro function that returns the pointer to a value.

do_nothing_c_

#define do_nothing_c_( Val )

Macro function that does nothing with the value, it even does not return Val.

do_not_deref_c_

#define do_not_deref_c_( Ptr )

Macro function that returns Ptr.

do_not_ref_c_

#define do_not_ref_c_( Val )

Macro function that returns Val.