Skip to content

Commit

Permalink
Avoid calculation of steps in Time::get_steps by adding additional
Browse files Browse the repository at this point in the history
member to Time
  • Loading branch information
jakobj committed Mar 20, 2017
1 parent 9fc8c6b commit aba06cf
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions nestkernel/nest_time.h
Expand Up @@ -296,13 +296,17 @@ class Time
explicit Time( tic_t tics )
: tics( tics )
{
calculate_steps();
} // This doesn't check ranges.
// Ergo: LIM_MAX.tics >= tics >= LIM_MIN.tics or
// tics == LIM_POS_INF.tics or LIM_NEG_INF.tics

public:
Time()
: tics( 0 ){};
: tics( 0 )
{
calculate_steps();
};

// Default copy constructor: assumes legal time object
// Defined by compiler.
Expand All @@ -313,26 +317,30 @@ class Time
? LIM_NEG_INF.tics
: LIM_POS_INF.tics )
{
calculate_steps();
}

Time( step t )
: tics( ( time_abs( t.t ) < LIM_MAX.steps )
? t.t * Range::TICS_PER_STEP
: ( t.t < 0 ) ? LIM_NEG_INF.tics : LIM_POS_INF.tics )
{
calculate_steps();
}

Time( ms t )
: tics( ( time_abs( t.t ) < LIM_MAX.ms )
? static_cast< tic_t >( t.t * Range::TICS_PER_MS + 0.5 )
: ( t.t < 0 ) ? LIM_NEG_INF.tics : LIM_POS_INF.tics )
{
calculate_steps();
}

static tic_t fromstamp( ms_stamp );
Time( ms_stamp t )
: tics( fromstamp( t ) )
{
calculate_steps();
}

/////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -364,13 +372,15 @@ class Time
set_to_zero()
{
tics = 0;
calculate_steps();
}

void
advance()
{
tics += Range::TICS_PER_STEP;
range();
calculate_steps();
}

Time
Expand Down Expand Up @@ -458,6 +468,7 @@ class Time
if ( time_abs( tics ) < LIM_MAX.tics )
return;
tics = ( tics < 0 ) ? LIM_NEG_INF.tics : LIM_POS_INF.tics;
calculate_steps();
}

void
Expand All @@ -474,6 +485,7 @@ class Time
{
tics += t.tics;
range();
calculate_steps();
return *this;
}

Expand Down Expand Up @@ -509,15 +521,21 @@ class Time

delay
get_steps() const
{
return steps_;
}

void
calculate_steps()
{
if ( tics == LIM_POS_INF.tics )
return LIM_POS_INF.steps;
steps_ = LIM_POS_INF.steps;
if ( tics == LIM_NEG_INF.tics )
return LIM_NEG_INF.steps;
steps_ = LIM_NEG_INF.steps;

// round tics up to nearest step
// by adding TICS_PER_STEP-1 before division
return ( tics + Range::TICS_PER_STEP_RND ) * Range::TICS_PER_STEP_INV;
steps_ = ( tics + Range::TICS_PER_STEP_RND ) * Range::TICS_PER_STEP_INV;
}

/**
Expand All @@ -538,6 +556,9 @@ class Time
{
return ld_round( ms * Range::STEPS_PER_MS );
}

private:
unsigned long steps_;
};

/////////////////////////////////////////////////////////////
Expand Down

0 comments on commit aba06cf

Please sign in to comment.