-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmark.hpp
More file actions
227 lines (190 loc) · 6.53 KB
/
cmark.hpp
File metadata and controls
227 lines (190 loc) · 6.53 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// cmark.hpp - 2016 - Atlee Brink
// class cmark: Markov factor utensils
#pragma once
#include <fstream>
#include <iostream>
#include <memory>
#include <unordered_set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<class Flavor = double, class Color = unsigned int, class Hue = unsigned int>
class
cmark {
public:
// a lone box
struct
smarkbox {
vector< pair<Color, size_t> > palette;
vector<Flavor> flavors;
Flavor taste( const vector<Hue> &spectrum ) const {
size_t touch = 0;
for( const auto &band : palette ) touch += spectrum[ band.first ] * band.second;
return flavors[ touch ];
}
};
// a lone line of boxes
class
smarkboxline : public unordered_set< shared_ptr< smarkbox > >
{};
// for spectral analysis of boxen
class
smarkboxprism : public vector< smarkboxline >
{};
static
shared_ptr<smarkbox>
merge (
const vector<Hue> &bandwidths,
const smarkboxline &line,
Color color
) {
// new smarkbox
auto pbox = make_shared<smarkbox>();
// which hold just one color
pbox->palette.resize( 1 );
pbox->palette[0] = { color, 1 };
// ..and all its hues
pbox->flavors.resize( bandwidths[color] );
// what do they taste like?
for( Hue h = 0; h < bandwidths[color]; h++ ) {
Flavor bitetaste = (Flavor)1;
for( auto box : line ) bitetaste *= box->flavors[h];
pbox->flavors[h] = bitetaste;
}
return pbox;
}
static
shared_ptr<smarkbox>
ostracize (
const vector<Hue> &bandwidths,
const smarkboxline &line,
Color bad
) {
// good colors are adjacent to 'line', which is the bad color
unordered_set<Color> goodcolors;
for( const auto pbox : line ) {
for( const auto pair : pbox->palette ) {
if( pair.first != bad ) goodcolors.insert( pair.first );
}
}
// new smarkbox
auto pbox = make_shared<smarkbox>();
pbox->palette.resize( goodcolors.size() );
// moves through available hue patterns
vector< pair<Color, Hue> > pattern( goodcolors.size() );
// assign a palette to the new smarkbox (colors and strides),
// and prepare an initial hue pattern.
size_t i = pbox->palette.size(), width = 1;
for( auto color : goodcolors ) {
pbox->palette[i-1] = { color, width };
width *= bandwidths[ pbox->palette[i-1].first ];
pattern[i-1] = { color, (Hue)0 };
i--;
}
// result of 'pattern' projected onto full spectrum: changes over time
vector< Hue > spectrum( bandwidths.size(), (Hue)0 );
// store flavors of all possible hue patterns from this palette
for( bool hungry = true; hungry; ) {
// sum tastes over bad color
Flavor sumtastes = (Flavor)0;
for( Hue h = 0; h < bandwidths[bad]; h++ ) {
spectrum[bad] = h;
Flavor bitetaste = (Flavor)1;
for( auto box : line ) bitetaste *= box->taste( spectrum );
sumtastes += bitetaste;
}
pbox->flavors.push_back( sumtastes );
// adjust pattern and spectrum for next taste
for( size_t band = pattern.size() - 1;; ) {
++pattern[band].second;
++spectrum[pattern[band].first];
if( pattern[band].second < bandwidths[pattern[band].first] ) break;
if( !band ) {
hungry = false;
break;
}
pattern[band].second = 0;
spectrum[pattern[band].first] = 0;
band--;
}
}
return pbox;
}
// rounds up boxen
static
bool
wranglesmarks(
vector< Hue > &bandwidths,
smarkboxprism &prism,
const string &markfile
) {
ifstream markflow( markfile );
if( !markflow ) {
cerr << "wranglesmarks: markfile \"" << markfile << "\" won't open!\n";
return false;
}
vector< shared_ptr< smarkbox > > smarkorder;
// preamble
{
string type;
markflow >> type;
if( type != "MARKOV" ) {
cerr << "wranglesmarks: markfile \"" << markfile << "\""
<< " has type \"" << type
<< "\", but I only understand type \"MARKOV\"!\n";
return false;
}
size_t colorcount;
markflow >> colorcount;
bandwidths.resize( colorcount );
prism.resize( colorcount, smarkboxline() );
for( auto &width : bandwidths ) {
size_t bigwidth;
markflow >> bigwidth;
width = (Hue)bigwidth;
}
size_t prismcard;
markflow >> prismcard;
smarkorder.resize( prismcard );
for( size_t i = 0; i < prismcard; i++ ) {
auto pbox = make_shared<smarkbox>();
smarkorder[i] = pbox;
size_t palettesize;
markflow >> palettesize;
pbox->palette.resize( palettesize );
for( auto &band : pbox->palette ) {
size_t color;
markflow >> color;
if( color >= colorcount ) {
cerr << "wranglesmarks: markfile \"" << markfile
<< " contains an out-of-range variable index!\n";
return false;
}
band.first = (Color)color;
prism[color].insert( pbox );
}
for(
size_t i = pbox->palette.size(), width = 1;
i > 0;
width *= bandwidths[ pbox->palette[i---1].first ]
) {
pbox->palette[i-1].second = width;
}
}
}
// smarkboxen proper
for( auto &pbox : smarkorder ) {
size_t volume;
markflow >> volume;
pbox->flavors.resize( volume );
for( auto &flavor : pbox->flavors ) markflow >> flavor;
}
if( !markflow.good() ) {
cerr << "wranglesmarks: markfile \"" << markfile << "\" confused me!\n";
return false;
}
return true;
}
};
// end