Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
states.cpp
Go to the documentation of this file.
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>
41 {
42  nstates = 0;
43  currentstate = 0;
44  stateval = NULL;
45 }
46 
47 /* The copy constructor creates a new instance based on the given
48  states object. */
49 template <class state_type_t>
51 {
52  nstates = c.nstates;
53  currentstate = c.currentstate;
54 
55  // copy state variables if necessary
56  if (nstates && c.stateval)
57  {
58  int size = nstates * sizeof (state_type_t) * STATE_NUM;
59  stateval = (state_type_t *) malloc (size);
60  memcpy (stateval, c.stateval, size);
61  }
62  else stateval = NULL;
63 }
64 
65 // Destructor deletes a states object.
66 template <class state_type_t>
68 {
69  if (stateval) free (stateval);
70 }
71 
72 /* The function allocates and initializes memory for the save-state
73  variables. */
74 template <class state_type_t>
76 {
77  if (stateval != NULL) free (stateval);
78  if (nstates)
79  {
80  stateval = (state_type_t *)
81  calloc (nstates, sizeof (state_type_t) * STATE_NUM);
82  }
83  currentstate = 0;
84 }
85 
86 // Clears the save-state variables.
87 template <class state_type_t>
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 state_type_t states<state_type_t>::getState (int state, int n)
100 {
101  int i = (n + currentstate) & STATE_MASK;
102  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 void states<state_type_t>::setState (int state, state_type_t val, int n)
110 {
111  int i = (n + currentstate) & STATE_MASK;
112  stateval[(state << STATE_SHIFT) + i] = val;
113 }
114 
115 // Shifts one state forward.
116 template <class state_type_t>
118 {
119  if (--currentstate < 0) currentstate = STATE_NUM - 1;
120 }
121 
122 // Shifts one state backward.
123 template <class state_type_t>
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 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  state_type_t * p = &stateval[state << STATE_SHIFT];
136  // fill each array member with the supplied value
137  for (int i = 0; i < STATE_NUM; i++)
138  {
139  *p++ = val;
140  }
141 }
142 
143 /* This function stores the values of the given state into the given
144  pointer location. */
145 template <class state_type_t>
146 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  for (int i = 0; i < STATE_NUM; i++)
151  {
152  values[i] = getState (state, i);
153  }
154 }
155 
156 /* This function stores the values in the given pointer location
157  into the state. */
158 template <class state_type_t>
159 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  for (int i = 0; i < STATE_NUM; i++)
164  {
165  setState (state, values[i], i);
166  }
167 }
168 
169 } // namespace qucs
#define STATE_MASK
Definition: states.cpp:34
size
Definition: parse_vcd.y:203
int currentstate
Definition: states.h:65
void fillState(int, state_type_t)
Definition: states.cpp:132
#define STATE_SHIFT
Definition: states.cpp:32
void prevState(void)
Definition: states.cpp:124
state_type_t * stateval
Definition: states.h:63
n
Definition: parse_citi.y:147
void initStates(void)
Definition: states.cpp:75
i
Definition: parse_mdl.y:516
template class for storing state variables.
Definition: states.h:38
void nextState(void)
Definition: states.cpp:117
free($1)
state_type_t getState(int, int n=0)
Definition: states.cpp:99
#define STATE_NUM
Definition: states.cpp:33
void saveState(int, state_type_t *)
Definition: states.cpp:146
void inputState(int, state_type_t *)
Definition: states.cpp:159
void clearStates(void)
Definition: states.cpp:88
int nstates
Definition: states.h:64
values
Definition: parse_spice.y:223
void setState(int, state_type_t, int n=0)
Definition: states.cpp:109