cRange

Overview

Module with functions and types to work with cRange values.

Types and Definitions

cRange

struct cRange
{
   int64_t min;
   int64_t max;
};
typedef struct cRange cRange;

A cRange is bounded inclusively below and above [min..max].

Generated

cRangeSlice

struct cRangeSlice
{
   int64_t s;
   cRange const* v;
};
typedef struct cRangeSlice cRangeSlice;

Via the macro SLICES_C_ generated struct.

cVarRangeSlice

struct cVarRangeSlice
{
   int64_t s;
   cRange* v;
};
typedef struct cVarRangeSlice cVarRangeSlice;

Via the macro SLICES_C_ generated struct.

Functions

init

closed_open_range_c_

#define closed_open_range_c_( Start, End )

Creates a half opened range [Start..End).

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

int main( void )
{
   init_tap_c_();

   cRange range = closed_open_range_c_( -3, 3 );

   expect_c_( range.min == -3 );
   expect_c_( range.max == 2 );

   return finish_tap_c_();
}

closed_range_c_

#define closed_range_c_( Start, End )

Creates a closed range [Start..End].

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

int main( void )
{
   init_tap_c_();

   cRange range = closed_range_c_( -3, 3 );

   expect_c_( range.min == -3 );
   expect_c_( range.max == 3 );

   return finish_tap_c_();
}

open_closed_range_c_

#define open_closed_range_c_( Start, End )

Creates a half opened range (Start..End].

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

int main( void )
{
   init_tap_c_();

   cRange range = open_closed_range_c_( -3, 3 );

   expect_c_( range.min == -2 );
   expect_c_( range.max == 3 );

   return finish_tap_c_();
}

open_range_c_

#define open_range_c_( Start, End )

Creates a open range (Start..End).

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

int main( void )
{
   init_tap_c_();

   cRange rng = open_range_c_( -3, 3 );

   expect_c_( rng.min == -2 );
   expect_c_( rng.max == 2 );

   return finish_tap_c_();
}

sized_range_c_

#define sized_range_c_( Start, Size )

Creates a range that uses Start as Min Value and contains Size values.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cRange.c"

int main( void )
{
   init_tap_c_();

   cRange range = sized_range_c_( -3, 7 );

   expect_c_( range.min == -3 );
   expect_c_( range.max == 3 );

   return finish_tap_c_();
}

overall

clamp_into_range_c

int64_t clamp_into_range_c( cRange range, int64_t value );

Clamps the value into the defined range.

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

int main( void )
{
   init_tap_c_();

   cRange range = { .min = 6, .max = 99 };

   expect_c_( clamp_into_range_c( range, 1 ) == 6 );
   expect_c_( clamp_into_range_c( range, 128 ) == 99 );

   return finish_tap_c_();
}

eq_range_c

bool eq_range_c( cRange a, cRange b );

Returns true if both ranges are equal, otherwise false.

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

int main( void )
{
   init_tap_c_();

   cRange a = { .min=22, .max=55 };
   cRange b = { .min=22, .max=53 };

   expect_c_( eq_range_c( a, a ) );
   expect_c_( not eq_range_c( a, b ) );

   return finish_tap_c_();
}

in_range_c

bool in_range_c( cRange range, int64_t value );

Returns true if the range contains value, otherwise false.

range_is_valid_c

bool range_is_valid_c( cRange range );

Returns true if the range is valid, otherwise false.

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

int main( void )
{
   init_tap_c_();

   cRange valid = { .min = 0, .max = 10 };
   expect_c_( range_is_valid_c( valid ) );

   cRange invalid = { .min = 0, .max = -10 };
   expect_c_( not range_is_valid_c( invalid ) );

   return finish_tap_c_();
}

range_size_c

int64_t range_size_c( cRange range );

Returns the number of values the range contains.

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

int main( void )
{
   init_tap_c_();

   cRange range = closed_range_c_( 18, 25 );
   expect_c_( range_size_c( range ) == 8 );

   range = sized_range_c_( 18, 8 );
   expect_c_( range_size_c( range ) == 8 );

   return finish_tap_c_();
}

shift_range_c

cRange shift_range_c( cRange range, int64_t distance );

Shifts the range forward(positive distance) or backwards(negative distance).

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

int main( void )
{
   init_tap_c_();

   cRange range = closed_range_c_( 0, 9 );

   cRange next = shift_range_c( range, range_size_c( range ) );
   expect_c_( next.min == 10 );
   expect_c_( next.max == 19 );

   cRange prev = shift_range_c( range, -1 * range_size_c( range ) );
   expect_c_( prev.min == -10 );
   expect_c_( prev.max == -1 );

   return finish_tap_c_();
}