-
Notifications
You must be signed in to change notification settings - Fork 4
/
hutils.cpp
177 lines (153 loc) · 3.7 KB
/
hutils.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
/**
* @file hutils.cpp
* @brief Various utility functions for SorterHunter program
* @author Bert Dobbelaere bert.o.dobbelaere[at]telenet[dot]be
*
* Copyright (c) 2017 Bert Dobbelaere
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "hutils.h"
#include <cstdio>
/**
* Create orthogonal convex hull
*/
OCH_t::OCH_t()
{
}
/**
* Clear OCH data
*/
void OCH_t::clear()
{
och.clear();
}
/**
* Add a (size, depth) pair to the OCH computation
* @param l length of network found
* @param d depth of network found
* @param true if the network is an "improvement" i.e. belongs to the updated set of OCH pairs that minimize both criteria.
*/
bool OCH_t::improved(u32 l, u32 d)
{
bool matched=false;
for(size_t k=0;k<och.size();k++)
{
if((l>=och[k].size)&&(d>=och[k].depth))
matched=true;
}
if(matched)
return false;
std::vector<OCH_Entry> newch;
OCH_Entry ce;
ce.size=l;
ce.depth=d;
newch.push_back(ce);
for(size_t k=0;k<och.size();k++)
{
if((och[k].size<l)||(och[k].depth<d))
{
newch.push_back(och[k]);
}
}
och=newch;
return true;
}
/**
* Print best performing (length, depth) pairs found so far
*/
void OCH_t::print() const
{
printf("Most performant: [");
for(size_t k=0;k<och.size();k++)
{
printf("(%u,%u)",och[k].size,och[k].depth);
printf("%c", (k<(och.size()-1)) ? ',':']');
}
printf("\r\n");
}
u32 computeDepth(const Network_t &nw)
{
std::vector<SortWord_t> layers;
int nlayers=0;
for(size_t k=0;k<nw.size();k++)
{
u32 i=nw[k].lo;
u32 j=nw[k].hi;
int matchidx=nlayers;
int idx=nlayers-1;
while(idx>=0)
{
if((layers[idx] & ((1ULL<<i)|(1ULL<<j))) == 0)
{
matchidx=idx;
}
else
{
break;
}
idx--;
}
if(matchidx>=nlayers)
{
layers.push_back(0);
nlayers++;
}
layers[matchidx]|= 1ULL<<i;
layers[matchidx]|= 1ULL<<j;
}
return nlayers;
}
void printnw(const Network_t &nw)
{
printf("[");
for(size_t k=0;k<nw.size();k++)
{
printf("(%u,%u)",nw[k].lo,nw[k].hi);
printf("%c", ((k+1)<nw.size()) ? ',':']');
}
printf("}\r\n");
}
void symmetricExpansion(u8 ninputs, const Network_t &inpairs, Network_t &outpairs)
{
outpairs.clear();
for(size_t k=0;k<inpairs.size();k++)
{
outpairs.push_back(inpairs[k]);
if((inpairs[k].lo+inpairs[k].hi)!=(ninputs-1)) // Don't duplicate pair that maps on itself
{
Pair_t sp={(u8)(ninputs-1-inpairs[k].hi), (u8)(ninputs-1-inpairs[k].lo)};
outpairs.push_back(sp);
}
}
}
void concatNetwork(const Network_t &nw1, const Network_t &nw2, Network_t &result)
{
result=nw1;
result.insert(result.end(),nw2.begin(),nw2.end());
}
void appendNetwork(Network_t &dst, const Network_t &src)
{
if(src.size()>0)
{
dst.insert(dst.end(),src.begin(),src.end());
}
}