slice

Overview

A slice is a abstract struct that refers to a segment of an array. Each struct that has an attribute s (defines the sequence length) and v (points to the first value of the sequence) can be viewed as a slice.

A slice does not own the referenced sequence, this allows to point with different slices at the same array.

slice-pineapple.svg

It is best practice to define slice structs that just have the s and the v attribute. It is also recommended to use two kind of slices for a type or struct. A slice type where v is a pointer to const data and a slice that points to variable data.

Data Type Const Data Slice Variable Data Slice

char

cChars

cVarChars

int8_t

cInt8Slice

cVarInt8Slice

This module defines code generation macros to generate typed slice structs. Also has this module macro functions that work with all structs that can be viewed as a slice.

Code Generation Macros

init

EMPTY_SLICE_C_

#define EMPTY_SLICE_C_( FuncName, SliceType )

Generates a constructor function that returns a empty slice where s is 0 and v is NULL. The generated function has the following common signature:

SliceType FuncName( void );

MAKE_SLICE_C_

#define MAKE_SLICE_C_( FuncName, SliceType, Type )

Generates a constructor function that returns a slice that is defined with a pointer at the first element and a pointer after the last element. The generated function has the following common signature:

SliceType FuncName( Type* beg, Type* end )
Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars cs = c_c( "abcdef" );

   char const* beg = begin_c_( cs );
   char const* end = end_c_( cs );

   cChars abcdef = make_chars_c( beg, end );
   expect_c_( chars_is_c( abcdef, "abcdef" ) );

   ++beg;
   --end;
   cChars bcde = make_chars_c( beg, end );
   expect_c_( chars_is_c( bcde, "bcde" ) );

   return finish_tap_c_();
}

SLICE_C_

#define SLICE_C_( FuncName, SliceType, Type )

Generates a constructor function that returns a slice where the s and v values are set. The generated function has the following common signature:

SliceType FuncName( int64_t s, Type* v );

sub

LEFT_SLICE_C_

#define LEFT_SLICE_C_( FuncName, SliceType )

Generates a function that returns a sub-slice that contains the leftmost values of a slice.

The generated function has the following common signature:

SliceType FuncName( SliceType slice, int64_t maxLen )

If maxLen is greater or equal the s value of slice, contains the sub-slice all values from the slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"
#include "clingo/type/int64.h" // for cInt64Slice

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cChars chars = c_c( "Pineapple" );

   cChars pine = left_chars_c( chars, 4 );
   expect_c_( chars_is_c( pine, "Pine" ) );

   cChars pineapple = left_chars_c( chars, 128 );
   expect_c_( chars_is_c( pineapple, "Pineapple" ) );

   cChars empty = left_chars_c( chars, 0 );
   expect_c_( is_empty_c_( empty ) );

   // -------------------------------------------------------------- cInt64Slice
   cInt64Slice int64s = slice_c_( int64_t, 123, -456, 789, 369, 246 );

   cInt64Slice three = left_int64_slice_c( int64s, 3 );
   expect_c_( three.s == 3 );
   expect_c_( three.v[ 0 ] == 123 );
   expect_c_( three.v[ 2 ] == 789 );

   return finish_tap_c_();
}

MID_SLICE_C_

#define MID_SLICE_C_( FuncName, SliceType )

Generates a function that returns a sub-slice starting(including) at a given index.

The generated function has the following common signature:

SliceType FuncName( SliceType slice, int64_t index )

If index does not point into the slice returns the function an empty slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"
#include "clingo/type/int16.h" // for cInt16Slice

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cChars chars = c_c( "Nine pineapples" );

   cChars pineapples = mid_chars_c( chars, 5 );
   expect_c_( chars_is_c( pineapples, "pineapples" ) );

   cChars apples = mid_chars_c( chars, 9 );
   expect_c_( chars_is_c( apples, "apples" ) );

   cChars nullSlice = mid_chars_c( chars, chars.s );
   expect_c_( is_empty_c_( nullSlice ) );

   // -------------------------------------------------------------- cInt16Slice
   cInt16Slice int16s = slice_c_( int16_t, 8, -16, 128, -1024, 512 );

   cInt16Slice two = mid_int16_slice_c( int16s, 3 );
   expect_c_( two.s == 2 );
   expect_c_( two.v[ 0 ] == -1024 );
   expect_c_( two.v[ 1 ] == 512 );

   return finish_tap_c_();
}

RIGHT_SLICE_C_

#define RIGHT_SLICE_C_( FuncName, SliceType )

Generates a function that return a sub-slice that contains the rightmost values of a slice. The generated function has the following common signature:

SliceType FuncName( SliceType slice, int64_t maxLen )

If maxLen is greater or equal as the s value of slice, contains the sub-slice all values from the slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"
#include "clingo/type/int8.h" // for cInt8Slice

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cChars chars = c_c( "Pineapple" );

   cChars pple = right_chars_c( chars, 4 );
   expect_c_( chars_is_c( pple, "pple" ) );

   cChars pineapple = right_chars_c( chars, 12 );
   expect_c_( chars_is_c( pineapple, "Pineapple" ) );

   cChars empty = right_chars_c( chars, 0 );
   expect_c_( is_empty_c_( empty ) );

   // --------------------------------------------------------------- cInt8Slice
   cInt8Slice int8s = slice_c_( int8_t, 8, 27, -99, 23, 77, 80, -111 );

   cInt8Slice four = right_int8_slice_c( int8s, 4 );
   expect_c_( four.s == 4 );
   expect_c_( four.v[ 0 ] == 23 );
   expect_c_( four.v[ 3 ] == -111 );

   return finish_tap_c_();
}

SUB_SLICE_C_

#define SUB_SLICE_C_( FuncName, SliceType )

Generates a function that returns a sub-slice of a given slice. The generated function has the following common signature:

SliceType FuncName( SliceType slice, int64_t begIdx, int64_t endIdx )

The returned sub-slice includes the value at begIdx(begin index) as first value and the value at endIdx-1 as last value. If begIdx or endIdx are not valid indices returns the function a empty slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"
#include "clingo/type/int32.h" // for cInt16Slice

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cChars chars = c_c( "Nine pineapples" );

   cChars nine = sub_chars_c( chars, 0, 4 );
   expect_c_( chars_is_c( nine, "Nine" ) );

   cChars pineapple = sub_chars_c( chars, 5, 15 );
   expect_c_( chars_is_c( pineapple, "pineapples" ) );

   cChars emptyChars = sub_chars_c( chars, 6, 16 );
   expect_c_( is_empty_c_( emptyChars ) );

   // -------------------------------------------------------------- cInt32Slice
   cInt32Slice int32s = slice_c_( int32_t, -123, 4356, 876, -3, 98676 );

   cInt32Slice three = sub_int32_slice_c( int32s, 1, 4 );
   expect_c_( three.s == 3 );
   expect_c_( three.v[ 0 ] == 4356 );
   expect_c_( three.v[ 2 ] == -3 );

   cInt32Slice one = sub_int32_slice_c( int32s, 2, 3 );
   expect_c_( one.s == 1 );
   expect_c_( one.v[ 0 ] == 876 );

   return finish_tap_c_();
}

var slice

AS_SLICE_C_

#define AS_SLICE_C_( FuncName, SliceType, VarSliceType )

Generates a function that converts a slice with variable data to a slice with const data. The generated function has the following common signature:

SliceType FuncName( VarSliceType slice )

CAST_SLICE_C_

#define CAST_SLICE_C_( FuncName, VarSliceType, SliceType )

Generates a function that converts a slice with const data to a slice with variable data. The generated function has the following common signature:

VarSliceType FuncName( VarSliceType slice, SliceType sub );

The function returns an empty slice if sub is not a complete subset of slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/char.h"

int main( void )
{
   init_tap_c_();

   return finish_tap_c_();
}

SET_SLICE_C_

#define SET_SLICE_C_( FuncName, VarSliceType, SliceType )

Generates a function that allows to set the variable data with the values of a slice witch const data. The generated function has the following common signature:

int64_t FuncName( VarSliceType dest, SliceType src )

The function set at most all values in dest, never more. The return value shows how many values are set.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

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

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cVarChars dest = scalars_c_( 8, char );
   dest.s = set_chars_c_( dest, "abcdef" );
   expect_cs_( dest, "abcdef" );

   dest.s = 8;
   dest.s = set_chars_c_( dest, "abcdefgh" );
   expect_cs_( dest, "abcdefgh" );

   dest.s = 8;
   dest.s = set_chars_c_( dest, "abcdefghij" );
   expect_cs_( dest, "abcdefgh" );

   return finish_tap_c_();
}

type

SLICE_DEF_C_

#define SLICE_DEF_C_( Type, SliceType, FuncSuffix, VarSliceType, VarFuncSuffix )

Generates two slices for sequences with Type values. The struct SliceType for const sequences and VarSliceType for variable sequences. The values FuncSuffix and VarFuncSuffix are used as postfix for the generated inline functions.

The following call can be used in a .h or .c file.

SLICE_DEF_C_(
   int8_t,           // Type
   cInt8Slice,       // SliceType
   int8_slice_c,     // FuncSuffix
   cVarInt8Slice,    // VarSliceType
   var_int8_slice_c  // VarFuncSuffix
)

The overview shows the generated structs and functions for the call.

Const Data Slice Variable Data Slice

struct

cInt8Slice

cVarInt8Slice

SLICE_C_

int8_slice_c

var_int8_slice_c

MAKE_SLICE_C_

make_int8_slice_c

make_var_int8_slice_c

EMPTY_SLICE_C

empty_int8_slice_c

empty_var_int8_slice_c

SUB_SLICE_C_

sub_int8_slice_c

sub_var_int8_slice_c

LEFT_SLICE_C_

left_int8_slice_c

left_var_int8_slice_c

MID_SLICE_C_

mid_int8_slice_c

mid_var_int8_slice_c

RIGHT_SLICE_C_

right_int8_slice_c

right_var_int8_slice_c

AS_SLICE_C_

-

as_int8_slice_c

CAST_SLICE_C

-

cast_int8_slice_c

SET_SLICE_C_

-

set_int8_slice_c

SLICE_IMPL_C_

#define SLICE_IMPL_C_( Type, SliceType, FuncSuffix, VarSliceType, VarFuncSuffix )

Generates the correspondent extern inline functions for the SLICE_DEF_C_ macro.

type

TEMP_SLICE_DEF_C_

#define TEMP_SLICE_DEF_C_( Entry, EntryStruct )

Generates a struct with the name Entry and a slice with the composed name EntrySlice. The Entry struct will have the in EntryStruct defined structure.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* input;
      int exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "1234", 1234 ),
      t_( "-78", -78 )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      int res = atoi( t.input );
      tap_descf_c( res == t.exp, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

TEMP_VAR_SLICE_DEF_C_

#define TEMP_VAR_SLICE_DEF_C_( Entry, EntryStruct )

Generates a struct with the name Entry and a var slice with the composed name EntrySlice. The Entry struct will have the in EntryStruct defined structure.

Functions

init

heap_slice_c_

#define heap_slice_c_( Size, Type )

A macro function for the initialization of a slice with a sequence that will be allocated on the heap. Size defines the length of the sequence. Type defines the data type of the values in the sequence.

scalars_c_

#define scalars_c_( Size, Type )

A macro function for the initializaiton of a slice with a sequence of scalar values. Size defines the length of the sequence. Type defines the data type of the values in the sequence.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/int64.h" // for cInt64Slice

int main( void )
{
   init_tap_c_();

   cVarInt64Slice slice = scalars_c_( 3, int64_t );
   slice.v[ 0 ] = -10;
   slice.v[ 1 ] = 100;
   slice.v[ 2 ] = 200;

   expect_c_( slice.s == 3 );
   expect_c_( slice.v[ 0 ] == -10 );
   expect_c_( slice.v[ 1 ] == 100 );
   expect_c_( slice.v[ 2 ] == 200 );

   // wrong code we acces the memory outside the array
   // slice.v[ 3 ] = 999;

   return finish_tap_c_();
}

slice_c_

#define slice_c_( Type, ... )

A macro function for the initialization of a slice with a sequence. Type defines the data type of the values in the sequence. The additional arguments(…) represent the values that the sequence should have.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

//************************************** example struct
struct Point
{
   int32_t x;
   int32_t y;
};

typedef struct Point Point;

SLICE_DEF_C_(
   Point,
   PointSlice,
   point_slice_c,
   VarPointSlice,
   var_point_slice_c
)
//**************************************

static cChars abc = slice_c_( char, 'a', 'b', 'c' );

int main( void )
{
   init_tap_c_();

   // global static slice
   expect_c_( abc.s == 3 );
   expect_c_( abc.v[ 0 ] == 'a' );
   expect_c_( abc.v[ 1 ] == 'b' );
   expect_c_( abc.v[ 2 ] == 'c' );

   // local slice
   cVarChars def = slice_c_( char, 'd', 'e', 'f' );
   expect_c_( def.s == 3 );
   expect_c_( def.v[ 0 ] == 'd' );
   expect_c_( def.v[ 1 ] == 'e' );
   expect_c_( def.v[ 2 ] == 'f' );

   def.v[ 1 ] = 'x';
   expect_c_( def.v[ 1 ] == 'x' );

   // with struct values
   VarPointSlice points = slice_c_( Point, { .x=1, .y=2 }, { 3, 4 } );
   expect_c_( points.v[ 0 ].x == 1 );
   expect_c_( points.v[ 0 ].y == 2 );
   expect_c_( points.v[ 1 ].x == 3 );
   expect_c_( points.v[ 1 ].y == 4 );

   return finish_tap_c_();
}

structs_c_

#define structs_c_( Size, Type )

A macro function for the initializaiton of a slice with a sequence of structs. Size defines the length of the sequence. Type defines the data type of the values in the sequence.

Example
#include "clingo/lang/slice.h"
#include "clingo/lang/expect.h"

//************************************** example struct
struct Point
{
   int64_t x;
   int64_t y;
};

typedef struct Point Point;

SLICE_DEF_C_(
   Point,
   PointSlice,
   point_slice_c,
   VarPointSlice,
   var_point_slice_c
)
//**************************************

int main( void )
{
   init_tap_c_();

   VarPointSlice slice = structs_c_( 3, Point );
   slice.v[ 0 ] = (Point){ .x=1, .y=200 };
   slice.v[ 1 ] = (Point){ 3000, 44 };
   slice.v[ 2 ].x = -55;
   slice.v[ 2 ].y = 666;

   expect_c_( slice.s == 3 );
   expect_c_( slice.v[ 0 ].x ==    1 );
   expect_c_( slice.v[ 0 ].y ==  200 );
   expect_c_( slice.v[ 1 ].x == 3000 );
   expect_c_( slice.v[ 1 ].y ==   44 );
   expect_c_( slice.v[ 2 ].x ==  -55 );
   expect_c_( slice.v[ 2 ].y ==  666 );

   // wrong code we acces the memory outside the array
   // slice.v[ 3 ] = (Point){ .x=999 };

   return finish_tap_c_();
}

check

is_empty_c_

#define is_empty_c_( Slice )

Returns true if the Slice is empty, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   expect_c_( is_empty_c_( empty_chars_c() ) );
   expect_c_( is_empty_c_( c_c( "" ) ) );

   cChars abc = c_c( "abc" );
   expect_c_( not is_empty_c_( abc ) );
   abc.s = 0;
   expect_c_( is_empty_c_( abc ) );

   return finish_tap_c_();
}

is_sub_c_

#define is_sub_c_( Main, Sub )

Returns true if Sub is a sub-slice from Main, otherwise false.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   // ------------------------------------------------------------------- cChars
   cChars chars = c_c( "Earth, Wind and Fire" );

   cChars earth = left_chars_c( chars, 5 );
   cChars wind = sub_chars_c( chars, 7, 11 );
   cChars fire = right_chars_c( chars, 4 );
   cChars windAndFire = mid_chars_c( chars, 7 );

   expect_c_( is_sub_c_( chars, earth ) );
   expect_c_( is_sub_c_( chars, wind ) );
   expect_c_( is_sub_c_( chars, fire ) );
   expect_c_( is_sub_c_( chars, windAndFire ) );

   return finish_tap_c_();
}

valid_index_c_

#define valid_index_c_( Slice, Index )

Checks if a Index is inside the size boundaries of the Slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars empty = empty_chars_c();
   expect_c_( !valid_index_c_( empty, 0 ) );
   expect_c_( !valid_index_c_( empty, 1 ) );

   cChars abc = c_c( "abc" );
   expect_c_( !valid_index_c_( abc, -1 ) );
   expect_c_(  valid_index_c_( abc,  0 ) );
   expect_c_(  valid_index_c_( abc,  1 ) );
   expect_c_(  valid_index_c_( abc,  2 ) );
   expect_c_( !valid_index_c_( abc,  3 ) );

   return finish_tap_c_();
}

access

first_c_

#define first_c_( Slice )

Returns the first value in the Slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );
   expect_c_( first_c_( abc ) == 'a' );

   cChars foo = c_c( "xyz" );
   expect_c_( first_c_( foo ) == 'x' );

   return finish_tap_c_();
}

get_c_

#define get_c_( Slice, Index, Def )

Returns the value at the Index position in the Slice. If the Index is invalid will the Def value returned.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );
   expect_c_( get_c_( abc, -1, 'x' ) == 'x' );
   expect_c_( get_c_( abc,  0, 'x' ) == 'a' );
   expect_c_( get_c_( abc,  1, 'x' ) == 'b' );
   expect_c_( get_c_( abc,  2, 'x' ) == 'c' );
   expect_c_( get_c_( abc,  3, 'x' ) == 'x' );

   return finish_tap_c_();
}

last_c_

#define last_c_( Slice )

Returns the last value in the Slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );
   expect_c_( last_c_( abc ) == 'c' );

   cChars bar = c_c( "bar" );
   expect_c_( last_c_( bar ) == 'r' );

   return finish_tap_c_();
}

put_c_

#define put_c_( Slice, Index, Value )

Updates the Value at the Index position in the Slice. If the Index is invalid will no value be replaced.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/int64.h" // for cInt64Slice

int main( void )
{
   init_tap_c_();

   cVarInt64Slice slice4 = slice_c_( int64_t, 11, 22, 33, 44 );
   cVarInt64Slice slice3 = left_var_int64_slice_c( slice4, 3 );
   put_c_( slice3, 0, 10 );
   put_c_( slice3, 1, 20 );
   put_c_( slice3, 2, 30 );
   put_c_( slice3, 3, 40 ); // out of slice range

   expect_c_( slice4.v[ 0 ] == 10 );
   expect_c_( slice4.v[ 1 ] == 20 );
   expect_c_( slice4.v[ 2 ] == 30 );
   expect_c_( slice4.v[ 3 ] == 44 ); // not set

   return finish_tap_c_();
}

pointer access

begin_c_

#define begin_c_( Slice )

Returns a pointer to the firts element of the sequence.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );
   expect_c_( *begin_c_( abc ) == 'a' );

   char const* iter = begin_c_( abc );
   expect_c_( *iter == 'a' );

   ++iter;
   expect_c_( *iter == 'b' );

   ++iter;
   expect_c_( *iter == 'c' );

   ++iter;
   expect_c_( iter == end_c_( abc ) );

   return finish_tap_c_();
}

end_c_

#define end_c_( Slice )

Returns a pointer to the element past the last element of the sequence.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abcx" );
   abc.s--;
   expect_c_( *end_c_( abc ) == 'x' );

   char const* iter = end_c_( abc );
   expect_c_( *iter == 'x' );

   --iter;
   expect_c_( *iter == 'c' );

   --iter;
   expect_c_( *iter == 'b' );

   --iter;
   expect_c_( iter == begin_c_( abc ) );

   return finish_tap_c_();
}

for_each_c_

#define for_each_c_( Type, Itr, Slice )

Macro function to define a for statement to iterate over all elements in Slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cVarChars buf = scalars_c_( 8, char );

   bool flag = true;
   for_each_c_( char*, c, buf )
   {
      if ( flag )
      {
         *c = '+';
         flag = false;
      }
      else
      {
         *c = '.';
         flag = true;
      }
   }

   cChars slc = as_chars_c( buf );
   expect_c_( chars_is_c( slc, "+.+.+.+." ) );

   return finish_tap_c_();
}

nth_c_

#define nth_c_( Slice, N )

Returns a pointer to value at the position N in the Slice. Th N value can be negative to access the Slice backwards. If the N value is out of range will NULL be returned.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   expect_c_( *nth_c_( abcdef, 0 ) == 'a' );
   expect_c_( *nth_c_( abcdef, 5 ) == 'f' );

   expect_c_( *nth_c_( abcdef, -1 ) == 'f' );
   expect_c_( *nth_c_( abcdef, -6 ) == 'a' );

   expect_c_( nth_c_( abcdef, 6 ) == NULL );
   expect_c_( nth_c_( abcdef, -7 ) == NULL );

   return finish_tap_c_();
}

index_of_c_

#define index_of_c_( Slice, Ptr )

Returns the index of the pointer Ptr. If the pointer does not point to a value of the sequence returns the macro function -1.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );

   char const* ptr = NULL;
   expect_c_( index_of_c_( abc, ptr ) == -1 );

   ptr = begin_c_( abc );
   expect_c_( index_of_c_( abc, ptr ) == 0 );

   ptr++;
   expect_c_( index_of_c_( abc, ptr ) == 1 );

   ptr = rbegin_c_( abc );
   expect_c_( index_of_c_( abc, ptr ) == 2 );

   ptr = end_c_( abc );
   expect_c_( index_of_c_( abc, ptr ) == -1 );

   ptr = rend_c_( abc );
   expect_c_( index_of_c_( abc, ptr ) == -1 );

   return finish_tap_c_();
}

points_into_c_

#define points_into_c_( Slice, Ptr )

Checks if the pointer Ptr points to a value in the sequence of the Slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );

   expect_c_(  points_into_c_( abc, begin_c_( abc ) ) );
   expect_c_(  points_into_c_( abc, rbegin_c_( abc ) ) );
   expect_c_( !points_into_c_( abc, end_c_( abc ) ) );
   expect_c_( !points_into_c_( abc, rend_c_( abc ) ) );

   char const* ptr = begin_c_( abc );
   ptr++;
   expect_c_( points_into_c_( abc, ptr ) );

   ptr = NULL;
   expect_c_( !points_into_c_( abc, ptr ) );

   return finish_tap_c_();
}

ptr_for_c_

#define ptr_for_c_( Slice, Index )

Returns a pointer to value at the Index position in the Slice. If the Index is invalid will a null pointer returned.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

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

   // ---------------------------------------------------- returns valid pointer
   ptr = ptr_for_c_( abcdef, 0 );
   expect_c_( *ptr == 'a' );

   ptr = ptr_for_c_( abcdef, 2 );
   expect_c_( *ptr == 'c' );

   ptr = ptr_for_c_( abcdef, 5 );
   expect_c_( *ptr == 'f' );

   // ---------------------------------------------------- try to access outside
   ptr = ptr_for_c_( abcdef, -1 );
   expect_c_( ptr == NULL );

   ptr = ptr_for_c_( abcdef, 6 );
   expect_c_( ptr == NULL );

   return finish_tap_c_();
}

rbegin_c_

#define rbegin_c_( Slice )

Returns a pointer to the last element of the sequence.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "abc" );
   expect_c_( *rbegin_c_( abc ) == 'c' );

   char const* itr = rbegin_c_( abc );
   expect_c_( *itr == 'c' );

   itr--;
   expect_c_( *itr == 'b' );

   itr--;
   expect_c_( *itr == 'a' );

   itr--;
   expect_c_( itr == rend_c_( abc ) );

   return finish_tap_c_();
}

rend_c_

#define rend_c_( Slice )

Returns a pointer to the element before the first element of the sequence.

Example
#include "clingo/lang/expect.h"
#include "clingo/lang/slice.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars abc = c_c( "xabc" );
   abc.v++;
   abc.s--;
   expect_c_( *rend_c_( abc ) == 'x' );

   char const* itr = rend_c_( abc );
   expect_c_( *itr == 'x' );

   itr++;
   expect_c_( *itr == 'a' );

   itr++;
   expect_c_( *itr == 'b' );

   itr++;
   expect_c_( itr == rbegin_c_( abc ) );

   return finish_tap_c_();
}