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).

chunk.svg

In addition has a chunk the attributes favSize (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 move a chunk struct along the slice sequence.

Code Generation Macros

init

INIT_FRONT_CHUNK_C_

#define INIT_FRONT_CHUNK_C_( FuncName, ChunkType, SliceType )

Generates a initialization function that sets the chunk on the front of a slice. The parameter s defines the size of the chunk subslices. The generated function has the following common signature:

void FuncName( ChunkType chunk[static 1], int64_t s, SliceType slice );

INIT_BACK_CHUNK_C_

#define INIT_FRONT_CHUNK_C_( FuncName, ChunkType, SliceType )

Generates a initialization function that sets the chunk on the back of a slice. The parameter s defines the size of the chunk subslices. The generated function has the following common signature.

void FuncName( ChunkType chunk[static 1], int64_t s, SliceType slice );

type

CHUNK_DEF_C_

#define CHUNK_DEF_C_( Type,
                      ChunkType, SliceType, FuncSuffix,
                      VarChunkType, VarSliceType, VarFuncSuffix )

Generates two chunk structs for slices with Type values. The struct ChunkType for the const sequence of SliceType and VarChunkType for the variable sequence of VarSliceType. 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.

CHUNK_DEF_C_(
   char,             // Type
   cCharChunk,       // ChunkType
   cChars,           // SliceType
   char_chunk_c,     // FuncSuffix
   cVarCharChunk,    // VarChunkType
   cVarChars,        // VarSliceType
   var_char_chunk_c  // VarFuncSuffix
)

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

Const Data Chunk Variable Data Chunk

struct

cCharChunk

cVarCharChunk

INIT_FRONT_CHUNK_C_

init_front_char_chunk_c

init_front_var_char_chunk_c

INIT_BACK_CHUNK_C_

init_back_char_chunk_c

init_back_var_char_chunk_c

CHUNK_IMPL_C_

#define CHUNK_IMPL_C_( ChunkType, SliceType, FuncSuffix,
                       VarChunkType, VarSliceType, VarFuncSuffix )

Generates the correspondent extern inline functions for the WINDOW_DEF_C_ macro.

STATIC_FRONT_CHUNK_C_

#define STATIC_FRONT_CHUNK_C_( Type, ChunkType, SliceType, FuncSuffix )

Generates a chunk struct and the corresponding static INIT_FRONT_CHUNK_C_ function.

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

//-------------------- Type,       ChunkType, SliceType,    FuncSuffix
STATIC_FRONT_CHUNK_C_( char const, charChunk, cChars,       char_chunk )

int main( void )
{
   init_tap_c_();

   cChars abcdefg = c_c( "abcdefg" );

   charChunk chunk;
   init_front_char_chunk( &chunk, 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_();
}

STATIC_BACK_CHUNK_C_

#define STATIC_BACK_CHUNK_C_( Type, ChunkType, SliceType, FuncSuffix )

Generates a chunk struct and the corresponding static INIT_BACK_CHUNK_C_ function.

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

//------------------- Type,       ChunkType, SliceType,  FuncSuffix
STATIC_BACK_CHUNK_C_( char const, charChunk, cChars,     char_chunk )

int main( void )
{
   init_tap_c_();

   cChars abcdefg = c_c( "abcdefg" );

   charChunk chunk;
   init_back_char_chunk( &chunk, 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_();
}

Functions

exact_chunk_c_

#define exact_chunk_c_( Chunk )

Returns true if the chunk is valid and s equates to favSize.

Example
#include "clingo/io/cRecorder.h" // for cRecorder
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars slice = c_c( "abc" );
   cCharChunk chunk;

   // -------------------------------------------------------------- exact cases
   init_front_char_chunk_c( &chunk, 1, slice );
   expect_c_( exact_chunk_c_( chunk ) );

   init_front_char_chunk_c( &chunk, 3, slice );
   expect_c_( exact_chunk_c_( chunk ) );

   // ---------------------------------------------------------- inexactly cases
   init_front_char_chunk_c( &chunk, 0, slice );
   expect_c_( not exact_chunk_c_( chunk ) );

   init_front_char_chunk_c( &chunk, 4, slice );
   expect_c_( not exact_chunk_c_( chunk ) );

   // --------------------------------------------------------- iteration - next
   cRecorder* rec = &recorder_c_( 8 );
   init_front_char_chunk_c( &chunk, 2, c_c( "abcdefg" ) );
   while ( exact_chunk_c_( chunk ) )
   {
      record_chars_c( rec, chars_c( chunk.s, chunk.v ) );
      next_chunk_c_( chunk );
   }
   expect_c_( recorded_is_c( rec, "abcdef" ) );

   // --------------------------------------------------------- iteration - prev
   reset_recorder_c( rec );
   init_back_char_chunk_c( &chunk, 2, c_c( "abcdefg" ) );
   while ( exact_chunk_c_( chunk ) )
   {
      record_chars_c( rec, chars_c( chunk.s, chunk.v ) );
      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.

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

int main( void )
{
   init_tap_c_();

   cChars abcdefg = c_c( "abcdefg" );

   cCharChunk chunk;
   init_front_char_chunk_c( &chunk, 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.

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

int main( void )
{
   init_tap_c_();

   cChars abcdefg = c_c( "abcdefg" );

   cCharChunk chunk;
   init_back_char_chunk_c( &chunk, 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.

Example
#include "clingo/io/cRecorder.h" // for cRecorder
#include "clingo/lang/chunk.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cChars.h"

int main( void )
{
   init_tap_c_();

   cChars slice = c_c( "abc" );
   cCharChunk chunk;

   // -------------------------------------------------------------- valid cases
   init_front_char_chunk_c( &chunk, 1, slice );
   expect_c_( valid_chunk_c_( chunk ) );

   init_front_char_chunk_c( &chunk, 3, slice );
   expect_c_( valid_chunk_c_( chunk ) );

   init_front_char_chunk_c( &chunk, 4, slice );
   expect_c_( valid_chunk_c_( chunk ) );

   // ------------------------------------------------------------ invalid cases
   init_front_char_chunk_c( &chunk, 0, slice );
   expect_c_( not valid_chunk_c_( chunk ) );

   // --------------------------------------------------------- iteration - next
   cRecorder* rec = &recorder_c_( 8 );
   init_front_char_chunk_c( &chunk, 2, c_c( "abcdefg" ) );
   while ( valid_chunk_c_( chunk ) )
   {
      record_chars_c( rec, chars_c( chunk.s, chunk.v ) );
      next_chunk_c_( chunk );
   }
   expect_c_( recorded_is_c( rec, "abcdefg" ) );

   // --------------------------------------------------------- iteration - prev
   reset_recorder_c( rec );
   init_back_char_chunk_c( &chunk, 2, c_c( "abcdefg" ) );
   while ( valid_chunk_c_( chunk ) )
   {
      record_chars_c( rec, chars_c( chunk.s, chunk.v ) );
      prev_chunk_c_( chunk );
   }
   expect_c_( recorded_is_c( rec, "fgdebca" ) );

   return finish_tap_c_();
}