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

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 init and use a window struct to move along the slice sequence.

Code Generation Macros

type

WINDOW_C_

#define WINDOW_C_( Type, SliceType, WindowType )

Generates a window struct for a slice with Type values.

Functions

init

back_window_c_

#define back_window_c_( Size, Slice )

Initializes a window on the back of the Slice. The Size parameter defines the size of the window subslices.

front_window_c_

#define front_window_c_( Size, Slice )

Initializes a window on the front of the Slice. The Size parameter defines the size of the window subslices.

use

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"

WINDOW_C_( char const, cChars, cCharWindow )

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   cCharWindow window = front_window_c_( 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"

WINDOW_C_( char const, cChars, cCharWindow )

int main( void )
{
   init_tap_c_();

   cChars abcdef = c_c( "abcdef" );

   cCharWindow window = back_window_c_( 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"

WINDOW_C_( char const, cChars, cCharWindow )

int main( void )
{
   init_tap_c_();

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

   // -------------------------------------------------------------- valid cases
   window = (cCharWindow)front_window_c_( 1, slice );
   expect_c_( valid_window_c_( window ) );

   window = (cCharWindow)front_window_c_( 2, slice );
   expect_c_( valid_window_c_( window ) );

   window = (cCharWindow)front_window_c_( 3, slice );
   expect_c_( valid_window_c_( window ) );

   // ------------------------------------------------------------ invalid cases
   window = (cCharWindow)front_window_c_( 0, slice );
   expect_c_( not valid_window_c_( window ) );

   window = (cCharWindow)front_window_c_( 4, slice );
   expect_c_( not valid_window_c_( window ) );

   // ---------------------------------------------------------- iteration -next
   cRecorder* rec = &recorder_c_( 32 );
   window = (cCharWindow)front_window_c_( 2, c_c( "abcdef" ) );
   while ( valid_window_c_( window ) )
   {
      record_chars_c( rec, as_c_( cChars, window ) );
      next_window_c_( window );
   }
   expect_c_( recorded_is_c( rec, "abbccddeef" ) );

   // ---------------------------------------------------------- iteration -prev
   reset_recorder_c( rec );
   window = (cCharWindow)back_window_c_( 2, c_c( "abcdef" ) );
   while ( valid_window_c_( window ) )
   {
      record_chars_c( rec, as_c_( cChars, window ) );
      prev_window_c_( window );
   }
   expect_c_( recorded_is_c( rec, "efdecdbcab" ) );

   return finish_tap_c_();
}