chunk
Overview
A chunk is a abstract struct that allows to iterate over multiple values of a slice. The subslices(chunks) are not overlapping.
A chunk struct can also be viewed as slice because it has the attributes s (sequence length) and v (points to the first value).
In addition has a chunk the attributes fav (favored size) and slice (the actual slice).
This module defines code generation macros to generate typed chunk structs. Also has this module macro functions to init and use a chunk struct to move along the slice sequence.
Code Generation Macros
type
CHUNK_C_
#define CHUNK_C_( Type, SliceType, ChunkType )
Generates a chunk struct for a slice with Type values. The overview shows the generated structs and functions for the call.
Functions
init
back_chunk_c_
#define back_chunk_c_( Size, Slice )
Initializes a chunk on the back of the Slice. The Size parameter defines the size of the chunk subslices.
front_chunk_c_
#define front_chunk_c_( Size, Slice )
Initializes a chunk on the front of the Slice. The Size paramter defines the size of the chunk subslices.
use
exact_chunk_c_
#define exact_chunk_c_( Chunk )
Returns true if the chunk is valid and s equates to fav.
#include "clingo/io/cRecorder.h" // for cRecorder
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"
CHUNK_C_( char const, cChars, cCharChunk )
int main( void )
{
init_tap_c_();
cChars slice = c_c( "abc" );
cCharChunk chunk;
// -------------------------------------------------------------- exact cases
chunk = (cCharChunk)front_chunk_c_( 1, slice );
expect_c_( exact_chunk_c_( chunk ) );
chunk = (cCharChunk)front_chunk_c_( 3, slice );
expect_c_( exact_chunk_c_( chunk ) );
// ---------------------------------------------------------- inexactly cases
chunk = (cCharChunk)front_chunk_c_( 0, slice );
expect_c_( not exact_chunk_c_( chunk ) );
chunk = (cCharChunk)front_chunk_c_( 4, slice );
expect_c_( not exact_chunk_c_( chunk ) );
// --------------------------------------------------------- iteration - next
cRecorder* rec = &recorder_c_( 8 );
chunk = (cCharChunk)front_chunk_c_( 2, c_c( "abcdefg" ) );
while ( exact_chunk_c_( chunk ) )
{
record_chars_c( rec, as_c_( cChars, chunk ) );
next_chunk_c_( chunk );
}
expect_c_( recorded_is_c( rec, "abcdef" ) );
// --------------------------------------------------------- iteration - prev
reset_recorder_c( rec );
chunk = (cCharChunk)back_chunk_c_( 2, c_c( "abcdefg" ) );
while ( exact_chunk_c_( chunk ) )
{
record_chars_c( rec, as_c_( cChars, chunk ) );
prev_chunk_c_( chunk );
}
expect_c_( recorded_is_c( rec, "fgdebc" ) );
return finish_tap_c_();
}
next_chunk_c_
#define next_chunk_c_( Chunk )
Moves the chunk forward.
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"
CHUNK_C_( char const, cChars, cCharChunk )
int main( void )
{
init_tap_c_();
cChars abcdefg = c_c( "abcdefg" );
cCharChunk chunk = front_chunk_c_( 2, abcdefg ); // ---------------------- ab
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'a' );
expect_c_( last_c_( chunk ) == 'b' );
next_chunk_c_( chunk ); // ----------------------------------------------- cd
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'c' );
expect_c_( last_c_( chunk ) == 'd' );
next_chunk_c_( chunk ); // ----------------------------------------------- ef
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'e' );
expect_c_( last_c_( chunk ) == 'f' );
next_chunk_c_( chunk ); // ------------------------------------------------ g
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'g' );
expect_c_( last_c_( chunk ) == 'g' );
next_chunk_c_( chunk );
expect_c_( not valid_chunk_c_( chunk ) );
return finish_tap_c_();
}
prev_chunk_c_
#define prev_chunk_c_( Chunk )
Moves the chunk backward.
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"
CHUNK_C_( char const, cChars, cCharChunk )
int main( void )
{
init_tap_c_();
cChars abcdefg = c_c( "abcdefg" );
cCharChunk chunk = back_chunk_c_( 2, abcdefg ); // ----------------------- fg
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'f' );
expect_c_( last_c_( chunk ) == 'g' );
prev_chunk_c_( chunk ); // ----------------------------------------------- de
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'd' );
expect_c_( last_c_( chunk ) == 'e' );
prev_chunk_c_( chunk ); // ----------------------------------------------- bc
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'b' );
expect_c_( last_c_( chunk ) == 'c' );
prev_chunk_c_( chunk ); // ------------------------------------------------ a
expect_c_( valid_chunk_c_( chunk ) );
expect_c_( first_c_( chunk ) == 'a' );
expect_c_( last_c_( chunk ) == 'a' );
prev_chunk_c_( chunk );
expect_c_( not valid_chunk_c_( chunk ) );
return finish_tap_c_();
}
valid_chunk_c_
#define valid_chunk_c_( Chunk )
Returns true if the chunk is a valid subslice of the slice, otherwise false.
#include "clingo/io/cRecorder.h" // for cRecorder
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"
CHUNK_C_( char const, cChars, cCharChunk )
int main( void )
{
init_tap_c_();
cChars slice = c_c( "abc" );
cCharChunk chunk;
// -------------------------------------------------------------- valid cases
chunk = (cCharChunk)front_chunk_c_( 1, slice );
expect_c_( valid_chunk_c_( chunk ) );
chunk = (cCharChunk)front_chunk_c_( 3, slice );
expect_c_( valid_chunk_c_( chunk ) );
chunk = (cCharChunk)front_chunk_c_( 4, slice );
expect_c_( valid_chunk_c_( chunk ) );
// ------------------------------------------------------------ invalid cases
chunk = (cCharChunk)front_chunk_c_( 0, slice );
expect_c_( not valid_chunk_c_( chunk ) );
// --------------------------------------------------------- iteration - next
cRecorder* rec = &recorder_c_( 8 );
chunk = (cCharChunk)front_chunk_c_( 2, c_c( "abcdefg" ) );
while ( valid_chunk_c_( chunk ) )
{
record_chars_c( rec, (cChars){ chunk.s, chunk.v } );
next_chunk_c_( chunk );
}
expect_c_( recorded_is_c( rec, "abcdefg" ) );
// --------------------------------------------------------- iteration - prev
reset_recorder_c( rec );
chunk = (cCharChunk)back_chunk_c_( 2, c_c( "abcdefg" ) );
while ( valid_chunk_c_( chunk ) )
{
record_chars_c( rec, (cChars){ chunk.s, chunk.v } );
prev_chunk_c_( chunk );
}
expect_c_( recorded_is_c( rec, "fgdebca" ) );
return finish_tap_c_();
}