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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
/*
Ray
Copyright (C) 2010, 2011, 2012 Sébastien Boisvert
http://DeNovoAssembler.SourceForge.Net/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You have received a copy of the GNU General Public License
along with this program (gpl-3.0.txt).
see <http://www.gnu.org/licenses/>
*/
//#define CONFIG_DEBUG_VIRTUAL_COLORS
//#define ASSERT_LOW_LEVEL
#include "ColorSet.h"
#include <RayPlatform/cryptography/crypto.h>
#include <stdint.h>
#include <iostream>
using namespace std;
#ifdef CONFIG_ASSERT_LOW_LEVEL
#ifndef ASSERT
#define ASSERT
#endif /* ASSERT */
#endif /* ASSERT_LOW_LEVEL */
#ifdef CONFIG_ASSERT
#include <assert.h>
#endif /* ASSERT */
/** Time complexity: constant **/
ColorSet::ColorSet(){
VirtualKmerColorHandle voidColor=createVirtualColorHandleFromScratch();
getVirtualColor(voidColor)->clear();
int i=0;
OPERATION_IN_PLACE_ONE_REFERENCE=i++;
OPERATION_NO_VIRTUAL_COLOR_HAS_HASH_CREATION=i++;
OPERATION_VIRTUAL_COLOR_HAS_COLORS_FETCH=i++;
OPERATION_NO_VIRTUAL_COLOR_HAS_COLORS_CREATION=i++;
OPERATION_NEW_FROM_EMPTY=i++;
OPERATION_NEW_FROM_SCRATCH=i++;
OPERATION_applyHashOperation=i++;
OPERATION_getHash=i++;
OPERATION_getVirtualColorFrom=i++;
OPERATION_createVirtualColorFrom =i++;
OPERATION_incrementReferences =i++;
OPERATION_decrementReferences =i++;
OPERATION_purgeVirtualColor =i++;
OPERATION_allocateVirtualColorHandle =i++;
OPERATION_DUMMY =i++;
for(int i=0;i<OPERATION_DUMMY;i++){
m_operations[i]=0;
}
m_collisions=0;
}
/** O(1) **/
void ColorSet::incrementReferences(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
assert(handle < getTotalNumberOfVirtualColors());
#endif
// we don't care about the empty virtual color
if(handle == NULL_VIRTUAL_COLOR)
return;
VirtualKmerColor*virtualColor=getVirtualColor(handle);
// it is no longer available...
// because we will increment the references
if(virtualColor->getNumberOfReferences()==0){
m_availableHandles.erase(handle);
}
virtualColor->incrementReferences();
#ifdef CONFIG_ASSERT
assert(m_availableHandles.count(handle)==0);
assert(getVirtualColor(handle)->getNumberOfReferences()>=1);
#endif
m_operations[OPERATION_incrementReferences]++;
}
void ColorSet::decrementReferences(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
assert(handle < getTotalNumberOfVirtualColors());
#endif
VirtualKmerColor*virtualColor=getVirtualColor(handle);
// we don't care aboput the empty virtual color
if(handle == NULL_VIRTUAL_COLOR)
return;
#ifdef CONFIG_ASSERT
assert(m_availableHandles.count(handle)==0);
assert(virtualColor->getNumberOfReferences()>0);
#endif
virtualColor->decrementReferences();
// destroy the color if it is not used anymore
// and if it is not the color 0
if(handle!=NULL_VIRTUAL_COLOR && virtualColor->getNumberOfReferences()==0){
purgeVirtualColor(handle);
}
m_operations[OPERATION_decrementReferences]++;
}
void ColorSet::purgeVirtualColor(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
assert(getVirtualColor(handle)->getNumberOfReferences()==0);
#endif
VirtualKmerColor*virtualColorToPurge=getVirtualColor(handle);
removeVirtualColorFromIndex(handle);
// erase all colors
// also sets references to 0
// and resets the hash value
virtualColorToPurge->clear();
// destroy it at will
// actually, they are simply not removed.
// instead, they are re-used.
#ifdef CONFIG_ASSERT
assert(m_availableHandles.count(handle)==0);
#endif
m_availableHandles.insert(handle);
// but we don't remove it from the hash table
// because it may be useful sometime
// correction: we do remove it right now.
#ifdef CONFIG_ASSERT
assert(m_availableHandles.count(handle)>0);
assert(virtualColorToPurge->getNumberOfPhysicalColors()==0);
assert(virtualColorToPurge->getNumberOfReferences()==0);
#endif
m_operations[OPERATION_purgeVirtualColor]++;
}
/** O(1) **/
VirtualKmerColor*ColorSet::getVirtualColor(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
if(handle>= getTotalNumberOfVirtualColors())
cout<<"Error, handle= "<<handle<<" but size= "<<getTotalNumberOfVirtualColors()<<endl;
assert(handle< getTotalNumberOfVirtualColors());
#endif
return & m_virtualColors[handle];
}
int ColorSet::getTotalNumberOfPhysicalColors(){
return m_physicalColors.size();
}
LargeCount ColorSet::getTotalNumberOfVirtualColors(){
return m_virtualColors.size();
}
void ColorSet::printSummary(ostream*out,bool xml){
(*out)<<endl;
(*out)<<"**********************************************************"<<endl;
(*out)<<"Coloring summary"<<endl;
(*out)<<" Number of virtual colors: "<<getTotalNumberOfVirtualColors()<<endl;
(*out)<<" Number of real colors: "<<getTotalNumberOfPhysicalColors()<<endl;
(*out)<<endl;
(*out)<<"Keys in index: "<<m_index.size()<<endl;
(*out)<<"Observed collisions when populating the index: "<<m_collisions<<endl;
(*out)<<"COLOR_NAMESPACE_MULTIPLIER= "<<COLOR_NAMESPACE_MULTIPLIER<<endl;
(*out)<<endl;
(*out)<<"Operations"<<endl;
(*out)<<endl;
(*out)<<" OPERATION_getVirtualColorFrom operations: "<<m_operations[OPERATION_getVirtualColorFrom]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_IN_PLACE_ONE_REFERENCE: "<<m_operations[OPERATION_IN_PLACE_ONE_REFERENCE]<<endl;
(*out)<<" OPERATION_NO_VIRTUAL_COLOR_HAS_HASH_CREATION operations: "<<m_operations[OPERATION_NO_VIRTUAL_COLOR_HAS_HASH_CREATION]<<endl;
(*out)<<" OPERATION_VIRTUAL_COLOR_HAS_COLORS_FETCH operations: "<<m_operations[OPERATION_VIRTUAL_COLOR_HAS_COLORS_FETCH]<<endl;
(*out)<<" OPERATION_NO_VIRTUAL_COLOR_HAS_COLORS_CREATION operations: "<<m_operations[OPERATION_NO_VIRTUAL_COLOR_HAS_COLORS_CREATION]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_createVirtualColorFrom operations: "<<m_operations[OPERATION_createVirtualColorFrom]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_allocateVirtualColorHandle operations: "<<m_operations[OPERATION_allocateVirtualColorHandle]<<endl;
(*out)<<" OPERATION_NEW_FROM_EMPTY operations: "<<m_operations[OPERATION_NEW_FROM_EMPTY]<<endl;
(*out)<<" OPERATION_NEW_FROM_SCRATCH operations: "<<m_operations[OPERATION_NEW_FROM_SCRATCH]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_applyHashOperation operations: "<<m_operations[OPERATION_applyHashOperation]<<endl;
(*out)<<" OPERATION_getHash operations: "<<m_operations[OPERATION_getHash]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_incrementReferences operations: "<<m_operations[OPERATION_incrementReferences]<<endl;
(*out)<<" OPERATION_decrementReferences operations: "<<m_operations[OPERATION_decrementReferences]<<endl;
(*out)<<endl;
(*out)<<" OPERATION_purgeVirtualColor operations: "<<m_operations[OPERATION_purgeVirtualColor]<<endl;
(*out)<<"**********************************************************"<<endl;
(*out)<<endl;
if(!xml){
return;
}
// print frequencies
map<CoverageDepth,LargeCount> referenceFrequencies;
map<CoverageDepth,LargeCount> colorFrequencies;
for(int i=0;i< (int)getTotalNumberOfVirtualColors();i++){
LargeCount references=getVirtualColor(i)->getNumberOfReferences();
referenceFrequencies[references]++;
set<PhysicalKmerColor>*colors=getVirtualColor(i)->getPhysicalColors();
colorFrequencies[colors->size()]++;
}
(*out)<<endl;
(*out)<<"<referencesPerVirtualColor><frequencies>"<<endl;
for(map<CoverageDepth,LargeCount>::iterator i=referenceFrequencies.begin();i!=referenceFrequencies.end();
i++){
(*out)<<i->first<<" "<<i->second<<endl;
}
(*out)<<"</frequencies></referencesPerVirtualColor>"<<endl;
(*out)<<"<physicalColorsPerVirtualColor><frequencies>"<<endl;
for(map<CoverageDepth,LargeCount>::iterator i=colorFrequencies.begin();i!=colorFrequencies.end();
i++){
(*out)<<i->first<<" "<<i->second<<endl;
}
(*out)<<"</frequencies></physicalColorsPerVirtualColor>"<<endl;
}
void ColorSet::printColors(ostream*out){
(*out)<<"Coloring summary"<<endl;
(*out)<<" Number of virtual colors: "<<getTotalNumberOfVirtualColors()<<endl;
(*out)<<" Number of real colors: "<<getTotalNumberOfPhysicalColors()<<endl;
(*out)<<endl;
for(int i=0;i< (int)getTotalNumberOfVirtualColors();i++){
(*out)<<"Virtual color: "<<i<<endl;
LargeCount references=getVirtualColor(i)->getNumberOfReferences();
(*out)<<" References: "<<references<<endl;
set<PhysicalKmerColor>*colors=getVirtualColor(i)->getPhysicalColors();
(*out)<<" Number of physical colors: "<<colors->size()<<endl;
(*out)<<" Physical colors: "<<endl;
(*out)<<" ";
for(set<PhysicalKmerColor>::iterator j=colors->begin();j!=colors->end();j++){
(*out)<<" "<<*j;
}
(*out)<<endl;
if(colors->size()>0)
(*out)<<endl;
}
}
/** O(1) **/
VirtualKmerColorHandle ColorSet::allocateVirtualColorHandle(){
// if there is a virtual color with 0 reference,
// use it
m_operations[OPERATION_allocateVirtualColorHandle]++;
if(m_availableHandles.size()>0){
VirtualKmerColorHandle handle= *(m_availableHandles.begin());
// we don't remove it here from the available list.
// instead, it will be removed from the avaialble list
// when its references are > 0
#ifdef CONFIG_ASSERT
assert(getVirtualColor(handle)->getNumberOfReferences()==0);
assert(getVirtualColor(handle)->getPhysicalColors()->size()==0);
#endif
// re-use a virtual color
m_operations[OPERATION_NEW_FROM_EMPTY]++;
return handle;
}
#ifdef CONFIG_ASSERT
assert(m_availableHandles.size()==0);
#endif
// otherwise, create a new one
VirtualKmerColorHandle handle=createVirtualColorHandleFromScratch();
// add it in the available list since it has 0 references
m_availableHandles.insert(handle);
m_operations[OPERATION_NEW_FROM_SCRATCH]++;
#ifdef CONFIG_ASSERT
assert(m_availableHandles.size()==1);
#endif
return handle;
}
/** O(16) **/
uint64_t ColorSet::getHash(set<PhysicalKmerColor>*colors){
uint64_t hashValue=0;
for(set<PhysicalKmerColor>::iterator i=colors->begin();i!=colors->end();i++){
PhysicalKmerColor color=*i;
hashValue=applyHashOperation(hashValue,color);
}
m_operations[OPERATION_getHash]++;
return hashValue;
}
uint64_t ColorSet::applyHashOperation(uint64_t hashValue,PhysicalKmerColor color){
uint64_t localHash=1;
int operations=10;
while(operations--){
localHash*=color;
}
hashValue += localHash;
m_operations[OPERATION_applyHashOperation]++;
return hashValue;
}
LargeCount ColorSet::getNumberOfReferences(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
assert(handle< getTotalNumberOfVirtualColors());
#endif
return getVirtualColor(handle)->getNumberOfReferences();
}
int ColorSet::getNumberOfPhysicalColors(VirtualKmerColorHandle handle){
#ifdef CONFIG_ASSERT
assert(handle< getTotalNumberOfVirtualColors());
#endif
return getVirtualColor(handle)->getNumberOfPhysicalColors();
}
VirtualKmerColorHandle ColorSet::getVirtualColorFrom(VirtualKmerColorHandle handle,PhysicalKmerColor color){
#ifdef CONFIG_ASSERT
assert(handle < getTotalNumberOfVirtualColors());
assert(!getVirtualColor(handle)->hasPhysicalColor(color));
// the handle may be available if the number of references
// was decremented before the call to getVirtualColorFrom
//
// on second thought, it is better to decrement after
// because all this code is designed like this
//
assert(m_availableHandles.count(handle)==0);
#endif
m_operations[OPERATION_getVirtualColorFrom]++;
m_physicalColors.insert(color);
VirtualKmerColor*oldVirtualColor=getVirtualColor(handle);
// check if any of them have the correct hash
uint64_t oldHash=oldVirtualColor->getCachedHashValue();
// in any case, we have to compute the hash from scratch
// because the new color has to be inserted in
// a sorted se
// 2012-02-01: not anymore, captain !
// the sum of a group of numbers does not depend on their order...
uint64_t expectedHash=applyHashOperation(oldHash,color);
// case 3. no virtual color has the expected hash value
if(m_index.count(expectedHash) == 0){
// case X.: the virtual color has only one reference
// this reference is the one provided in input
// to the current call.
// in that case, we can just add the color to it and
// update its hash...
// but before doing that, we need to check that no one has the expectedHash
// I am not sure this is a valid approach.
// anyway, this will never happen because
// handles are purged when they reach 0 references
if(oldVirtualColor->getNumberOfReferences() == 1){
// we can update it, no problem
// because nobody is using it
// it is the copy-on-write design pattern I guess
removeVirtualColorFromIndex(handle);
oldVirtualColor->addPhysicalColor(color);
oldVirtualColor->setHash(expectedHash);
addVirtualColorToIndex(handle);
m_operations[OPERATION_IN_PLACE_ONE_REFERENCE]++;
return handle;
}
// at this point
// at least one virtual color has the color and the correct number
// of colors
// however, none of them have a matching hash
//cout<<" No one has has value= "<<expectedHash<<endl;
m_operations[OPERATION_NO_VIRTUAL_COLOR_HAS_HASH_CREATION]++;
#ifdef CONFIG_ASSERT_LOW_LEVEL
assertNoVirtualColorDuplicates(handle,color,433);
#endif /* ASSERT_LOW_LEVEL */
return createVirtualColorFrom(handle,color);
}
#ifdef CONFIG_ASSERT
assert(m_index.count(expectedHash)>0);
assert(m_index[expectedHash].size()>=1);
#endif /* ASSERT */
// for each of the hits
// check if they have all the required colors
// if so, return it
set<VirtualKmerColorHandle>*hits=&(m_index[expectedHash]);
//cout<<"hits "<<hits->size()<<endl;
// case 4. a virtual color has:
// (1) the color,
// (2) the correct number of physical colors,
// (3) the expected hash value
//
// check it out
for(set<VirtualKmerColorHandle>::iterator i=hits->begin();i!=hits->end();i++){
VirtualKmerColorHandle virtualColorToInvestigate=*i;
VirtualKmerColor*toCheck=getVirtualColor(virtualColorToInvestigate);
//cout<<"Checking virtual color "<<virtualColorToInvestigate<<endl;
if(toCheck->virtualColorHasAllPhysicalColorsOf(oldVirtualColor,color)){
#ifdef CONFIG_ASSERT
assert(virtualColorHasPhysicalColor(virtualColorToInvestigate,color));
#endif /* ASSERT */
m_operations[OPERATION_VIRTUAL_COLOR_HAS_COLORS_FETCH]++;
return virtualColorToInvestigate;
}
}
// at this point, we know for sure that no virtual color matches
// case 5. no virtual color has all the required colors
m_operations[OPERATION_NO_VIRTUAL_COLOR_HAS_COLORS_CREATION]++;
#ifdef CONFIG_ASSERT_LOW_LEVEL
assertNoVirtualColorDuplicates(handle,color,479);
#endif
return createVirtualColorFrom(handle,color);
}
VirtualKmerColorHandle ColorSet::createVirtualColorFrom(VirtualKmerColorHandle handle,PhysicalKmerColor color){
VirtualKmerColorHandle newHandle=allocateVirtualColorHandle();
// consume the handle
m_availableHandles.erase(newHandle);
#ifdef CONFIG_ASSERT
assert(!getVirtualColor(handle)->hasPhysicalColor(color));
assert(m_availableHandles.count(newHandle)==0);
assert(getNumberOfReferences(newHandle)==0);
assert(getNumberOfPhysicalColors(newHandle)==0);
#endif /* ASSERT */
// add the colors and return it
VirtualKmerColor*newVirtualColor=getVirtualColor(newHandle);
newVirtualColor->copyPhysicalColors(getVirtualColor(handle));
newVirtualColor->addPhysicalColor(color);
VirtualKmerColor*oldVirtualColor=getVirtualColor(handle);
uint64_t oldHash=oldVirtualColor->getCachedHashValue();
uint64_t hashValue=applyHashOperation(oldHash,color);
newVirtualColor->setHash(hashValue);
addVirtualColorToIndex(newHandle);
m_operations[OPERATION_createVirtualColorFrom]++;
#ifdef CONFIG_ASSERT
assert(getVirtualColor(newHandle)->getNumberOfPhysicalColors()==getVirtualColor(handle)->getNumberOfPhysicalColors()+1);
assert(getVirtualColor(newHandle)->getNumberOfReferences()==0);
assert(getVirtualColor(newHandle)->virtualColorHasAllPhysicalColorsOf(oldVirtualColor,color));
#endif
return newHandle;
}
set<PhysicalKmerColor>*ColorSet::getPhysicalColors(VirtualKmerColorHandle handle){
return getVirtualColor(handle)->getPhysicalColors();
}
bool ColorSet::virtualColorHasPhysicalColor(VirtualKmerColorHandle handle,PhysicalKmerColor color){
return getVirtualColor(handle)->hasPhysicalColor(color);
}
void ColorSet::removeVirtualColorFromIndex(VirtualKmerColorHandle handle){
VirtualKmerColor*virtualColor=getVirtualColor(handle);
uint64_t hashValue=virtualColor->getCachedHashValue();
m_index[hashValue].erase(handle);
if(m_index[hashValue].size()==0)
m_index.erase(hashValue);
}
void ColorSet::addVirtualColorToIndex(VirtualKmerColorHandle handle){
VirtualKmerColor*virtualColor=getVirtualColor(handle);
uint64_t hashValue=virtualColor->getCachedHashValue();
m_index[hashValue].insert(handle);
}
VirtualKmerColorHandle ColorSet::createVirtualColorHandleFromScratch(){
VirtualKmerColor a;
m_virtualColors.push_back(a);
VirtualKmerColorHandle handle=getTotalNumberOfVirtualColors()-1;
getVirtualColor(handle)->clear();
return handle;
}
void ColorSet::assertNoVirtualColorDuplicates(VirtualKmerColorHandle handle,PhysicalKmerColor color,int caseX){
set<PhysicalKmerColor> desiredColors;
set<PhysicalKmerColor>*colors3=getVirtualColor(handle)->getPhysicalColors();
for(set<PhysicalKmerColor>::iterator i=colors3->begin();i!=colors3->end();i++){
desiredColors.insert(*i);
}
desiredColors.insert(color);
for(int i=0;i<(int)getTotalNumberOfVirtualColors();i++){
if(getVirtualColor(i)->getNumberOfPhysicalColors()==(int)desiredColors.size()){
if(getVirtualColor(i)->hasPhysicalColors(&desiredColors)){
cout<<"Error, there is a virtual color that does the job already, case= "<<caseX<<endl;
cout<<"virtual color "<<i<<" has "<<getVirtualColor(i)->getNumberOfPhysicalColors()<<" physical colors";
cout<<" and "<<getVirtualColor(i)->getNumberOfReferences()<<" references"<<endl;
printPhysicalColors(getVirtualColor(i)->getPhysicalColors());
cout<<"Searched for "<<getVirtualColor(handle)->getNumberOfPhysicalColors()+1<<" physical colors,"<<endl;
printPhysicalColors(&desiredColors);
cout<<"previous virtual color was ";
cout<<handle<<" with exactly "<<getVirtualColor(handle)->getNumberOfPhysicalColors()<<" physical colors"<<endl;
printPhysicalColors(getVirtualColor(handle)->getPhysicalColors());
}
#ifdef CONFIG_ASSERT
assert(!getVirtualColor(i)->hasPhysicalColors(&desiredColors));
#endif /* ASSERT */
}
}
}
void ColorSet::printPhysicalColors(set<PhysicalKmerColor>*colors3){
for(set<PhysicalKmerColor>::iterator i=colors3->begin();i!=colors3->end();i++){
cout<<" "<<*i;
}
cout<<endl;
}
VirtualKmerColorHandle ColorSet::findVirtualColor(set<PhysicalKmerColor>*colors){
if(colors->size()==0)
return NULL_VIRTUAL_COLOR;
VirtualKmerColorHandle virtualColorInStore=lookupVirtualColor(colors);
if(virtualColorInStore!=NULL_VIRTUAL_COLOR)
return virtualColorInStore;
/*
* At this point, we need to create a virtual color with all the required
* physical colors.
*/
return createVirtualColorFromPhysicalColors(colors);
}
VirtualKmerColorHandle ColorSet::createVirtualColorFromPhysicalColors(set<PhysicalKmerColor>*colors){
if(colors->size()==0)
return NULL_VIRTUAL_COLOR;
#ifdef CONFIG_ASSERT
assert(colors->size()!=0);
#endif /* ASSERT */
VirtualKmerColorHandle newHandle=allocateVirtualColorHandle();
#ifdef CONFIG_ASSERT
assert(m_availableHandles.count(newHandle)>0);
#endif /* ASSERT */
m_availableHandles.erase(newHandle);
VirtualKmerColor*newVirtualColor=getVirtualColor(newHandle);
newVirtualColor->addPhysicalColors(colors);
uint64_t expectedHash=getHash(colors);
newVirtualColor->setHash(expectedHash);
addVirtualColorToIndex(newHandle);
#ifdef CONFIG_ASSERT
assert(lookupVirtualColor(colors)!=NULL_VIRTUAL_COLOR);
#endif /* ASSERT */
return newHandle;
}
VirtualKmerColorHandle ColorSet::lookupVirtualColor(set<PhysicalKmerColor>*colors){
uint64_t expectedHash=getHash(colors);
if(m_index.count(expectedHash)>0){
set<VirtualKmerColorHandle>*hits=&(m_index[expectedHash]);
for(set<VirtualKmerColorHandle>::iterator i=hits->begin();i!=hits->end();i++){
VirtualKmerColorHandle virtualColorToInvestigate=*i;
VirtualKmerColor*toCheck=getVirtualColor(virtualColorToInvestigate);
/*
* We need the same number of physical colors.
*/
if((int)colors->size()!=toCheck->getNumberOfPhysicalColors())
continue;
/*
* Each physical color must match
*/
if(!toCheck->hasPhysicalColors(colors))
continue;
/*
* The matching virtual color was found.
*/
return virtualColorToInvestigate;
}
}
return NULL_VIRTUAL_COLOR;
}
|