LCOV - code coverage report
Current view: top level - src - states.cpp (source / functions) Hit Total Coverage
Test: qucs-core-0.0.19 Code Coverage Lines: 33 46 71.7 %
Date: 2015-01-05 16:01:02 Functions: 8 10 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 18 66.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * states.cpp - save-state variable template class implementation
       3                 :            :  *
       4                 :            :  * Copyright (C) 2004 Stefan Jahn <stefan@lkcc.org>
       5                 :            :  *
       6                 :            :  * This is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2, or (at your option)
       9                 :            :  * any later version.
      10                 :            :  *
      11                 :            :  * This software is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this package; see the file COPYING.  If not, write to
      18                 :            :  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
      19                 :            :  * Boston, MA 02110-1301, USA.
      20                 :            :  *
      21                 :            :  * $Id$
      22                 :            :  *
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <stdio.h>
      26                 :            : #include <stdlib.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include "states.h"
      30                 :            : 
      31                 :            : // Some definitions for the save-state variables.
      32                 :            : #define STATE_SHIFT 3
      33                 :            : #define STATE_NUM   8
      34                 :            : #define STATE_MASK  7
      35                 :            : 
      36                 :            : namespace qucs {
      37                 :            : 
      38                 :            : // Constructor creates an unnamed instance of the states class.
      39                 :            : template <class state_type_t>
      40                 :     126026 : states<state_type_t>::states ()
      41                 :            : {
      42                 :     126026 :     nstates = 0;
      43                 :     126026 :     currentstate = 0;
      44                 :     126026 :     stateval = NULL;
      45                 :     126026 : }
      46                 :            : 
      47                 :            : /* The copy constructor creates a new instance based on the given
      48                 :            :    states object. */
      49                 :            : template <class state_type_t>
      50                 :          0 : states<state_type_t>::states (const states & c)
      51                 :            : {
      52                 :          0 :     nstates = c.nstates;
      53                 :          0 :     currentstate = c.currentstate;
      54                 :            : 
      55                 :            :     // copy state variables if necessary
      56 [ #  # ][ #  # ]:          0 :     if (nstates && c.stateval)
      57                 :            :     {
      58                 :          0 :         int size = nstates * sizeof (state_type_t) * STATE_NUM;
      59                 :          0 :         stateval = (state_type_t *) malloc (size);
      60                 :          0 :         memcpy (stateval, c.stateval, size);
      61                 :            :     }
      62                 :          0 :     else stateval = NULL;
      63                 :          0 : }
      64                 :            : 
      65                 :            : // Destructor deletes a states object.
      66                 :            : template <class state_type_t>
      67                 :     126026 : states<state_type_t>::~states ()
      68                 :            : {
      69         [ +  + ]:     126026 :     if (stateval) free (stateval);
      70                 :     126026 : }
      71                 :            : 
      72                 :            : /* The function allocates and initializes memory for the save-state
      73                 :            :    variables. */
      74                 :            : template <class state_type_t>
      75                 :        870 : void states<state_type_t>::initStates (void)
      76                 :            : {
      77         [ +  + ]:        870 :     if (stateval != NULL) free (stateval);
      78         [ +  + ]:        870 :     if (nstates)
      79                 :            :     {
      80                 :        229 :         stateval = (state_type_t *)
      81                 :            :                    calloc (nstates, sizeof (state_type_t) * STATE_NUM);
      82                 :            :     }
      83                 :        870 :     currentstate = 0;
      84                 :        870 : }
      85                 :            : 
      86                 :            : // Clears the save-state variables.
      87                 :            : template <class state_type_t>
      88                 :            : void states<state_type_t>::clearStates (void)
      89                 :            : {
      90                 :            :     if (nstates && stateval)
      91                 :            :         memset (stateval, 0, nstates * sizeof (state_type_t) * STATE_NUM);
      92                 :            :     currentstate = 0;
      93                 :            : }
      94                 :            : 
      95                 :            : /* The function returns a save-state variable at the given position.
      96                 :            :    Higher positions mean earlier states.  By default the function
      97                 :            :    returns the current state of the save-state variable. */
      98                 :            : template <class state_type_t>
      99                 :   25226432 : state_type_t states<state_type_t>::getState (int state, int n)
     100                 :            : {
     101                 :   25226432 :     int i = (n + currentstate) & STATE_MASK;
     102                 :   25226432 :     return stateval[(state << STATE_SHIFT) + i];
     103                 :            : }
     104                 :            : 
     105                 :            : /* This function applies the given value to a save-state variable.
     106                 :            :    Higher positions mean earlier states.  By default the function sets
     107                 :            :    the current state of the save-state variable. */
     108                 :            : template <class state_type_t>
     109                 :    3647597 : void states<state_type_t>::setState (int state, state_type_t val, int n)
     110                 :            : {
     111                 :    3647597 :     int i = (n + currentstate) & STATE_MASK;
     112                 :    3647597 :     stateval[(state << STATE_SHIFT) + i] = val;
     113                 :    3647597 : }
     114                 :            : 
     115                 :            : // Shifts one state forward.
     116                 :            : template <class state_type_t>
     117                 :    1982427 : void states<state_type_t>::nextState (void)
     118                 :            : {
     119         [ +  + ]:    1982427 :     if (--currentstate < 0) currentstate = STATE_NUM - 1;
     120                 :    1982427 : }
     121                 :            : 
     122                 :            : // Shifts one state backward.
     123                 :            : template <class state_type_t>
     124                 :            : void states<state_type_t>::prevState (void)
     125                 :            : {
     126                 :            :     currentstate = (currentstate + 1) & STATE_MASK;
     127                 :            : }
     128                 :            : 
     129                 :            : /* This function applies the given value to a save-state variable through
     130                 :            :    all history values. */
     131                 :            : template <class state_type_t>
     132                 :      16652 : void states<state_type_t>::fillState (int state, state_type_t val)
     133                 :            : {
     134                 :            :     // get a pointer to the start of the state array
     135                 :      16652 :     state_type_t * p = &stateval[state << STATE_SHIFT];
     136                 :            :     // fill each array member with the supplied value
     137         [ +  + ]:     149868 :     for (int i = 0; i < STATE_NUM; i++)
     138                 :            :     {
     139                 :     133216 :         *p++ = val;
     140                 :            :     }
     141                 :      16652 : }
     142                 :            : 
     143                 :            : /* This function stores the values of the given state into the given
     144                 :            :    pointer location. */
     145                 :            : template <class state_type_t>
     146                 :     214550 : void states<state_type_t>::saveState (int state, state_type_t * values)
     147                 :            : {
     148                 :            :     // loop through the list of states copying them into the
     149                 :            :     // supplied input array
     150         [ +  + ]:    1930950 :     for (int i = 0; i < STATE_NUM; i++)
     151                 :            :     {
     152                 :    1716400 :         values[i] = getState (state, i);
     153                 :            :     }
     154                 :     214550 : }
     155                 :            : 
     156                 :            : /* This function stores the values in the given pointer location
     157                 :            :    into the state. */
     158                 :            : template <class state_type_t>
     159                 :          0 : void states<state_type_t>::inputState (int state, state_type_t * values)
     160                 :            : {
     161                 :            :     // loop through the list of states copying them into the
     162                 :            :     // supplied input array
     163         [ #  # ]:          0 :     for (int i = 0; i < STATE_NUM; i++)
     164                 :            :     {
     165                 :          0 :         setState (state, values[i], i);
     166                 :            :     }
     167                 :          0 : }
     168                 :            : 
     169                 :            : } // namespace qucs

Generated by: LCOV version 1.11