| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#ifdef _OPENMP |
| 28 |
# include <omp.h> |
| 29 |
#endif |
| 30 |
#include <list> |
| 31 |
#include "indexing_func_dim2.hh" |
| 32 |
#include "utility_data_structure/VCData.hh" |
| 33 |
#include "utility_data_structure/Bud.hh" |
| 34 |
#include "utility_data_structure/TreeLattice.hh" |
| 35 |
#include "utility_func/stopx.hh" |
| 36 |
#include "zlog/zlog.hh" |
| 37 |
#include "GenerateRelation.hh" |
| 38 |
|
| 39 |
bool find_bud(const vector<Bud>::const_iterator it_begin, |
| 40 |
const vector<Bud>::const_iterator it_end, const Double& cv2, |
| 41 |
const Bud& budex) |
| 42 |
{ |
| 43 |
const Double Q1 = budex.Q1().Value(); |
| 44 |
const Double Q1_var = cv2 * budex.Q1().Variance(); |
| 45 |
|
| 46 |
const Double diff = sqrt(Q1_var); |
| 47 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 48 |
|
| 49 |
const Int4 ibegin = distance( qdata.begin(), lower_bound( qdata.begin(), qdata.end(), QData(Q1 - diff, 0.0) ) ); |
| 50 |
const Int4 iend = distance( qdata.begin(), upper_bound( qdata.begin()+ibegin, qdata.end(), QData(Q1 + diff, 0.0) ) ); |
| 51 |
|
| 52 |
vector<Bud>::const_iterator it_basket; |
| 53 |
Bud budex2; |
| 54 |
for(Int4 i=ibegin; i<iend; i++) |
| 55 |
{ |
| 56 |
budex2.setIndex( i, budex.iK2(), budex.iK3(), budex.iK4() ); |
| 57 |
it_basket = lower_bound( it_begin, it_end, budex2 ); |
| 58 |
|
| 59 |
if( it_basket < it_end && *it_basket == budex2 ) return true; |
| 60 |
} |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
void findRelation(const Double& cv2, const Int4& max_peak_num, const Int4& max_itnum, |
| 65 |
vector<Bud>& ans) |
| 66 |
{ |
| 67 |
GenerateRelation gen_rel(max_peak_num); |
| 68 |
|
| 69 |
gen_rel.initialize_type2(); |
| 70 |
gen_rel.putRelationCandidate_type2(cv2, ans); |
| 71 |
sort( ans.begin(), ans.end() ); |
| 72 |
|
| 73 |
ZLOG_INFO( "Number of (q1, q2, q3, q4) satisfying 2(q1 + q2) = q3 + q4 : " + num2str(ans.size()) + "\n" ); |
| 74 |
|
| 75 |
const Int4 num_bud = ans.size(); |
| 76 |
|
| 77 |
vector<Bud> bud_tray; |
| 78 |
gen_rel.initialize_type3(); |
| 79 |
gen_rel.putRelationCandidate_type3(cv2, bud_tray); |
| 80 |
if( !bud_tray.empty() ) |
| 81 |
{ |
| 82 |
sort( bud_tray.begin(), bud_tray.end() ); |
| 83 |
|
| 84 |
if( !find_bud(ans.begin(), ans.begin()+num_bud, cv2, bud_tray[0]) ) |
| 85 |
{ |
| 86 |
ans.push_back(bud_tray[0]); |
| 87 |
} |
| 88 |
|
| 89 |
const Int4 max_size = bud_tray.size(); |
| 90 |
|
| 91 |
#ifdef _OPENMP |
| 92 |
#pragma omp parallel for |
| 93 |
#endif |
| 94 |
for(Int4 i_=1; i_<max_size; i_++) |
| 95 |
{ |
| 96 |
const vector<Bud>::const_iterator it=bud_tray.begin()+i_; |
| 97 |
|
| 98 |
if( *(it-1) == *it ) continue; |
| 99 |
if( !find_bud(ans.begin(), ans.begin()+num_bud, cv2, *it) ) |
| 100 |
{ |
| 101 |
#ifdef _OPENMP |
| 102 |
#pragma omp critical |
| 103 |
#endif |
| 104 |
{ |
| 105 |
ans.push_back(*it); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
ZLOG_INFO( "Number of (q?, q2, q3, q4) detected from the equation 3q2 + q4 = 3q3 + q5 : " + num2str( ans.size() - num_bud ) + "\n" ); |
| 112 |
|
| 113 |
sort( ans.begin(), ans.end() ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
// In this function, budex.iK1() < 0 is assumed. |
| 120 |
// On input and output, bud_basket is sorted. |
| 121 |
static void find_new_root_unobs(const Double& cv2, const Bud budex, |
| 122 |
const vector<Bud>& bud_basket, |
| 123 |
set<Bud>& newly_added_peak) |
| 124 |
{ |
| 125 |
const VCData K4 = budex.Q1(); |
| 126 |
const VCData K3 = ( budex.Q2() + budex.Q3() )* 2 - K4; |
| 127 |
|
| 128 |
const Double Q3 = K3.Value(); |
| 129 |
const Double Q3_diff = sqrt( cv2 * K3.Variance() ); |
| 130 |
|
| 131 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 132 |
const pair<vector<QData>::const_iterator, vector<QData>::const_iterator> |
| 133 |
it_pair_dbl2 = closest_qdata(qdata.begin(), qdata.end(), Q3, Q3_diff); |
| 134 |
if( it_pair_dbl2.first >= it_pair_dbl2.second ) return; |
| 135 |
assert( qdata.begin() <= it_pair_dbl2.first ); |
| 136 |
assert( it_pair_dbl2.second <= qdata.end() ); |
| 137 |
|
| 138 |
const Int4 Q3_start = distance(qdata.begin(), it_pair_dbl2.first); |
| 139 |
const Int4 Q3_end = distance(qdata.begin(), it_pair_dbl2.second); |
| 140 |
|
| 141 |
Bud bud_tray = putBud124( budex.iK3(), budex.iK2(), -1 ); |
| 142 |
|
| 143 |
pair<vector<Bud>::const_iterator, vector<Bud>::const_iterator> it_pair; |
| 144 |
vector<Bud>::const_iterator it2; |
| 145 |
|
| 146 |
Vec_BOOL found_flag(Q3_end-Q3_start, false); |
| 147 |
if( K3.Value() <= K4.Value() ) |
| 148 |
{ |
| 149 |
it_pair = equal_range_bud124(bud_basket.begin(), bud_basket.end(), bud_tray.iK1(), bud_tray.iK2(), bud_tray.iK4()); |
| 150 |
for(it2=it_pair.first; it2<it_pair.second; it2++) |
| 151 |
{ |
| 152 |
if( Q3_start <= it2->iK3() && it2->iK3() < Q3_end ) |
| 153 |
{ |
| 154 |
found_flag[it2->iK3() - Q3_start] = true; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
for(Int4 k2=Q3_start, k=0; k2<Q3_end; k2++, k++) |
| 159 |
{ |
| 160 |
if( found_flag[k] ) continue; |
| 161 |
bud_tray.setIndex( bud_tray.iK1(), bud_tray.iK2(), k2, -1 ); |
| 162 |
// bud_basket.insert( upper_bound( bud_basket.begin(), bud_basket.end(), bud_tray ), bud_tray ); |
| 163 |
newly_added_peak.insert( bud_tray ); |
| 164 |
} |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
it_pair = equal_range_bud12(bud_basket.begin(), bud_basket.end(), bud_tray.iK1(), bud_tray.iK2()); |
| 169 |
|
| 170 |
vector<Bud>::const_iterator it, it2; |
| 171 |
for(it2=it_pair.first; it2<it_pair.second; it2++) |
| 172 |
{ |
| 173 |
if( it2->iK3() == -1 && Q3_start <= it2->iK4() && it2->iK4() < Q3_end ) |
| 174 |
{ |
| 175 |
found_flag[it2->iK4() - Q3_start] = true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
for(Int4 k2=Q3_start, k=0; k2<Q3_end; k2++, k++) |
| 180 |
{ |
| 181 |
if( found_flag[k] ) continue; |
| 182 |
bud_tray.setIndex( bud_tray.iK1(), bud_tray.iK2(), -1, k2 ); |
| 183 |
// bud_basket.insert( upper_bound( bud_basket.begin(), bud_basket.end(), bud_tray ), bud_tray ); |
| 184 |
newly_added_peak.insert( bud_tray ); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
// In this function, K1, K2, K34 >= 0. |
| 191 |
// On input and output, bud_basket is sorted. |
| 192 |
static void find_new_root_obs(const Double& cv2, |
| 193 |
const Int4& K1, const Int4& K2, const Int4& K34, |
| 194 |
const vector<Bud>& bud_basket, |
| 195 |
set<Bud>& newly_added_peak) |
| 196 |
{ |
| 197 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 198 |
const Double Q3 = ( qdata[K1].q + qdata[K2].q ) * 2.0 - qdata[K34].q; |
| 199 |
if( Q3 > qdata[K34].q ) return; |
| 200 |
|
| 201 |
Bud bud_tray = putBud124( K1, K2, K34 ); |
| 202 |
pair< vector<Bud>::const_iterator, vector<Bud>::const_iterator> it_pair = equal_range_bud124(bud_basket.begin(), bud_basket.end(), bud_tray.iK1(), bud_tray.iK2(), bud_tray.iK4()); |
| 203 |
if( it_pair.first < it_pair.second ) return; |
| 204 |
|
| 205 |
newly_added_peak.insert( bud_tray ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
// On input and output, bud_basket is sorted. |
| 210 |
static void find_new_root(const Double& cv2, const Bud budex, |
| 211 |
const vector<Bud>& bud_basket, |
| 212 |
set<Bud>& newly_added_peak) |
| 213 |
{ |
| 214 |
if( budex.iK3() < 0 ) |
| 215 |
{ |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if( budex.iK1() < 0 ) |
| 220 |
{ |
| 221 |
find_new_root_unobs(cv2, budex, bud_basket, newly_added_peak); |
| 222 |
} |
| 223 |
else // budex.iK3(), budex.iK1(), budex.iK2() >=0. |
| 224 |
{ |
| 225 |
find_new_root_obs(cv2, budex.iK3(), budex.iK1(), budex.iK2(), bud_basket, newly_added_peak); |
| 226 |
find_new_root_obs(cv2, budex.iK3(), budex.iK2(), budex.iK1(), bud_basket, newly_added_peak); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
// On input and output, bud_basket is sorted. |
| 234 |
static void find_leftandright_branch(const Double& cv2, |
| 235 |
const vector<Bud>& bud_basket, |
| 236 |
Vec_BOOL& root_flag, |
| 237 |
vector< vector<Int4> >& leftbr_tray, |
| 238 |
vector< vector<Int4> >& rightbr_tray) |
| 239 |
{ |
| 240 |
root_flag.clear(); |
| 241 |
leftbr_tray.clear(); |
| 242 |
rightbr_tray.clear(); |
| 243 |
|
| 244 |
const Int4 nsize = bud_basket.size(); |
| 245 |
root_flag.resize(nsize, true); |
| 246 |
leftbr_tray.resize(nsize); |
| 247 |
rightbr_tray.resize(nsize); |
| 248 |
|
| 249 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 250 |
const Int4 bad_num = bud_basket.size(); |
| 251 |
|
| 252 |
Int4 k2; |
| 253 |
Double diff, Q3; |
| 254 |
Bud budex; |
| 255 |
pair<vector<Bud>::const_iterator, vector<Bud>::const_iterator> it_pair; |
| 256 |
|
| 257 |
/* 2011.10.19 VIC Tamura */ |
| 258 |
Int4 LOOP_COUNTER = 0; |
| 259 |
|
| 260 |
#ifdef _OPENMP |
| 261 |
#pragma omp parallel for private(k2, diff, it_pair, budex, Q3) |
| 262 |
#endif |
| 263 |
for(Int4 k=0; k<bad_num; k++) |
| 264 |
{ |
| 265 |
/* 2011.10.19 VIC Tamura */ |
| 266 |
SET_PROGRESS(100*(LOOP_COUNTER++)/bad_num, 5, 1); // critical, but works |
| 267 |
if(IS_CANSELED()) continue; |
| 268 |
|
| 269 |
const vector<Bud>::const_iterator it=bud_basket.begin() + k; |
| 270 |
if( it->iK3() == it->iK4() ) continue; |
| 271 |
|
| 272 |
if( it->Q2() < it->Q1() ) |
| 273 |
{ |
| 274 |
budex = putBud124(it->iK3(), it->iK2(), it->iK1()); |
| 275 |
Q3 = ( it->Q2().Value() + it->Q3().Value() )* 2.0 - it->Q1().Value(); |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
budex = putBud124(it->iK3(), it->iK1(), it->iK2()); |
| 280 |
Q3 = ( it->Q1().Value() + it->Q3().Value() )* 2.0 - it->Q2().Value(); |
| 281 |
} |
| 282 |
|
| 283 |
it_pair = equal_range_bud124(bud_basket.begin(), bud_basket.end(), budex.iK1(), budex.iK2(), budex.iK4()); |
| 284 |
|
| 285 |
k2 = distance( bud_basket.begin(), it_pair.first ); |
| 286 |
for(vector<Bud>::const_iterator it2=it_pair.first; it2<it_pair.second; it2++, k2++) |
| 287 |
{ |
| 288 |
if( budex.iK1() < 0 || budex.iK2() < 0 || budex.iK4() < 0 ) |
| 289 |
{ |
| 290 |
diff = qdata[it2->iK3()].q - Q3; |
| 291 |
if( diff * diff > cv2 * qdata[it2->iK3()].q_var ) continue; |
| 292 |
} |
| 293 |
if( it->iK3() == budex.iK2() ) |
| 294 |
{ |
| 295 |
#ifdef _OPENMP |
| 296 |
#pragma omp critical (lhs) |
| 297 |
#endif |
| 298 |
{ |
| 299 |
leftbr_tray[k2].push_back(k); |
| 300 |
} |
| 301 |
} |
| 302 |
if( it->iK3() == budex.iK1() ) |
| 303 |
{ |
| 304 |
#ifdef _OPENMP |
| 305 |
#pragma omp critical (rhs) |
| 306 |
#endif |
| 307 |
{ |
| 308 |
rightbr_tray[k2].push_back(k); |
| 309 |
} |
| 310 |
} |
| 311 |
root_flag[k] = false; |
| 312 |
} |
| 313 |
} |
| 314 |
/* 2011.10.19 VIC Tamura */ |
| 315 |
CHECK_INTERRUPTION(); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
// On input and output, bud_basket is sorted. |
| 321 |
static void find_leftandright_root(const Double& cv2, |
| 322 |
const Bud& budex, const vector<Bud>& bud_basket, |
| 323 |
vector<Int4>& root_leftbr, |
| 324 |
vector<Int4>& root_rightbr) |
| 325 |
{ |
| 326 |
root_leftbr.clear(); |
| 327 |
root_rightbr.clear(); |
| 328 |
|
| 329 |
const vector<QData>& qdata = VCData::putPeakQData(); |
| 330 |
|
| 331 |
Int4 k2; |
| 332 |
Double Q4, diff; |
| 333 |
vector<Bud>::const_iterator it2; |
| 334 |
pair<vector<Bud>::const_iterator, vector<Bud>::const_iterator> it2_pair; |
| 335 |
|
| 336 |
// Find a bud taking the root on the left branch. |
| 337 |
Bud bud_tray = putBud123( budex.iK2(), budex.iK3(), budex.iK1() ); |
| 338 |
|
| 339 |
Q4 = ( budex.Q2().Value() + budex.Q3().Value() )*2.0 - budex.Q1().Value(); |
| 340 |
if( Q4 > budex.Q1().Value() ) |
| 341 |
{ |
| 342 |
it2_pair = equal_range_bud12( bud_basket.begin(), bud_basket.end(), bud_tray.iK1(), bud_tray.iK2() ); |
| 343 |
k2 = distance( bud_basket.begin(), it2_pair.first ); |
| 344 |
for(it2=it2_pair.first; it2<it2_pair.second; it2++, k2++) |
| 345 |
{ |
| 346 |
if( it2->iK3() != bud_tray.iK3() ) continue; |
| 347 |
if( it2->iK3() == it2->iK4() ) continue; |
| 348 |
if( bud_tray.iK1() < 0 || bud_tray.iK2() < 0 || bud_tray.iK3() < 0 ) |
| 349 |
{ |
| 350 |
diff = qdata[it2->iK4()].q - Q4; |
| 351 |
if( diff * diff > cv2 * qdata[it2->iK4()].q_var ) continue; |
| 352 |
} |
| 353 |
root_leftbr.push_back(k2); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
// Find a bud taking the root on the right branch. |
| 360 |
if( budex.iK1() == budex.iK2() ) |
| 361 |
{ |
| 362 |
root_rightbr = root_leftbr; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
bud_tray = putBud123( budex.iK1(), budex.iK3(), budex.iK2() ); |
| 367 |
|
| 368 |
Q4 = ( budex.Q1().Value() + budex.Q3().Value() )*2.0 - budex.Q2().Value(); |
| 369 |
if( Q4 > budex.Q2().Value() ) |
| 370 |
{ |
| 371 |
it2_pair = equal_range_bud12( bud_basket.begin(), bud_basket.end(), bud_tray.iK1(), bud_tray.iK2() ); |
| 372 |
k2 = distance( bud_basket.begin(), it2_pair.first ); |
| 373 |
for(it2=it2_pair.first; it2<it2_pair.second; it2++, k2++) |
| 374 |
{ |
| 375 |
if( it2->iK3() != bud_tray.iK3() ) continue; |
| 376 |
if( it2->iK3() == it2->iK4() ) continue; |
| 377 |
if( bud_tray.iK1() < 0 || bud_tray.iK2() < 0 || bud_tray.iK3() < 0 ) |
| 378 |
{ |
| 379 |
// if( it2->iK3() < 0 ) continue; |
| 380 |
diff = qdata[it2->iK4()].q - Q4; |
| 381 |
if( diff * diff > cv2 * qdata[it2->iK4()].q_var ) continue; |
| 382 |
} |
| 383 |
root_rightbr.push_back(k2); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
// On input, bud_basket is sorted. |
| 391 |
void findNeighborRelation(const Double& cv2, |
| 392 |
vector<Bud>& bud_basket, |
| 393 |
Vec_BOOL& root_flag, |
| 394 |
vector< vector<Int4> >& leftbr_tray, |
| 395 |
vector< vector<Int4> >& rightbr_tray, |
| 396 |
vector< vector<Int4> >& root_leftbr, |
| 397 |
vector< vector<Int4> >& root_rightbr) |
| 398 |
{ |
| 399 |
vector<Bud> new_bud_tray = bud_basket; |
| 400 |
set<Bud> new_bud_tray_all; |
| 401 |
|
| 402 |
do |
| 403 |
{ |
| 404 |
/* 2011.10.19 VIC Tamura */ |
| 405 |
SET_PROGRESS(100*(bud_basket.size()-new_bud_tray.size())/bud_basket.size(), 4, 1); |
| 406 |
CHECK_INTERRUPTION(); |
| 407 |
|
| 408 |
const Int4 isize = new_bud_tray.size(); |
| 409 |
#ifdef _OPENMP |
| 410 |
#pragma omp parallel |
| 411 |
#endif |
| 412 |
{ |
| 413 |
set<Bud> new_bud; |
| 414 |
|
| 415 |
#ifdef _OPENMP |
| 416 |
#pragma omp for |
| 417 |
#endif |
| 418 |
for(Int4 i=0; i<isize; i++) |
| 419 |
{ |
| 420 |
find_new_root(cv2, new_bud_tray[i], bud_basket, new_bud); |
| 421 |
} |
| 422 |
|
| 423 |
#ifdef _OPENMP |
| 424 |
#pragma omp single |
| 425 |
#endif |
| 426 |
{ |
| 427 |
new_bud_tray.clear(); |
| 428 |
} |
| 429 |
|
| 430 |
#ifdef _OPENMP |
| 431 |
#pragma omp critical |
| 432 |
#endif |
| 433 |
{ |
| 434 |
for(set<Bud>::const_iterator it=new_bud.begin(); it!=new_bud.end(); it++) |
| 435 |
{ |
| 436 |
if( new_bud_tray_all.find(*it) != new_bud_tray_all.end() ) continue; |
| 437 |
new_bud_tray_all.insert(*it); |
| 438 |
new_bud_tray.push_back(*it); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} while( !new_bud_tray.empty() ); |
| 443 |
|
| 444 |
bud_basket.insert(bud_basket.end(), new_bud_tray_all.begin(), new_bud_tray_all.end()); |
| 445 |
sort( bud_basket.begin(), bud_basket.end() ); |
| 446 |
|
| 447 |
const Int4 nsize = bud_basket.size(); |
| 448 |
|
| 449 |
find_leftandright_branch(cv2, bud_basket, |
| 450 |
root_flag, leftbr_tray, rightbr_tray); |
| 451 |
|
| 452 |
root_leftbr.clear(); |
| 453 |
root_rightbr.clear(); |
| 454 |
|
| 455 |
root_leftbr.resize(nsize); |
| 456 |
root_rightbr.resize(nsize); |
| 457 |
|
| 458 |
/* 2011.10.19 VIC Tamura */ |
| 459 |
Int4 LOOP_COUNTER = 0; |
| 460 |
|
| 461 |
#ifdef _OPENMP |
| 462 |
#pragma omp parallel for |
| 463 |
#endif |
| 464 |
for(Int4 i=0; i<nsize; i++) |
| 465 |
{ |
| 466 |
/* 2011.10.19 VIC Tamura */ |
| 467 |
SET_PROGRESS(100*(LOOP_COUNTER++)/nsize, 6, 1); // critical, but works |
| 468 |
if(IS_CANSELED()) continue; |
| 469 |
|
| 470 |
if( !root_flag[i] ) continue; |
| 471 |
if( bud_basket[i].iK3() == bud_basket[i].iK4() ) continue; |
| 472 |
|
| 473 |
find_leftandright_root(cv2, bud_basket[i], bud_basket, root_leftbr[i], root_rightbr[i]); |
| 474 |
} |
| 475 |
|
| 476 |
/* 2011.10.19 VIC Tamura */ |
| 477 |
CHECK_INTERRUPTION(); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
static void construct_tree(const Int4& k, |
| 486 |
const vector<Bud>::const_iterator& it_begin, |
| 487 |
const vector< vector<Int4> >& leftbr_tray, |
| 488 |
const vector< vector<Int4> >& rightbr_tray, |
| 489 |
TreeLattice& tree_tray) |
| 490 |
{ |
| 491 |
const vector<Bud>::const_iterator& it = it_begin + k; |
| 492 |
|
| 493 |
TreeLattice tree_left, tree_right; |
| 494 |
tree_left.setRoot(it->iK1(), it->iK4()); |
| 495 |
tree_right.setRoot(it->iK2(), it->iK4()); |
| 496 |
|
| 497 |
if( !leftbr_tray[k].empty() ) |
| 498 |
{ |
| 499 |
TreeLattice tree_left2; |
| 500 |
for(vector<Int4>::const_iterator it2=leftbr_tray[k].begin(); it2!=leftbr_tray[k].end(); it2++) |
| 501 |
{ |
| 502 |
if( *it2 != k ) |
| 503 |
{ |
| 504 |
construct_tree(*it2, it_begin, leftbr_tray, rightbr_tray, tree_left2); |
| 505 |
if( tree_left.Root().count_bud() < tree_left2.Root().count_bud() ) |
| 506 |
{ |
| 507 |
tree_left = tree_left2; |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
if( tree_left.Root().Right() != it->iK4() ) tree_left.swapBranch(); |
| 512 |
} |
| 513 |
|
| 514 |
if( !rightbr_tray[k].empty() ) |
| 515 |
{ |
| 516 |
TreeLattice tree_right2; |
| 517 |
for(vector<Int4>::const_iterator it2=rightbr_tray[k].begin(); it2!=rightbr_tray[k].end(); it2++) |
| 518 |
{ |
| 519 |
if( *it2 != k ) |
| 520 |
{ |
| 521 |
construct_tree(*it2, it_begin, leftbr_tray, rightbr_tray, tree_right2); |
| 522 |
if( tree_right.Root().count_bud() < tree_right2.Root().count_bud() ) |
| 523 |
{ |
| 524 |
tree_right = tree_right2; |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
if( tree_right.Root().Right() != it->iK4() ) tree_right.swapBranch(); |
| 529 |
} |
| 530 |
|
| 531 |
tree_tray.clear(); |
| 532 |
tree_tray.setRoot(tree_left.Root(), tree_right.Root()); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
void constructTree(const vector<Bud>& bud_basket, |
| 537 |
const Vec_BOOL& root_flag, |
| 538 |
const vector< vector<Int4> >& leftbr_tray, |
| 539 |
const vector< vector<Int4> >& rightbr_tray, |
| 540 |
const vector< vector<Int4> >& root_leftbr_tray, |
| 541 |
const vector< vector<Int4> >& root_rightbr_tray, const Int4& MAX_TREE, |
| 542 |
vector<TreeLattice>& tree_tray) |
| 543 |
{ |
| 544 |
tree_tray.clear(); |
| 545 |
const Int4 nsize = bud_basket.size(); |
| 546 |
|
| 547 |
/* 2011.10.19 VIC Tamura */ |
| 548 |
Int4 LOOP_COUNTER = 0; |
| 549 |
|
| 550 |
#ifdef _OPENMP |
| 551 |
#pragma omp parallel |
| 552 |
#endif |
| 553 |
{ |
| 554 |
TreeLattice chibi_tree; |
| 555 |
TreeLattice tree_left, tree_left2, tree_right, tree_right2; |
| 556 |
Int4 index; |
| 557 |
multiset<TreeLattice> tree_tray2; |
| 558 |
|
| 559 |
#ifdef _OPENMP |
| 560 |
#pragma omp for |
| 561 |
#endif |
| 562 |
for(Int4 k=0; k<nsize; k++) |
| 563 |
{ |
| 564 |
/* 2011.10.19 VIC Tamura */ |
| 565 |
SET_PROGRESS(100*(LOOP_COUNTER++)/nsize, 7, 1); // critical, but works |
| 566 |
if(IS_CANSELED()) continue; |
| 567 |
|
| 568 |
if( !root_flag[k] ) continue; |
| 569 |
const vector<Bud>::const_iterator it = bud_basket.begin() + k; |
| 570 |
|
| 571 |
const Int4 lsize = root_leftbr_tray[k].size(); |
| 572 |
const Int4 rsize = root_rightbr_tray[k].size(); |
| 573 |
|
| 574 |
// In the case of super-basis, avoid duplication of trees. |
| 575 |
for(index=0; index<lsize; index++) |
| 576 |
{ |
| 577 |
if( k > root_leftbr_tray[k][index] ) break; |
| 578 |
} |
| 579 |
if( index<lsize ) continue; |
| 580 |
for(index=0; index<rsize; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
index++) |
| 585 |
{ |
| 586 |
if( k > root_rightbr_tray[k][index] ) break; |
| 587 |
} |
| 588 |
if( index<rsize ) continue; |
| 589 |
construct_tree(k, bud_basket.begin(), leftbr_tray, rightbr_tray, chibi_tree); |
| 590 |
|
| 591 |
if( bud_basket[k].iK3() == bud_basket[k].iK4() ) |
| 592 |
{ |
| 593 |
chibi_tree.setRootEqualUpper(); |
| 594 |
} |
| 595 |
else if( it->IsSuperBasisObtuse() ) |
| 596 |
{ |
| 597 |
tree_left.setRoot( it->iK2(), it->iK3() ); |
| 598 |
for(vector<Int4>::const_iterator it2=root_leftbr_tray[k].begin(); it2<root_leftbr_tray[k].end(); it2++) |
| 599 |
{ |
| 600 |
construct_tree(*it2, bud_basket.begin(), leftbr_tray, rightbr_tray, tree_left2); |
| 601 |
if( tree_left.Root().count_bud() < tree_left2.Root().count_bud() ) |
| 602 |
{ |
| 603 |
tree_left = tree_left2; |
| 604 |
} |
| 605 |
} |
| 606 |
chibi_tree.setRootOnLeftBranch(tree_left.Root()); |
| 607 |
|
| 608 |
tree_right.setRoot( it->iK3(), it->iK1() ); |
| 609 |
for(vector<Int4>::const_iterator it2=root_rightbr_tray[k].begin(); it2<root_rightbr_tray[k].end(); it2++) |
| 610 |
{ |
| 611 |
construct_tree(*it2, bud_basket.begin(), leftbr_tray, rightbr_tray, tree_right2); |
| 612 |
if( tree_right.Root().count_bud() < tree_right2.Root().count_bud() ) |
| 613 |
{ |
| 614 |
tree_right = tree_right2; |
| 615 |
} |
| 616 |
} |
| 617 |
chibi_tree.setRootOnRightBranch(tree_right.Root()); |
| 618 |
} |
| 619 |
else if( it->Q1() < it->Q2() ) |
| 620 |
{ |
| 621 |
NodeB node = chibi_tree.Root(); |
| 622 |
if( lsize != 0 ) |
| 623 |
{ |
| 624 |
construct_tree(root_leftbr_tray[k][0], bud_basket.begin(), leftbr_tray, rightbr_tray, tree_left); |
| 625 |
for(vector<Int4>::const_iterator it2=root_leftbr_tray[k].begin()+1; it2<root_leftbr_tray[k].end(); it2++) |
| 626 |
{ |
| 627 |
construct_tree(*it2, bud_basket.begin(), leftbr_tray, rightbr_tray, tree_left2); |
| 628 |
if( tree_left.Root().count_bud() < tree_left2.Root().count_bud() ) |
| 629 |
{ |
| 630 |
tree_left = tree_left2; |
| 631 |
} |
| 632 |
} |
| 633 |
if( tree_left.Root().Right() != it->iK2() ) tree_left.swapBranch(); |
| 634 |
chibi_tree.setRoot(node, tree_left.Root()); |
| 635 |
} |
| 636 |
else |
| 637 |
{ |
| 638 |
const NodeB node2(it->iK3(), it->iK2()); |
| 639 |
chibi_tree.setRoot(node, node2); |
| 640 |
} |
| 641 |
} |
| 642 |
else |
| 643 |
{ |
| 644 |
NodeB nodex = chibi_tree.Root(); |
| 645 |
nodex.swapBranch(); |
| 646 |
if( rsize != 0 ) |
| 647 |
{ |
| 648 |
construct_tree(root_rightbr_tray[k][0], bud_basket.begin(), leftbr_tray, rightbr_tray, tree_right); |
| 649 |
for(vector<Int4>::const_iterator it2=root_rightbr_tray[k].begin()+1; it2<root_rightbr_tray[k].end(); it2++) |
| 650 |
{ |
| 651 |
construct_tree(*it2, bud_basket.begin(), leftbr_tray, rightbr_tray, tree_right2); |
| 652 |
if( tree_right.Root().count_bud() < tree_right2.Root().count_bud() ) |
| 653 |
{ |
| 654 |
tree_right = tree_right2; |
| 655 |
} |
| 656 |
} |
| 657 |
if( tree_right.Root().Right() != it->iK1() ) tree_right.swapBranch(); |
| 658 |
chibi_tree.setRoot(tree_right.Root(), nodex); |
| 659 |
} |
| 660 |
else |
| 661 |
{ |
| 662 |
const NodeB nodex2(it->iK3(), it->iK1()); |
| 663 |
chibi_tree.setRoot(nodex2, nodex); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
chibi_tree.setSortingCriteria(); |
| 668 |
tree_tray2.insert(chibi_tree); |
| 669 |
if( (Int4)tree_tray2.size() > MAX_TREE ) tree_tray2.erase(--tree_tray2.end()); |
| 670 |
} |
| 671 |
#ifdef _OPENMP |
| 672 |
#pragma omp critical |
| 673 |
#endif |
| 674 |
{ |
| 675 |
tree_tray.insert(tree_tray.end(), tree_tray2.begin(), tree_tray2.end()); |
| 676 |
} |
| 677 |
} |
| 678 |
/* 2011.10.19 VIC Tamura */ |
| 679 |
CHECK_INTERRUPTION(); |
| 680 |
|
| 681 |
sort(tree_tray.begin(), tree_tray.end()); |
| 682 |
if( (Int4)tree_tray.size() > MAX_TREE ) tree_tray.resize(MAX_TREE); |
| 683 |
} |