-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pattern.cpp
177 lines (129 loc) · 4.11 KB
/
Pattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Pattern.cpp: implementation of the Pattern class.
//
// Copyright Gideon Pertzov, 2003
//
// This software is provided "as is" without express or implied
// warranties. You may freely copy and compile this source into
// applications you distribute provided that credit is given to
// the original author.
//
//////////////////////////////////////////////////////////////////////
#include <cstdarg>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Pattern.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Pattern::~Pattern()
{
}
Pattern::Pattern() : _id(-1)
{
}
//====================================================================
// Constructor with pattern dimensions supplied
//====================================================================
Pattern::Pattern(int inSize, int outSize) : _id(-1)
{
assert (inSize > 0 && outSize > 0);
_inVec.resize(inSize);
_outVec.resize(outSize);
}
//====================================================================
// Constructor with pattern data supplied
//====================================================================
Pattern::Pattern( int inSize, int outSize, int id, ... )
{
assert (inSize > 0 && outSize > 0);
_inVec.resize(inSize);
_outVec.resize(outSize);
_id = id;
va_list vl;
va_start(vl, id );
for (int i = 0; i != inSize; ++i)
_inVec[i] = va_arg(vl, double);
for ( i = 0; i != outSize; ++i)
_outVec[i] = va_arg(vl, double);
va_end( vl );
}
//====================================================================
// Get input value
//====================================================================
double Pattern::getInput(int index) const
{
assert( index >= 0 && index < _inVec.size() );
return _inVec[index];
}
//====================================================================
// Get output value
//====================================================================
double Pattern::getOutput(int index) const
{
assert( index >= 0 && index < _outVec.size() );
return _outVec[index];
}
//====================================================================
// Set input value
//====================================================================
void Pattern::setInput(double value, int index)
{
assert( index >= 0 && index < _inVec.size() );
_inVec[index] = value;
}
//====================================================================
// Set output value
//====================================================================
void Pattern::setOutput(double value, int index)
{
assert( index >= 0 && index < _outVec.size() );
_outVec[index] = value;
}
//====================================================================
// Save pattern to file
//====================================================================
bool Pattern::save( ofstream &ost ) const
{
if ( !ost.good() )
return false;
ost << _id << "\t"; // pattern id
int numInputs = _inVec.size();
for(int i = 0; i != numInputs; ++i)
ost << setprecision(16) << _inVec[i] << "\t"; // input values
int numOutputs = _outVec.size();
for(i = 0; i != numOutputs; ++i)
{
ost << setprecision(16) << _outVec[i]; // output values
if ( i != numOutputs-1)
ost << "\t";
}
ost << endl;
return true;
}
//====================================================================
// Load pattern from file
//====================================================================
bool Pattern::load( ifstream &ist )
{
if ( !ist.good() )
return false;
ist >> _id; // pattern id
int numInputs = _inVec.size();
for(int i = 0; i != numInputs; ++i)
ist >> _inVec[i]; // input values
int numOutputs = _outVec.size();
for(i = 0; i != numOutputs; ++i)
ist >> _outVec[i]; // output values
// "eat" white space
char ch = ist.peek();
while ( isspace(ch) )
{
ch = ist.get();
if (ch == EOF)
break;
ch = ist.peek();
}
return true;
}