window

Overview

A window is a abstract struct that allows to itarate over multiple values of a slice. The subslices(windows) are overlapping.

A window struct can also be viewed as slice because it has the attributes s (sequence length) and v (points to the first value).

window.svg

In addition has a window an attribute slice for the actual slice.

This module defines code generation macros to generate typed window structs. Also has this module macro functions to move a window struct along the slice sequence.

Code Generation Macros

init

INIT_FRONT_WINDOW_C_

#define INIT_FRONT_WINDOW_C_( FuncName, WindowType, SliceType )

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

void FuncName( WindowType window[static 1], int64_t s, SliceType slice );

INIT_BACK_WINDOW_C_

#define INIT_BACK_WINDOW_C_( FuncName, WindowType, SliceType )

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

void FuncName( WindowType window[static 1], int64_t s, SliceType slice );

type

WINDOW_DEF_C_

#define WINDOW_DEF_C_( Type,
                       WindowType, SliceType, FuncSuffix,
                       VarWindowType, VarSliceType, VarFuncSuffix )

Generates two window structs for slices with Type values. The struct WindowType for the const sequence of SliceType and VarWindowType 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.

WINDOW_DEF_C_(
   char,             // Type
   cCharWindow,      // WindowType
   cChars,           // SliceType
   char_window_c,    // FuncSuffix
   cVarCharWindow,   // VarWindowType
   cVarChars,        // VarSliceType
   var_char_window_c // VarFuncSuffix
)

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

   Const Data Window Variable Data Window

struct

cCharWindow

cVarCharWindow

INIT_FRONT_WINDOW_C_

init_front_char_window_c

init_front_var_char_window_c

INIT_BACK_WINDOW_C_

init_back_char_window_c

init_back_var_char_window_c

WINDOW_IMPL_C_

#define WINDOW_IMPL_C_( WindowType, SliceType, FuncSuffix
                        VarWindowType, VarSliceType, VarFuncSuffix )

Generates the correspondent extern inline functions for the WINDOW_DEF_C_ macro.

STATIC_FRONT_WINDOW_C_

#define STATIC_FRONT_WINDOW_C_( Type, WindowType, SliceType, FuncSuffix )

Generates a window struct and the corresponding static INIT_FRONT_WINDOW_C_ function.

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

//--------------------- Type,       WindowType, SliceType,  FuncSuffix
STATIC_FRONT_WINDOW_C_( char const, charWindow, cChars,     char_window )

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   charWindow window;
   init_front_char_window( &window, 3, abcdef ); // ------------------------ abc
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'a' );
   expect_c_( last_c_( window ) == 'c' );

   next_window_c_( window ); // -------------------------------------------- bcd
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'b' );
   expect_c_( last_c_( window ) == 'd' );

   next_window_c_( window ); // -------------------------------------------- cde
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'c' );
   expect_c_( last_c_( window ) == 'e' );

   next_window_c_( window ); // -------------------------------------------- def
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'd' );
   expect_c_( last_c_( window ) == 'f' );

   next_window_c_( window );
   expect_c_( not valid_window_c_( window ) );

   return finish_tap_c_();
}

STATIC_BACK_WINDOW_C_

#define STATIC_BACK_WINDOW_C_( Type, WindowType, SliceType, FuncSuffix )

Generates a window struct and the corresponding static INIT_BACK_WINDOW_C_ function.

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

//-------------------- Type,       WindowType, SliceType,   FuncSuffix
STATIC_BACK_WINDOW_C_( char const, charWindow, cChars,      char_window )

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   charWindow window;
   init_back_char_window( &window, 3, abcdef ); // ------------------------- def
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'd' );
   expect_c_( last_c_( window ) == 'f' );

   prev_window_c_( window ); // -------------------------------------------- cde
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'c' );
   expect_c_( last_c_( window ) == 'e' );

   prev_window_c_( window ); // -------------------------------------------- bcd
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'b' );
   expect_c_( last_c_( window ) == 'd' );

   prev_window_c_( window ); // -------------------------------------------- abc
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'a' );
   expect_c_( last_c_( window ) == 'c' );

   prev_window_c_( window );
   expect_c_( not valid_window_c_( window ) );

   return finish_tap_c_();
}

Functions

next_window_c_

#define next_window_c_( Window )

Moves the window forward.

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

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   cCharWindow window;
   init_front_char_window_c( &window, 3, abcdef ); // ---------------------- abc
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'a' );
   expect_c_( last_c_( window ) == 'c' );

   next_window_c_( window ); // -------------------------------------------- bcd
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'b' );
   expect_c_( last_c_( window ) == 'd' );

   next_window_c_( window ); // -------------------------------------------- cde
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'c' );
   expect_c_( last_c_( window ) == 'e' );

   next_window_c_( window ); // -------------------------------------------- def
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'd' );
   expect_c_( last_c_( window ) == 'f' );

   next_window_c_( window );
   expect_c_( not valid_window_c_( window ) );

   return finish_tap_c_();
}

prev_window_c_

#define prev_window_c_( Window )

Moves the window backward.

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

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   cCharWindow window;
   init_back_char_window_c( &window, 3, abcdef ); // ----------------------- def
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'd' );
   expect_c_( last_c_( window ) == 'f' );

   prev_window_c_( window ); // -------------------------------------------- cde
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'c' );
   expect_c_( last_c_( window ) == 'e' );

   prev_window_c_( window ); // -------------------------------------------- bcd
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'b' );
   expect_c_( last_c_( window ) == 'd' );

   prev_window_c_( window ); // -------------------------------------------- abc
   expect_c_( valid_window_c_( window ) );
   expect_c_( first_c_( window ) == 'a' );
   expect_c_( last_c_( window ) == 'c' );

   prev_window_c_( window );
   expect_c_( not valid_window_c_( window ) );

   return finish_tap_c_();
}

valid_window_c_

#define valid_window_c_( Window )

Returns true if the window is a valid subslice of the slice, otherwise false.

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

int main( void )
{
   init_tap_c_();

   cChars slice = c_c( "abc" );
   cCharWindow window;

   // -------------------------------------------------------------- valid cases
   init_front_char_window_c( &window, 1, slice );
   expect_c_( valid_window_c_( window ) );

   init_front_char_window_c( &window, 2, slice );
   expect_c_( valid_window_c_( window ) );

   init_front_char_window_c( &window, 3, slice );
   expect_c_( valid_window_c_( window ) );

   // ------------------------------------------------------------ invalid cases
   init_front_char_window_c( &window, 0, slice );
   expect_c_( not valid_window_c_( window ) );

   init_front_char_window_c( &window, 4, slice );
   expect_c_( not valid_window_c_( window ) );

   // ---------------------------------------------------------- iteration -next
   cRecorder* rec = &recorder_c_( 32 );
   init_front_char_window_c( &window, 2, c_c( "abcdef" ) );
   while ( valid_window_c_( window ) )
   {
      record_chars_c( rec, chars_c( window.s, window.v ) );
      next_window_c_( window );
   }
   expect_c_( recorded_is_c( rec, "abbccddeef" ) );

   // ---------------------------------------------------------- iteration -prev
   reset_recorder_c( rec );
   init_back_char_window_c( &window, 2, c_c( "abcdef" ) );
   while ( valid_window_c_( window ) )
   {
      record_chars_c( rec, chars_c( window.s, window.v ) );
      prev_window_c_( window );
   }
   expect_c_( recorded_is_c( rec, "efdecdbcab" ) );

   return finish_tap_c_();
}