PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/SNAPLib/IntersectingAligner.cpp

https://github.com/terhorst/snap
C++ | 651 lines | 398 code | 81 blank | 172 comment | 104 complexity | a1d0209a19531ca4bd7e03fe19854b63 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*++
  2. Module Name:
  3. IntersectingAligner.cpp
  4. Abstract:
  5. SNAP genome aligner
  6. Authors:
  7. Bill Bolosky, September, 2011
  8. Environment:
  9. User mode service.
  10. This class is NOT thread safe. It's the caller's responsibility to ensure that
  11. at most one thread uses an instance at any time.
  12. Revision History:
  13. Adapted from Matei Zaharia's Scala implementation.
  14. --*/
  15. #include "stdafx.h"
  16. #include "Aligner.h"
  17. #include "IntersectingAligner.h"
  18. #include "LandauVishkin.h"
  19. using std::sort;
  20. IntersectingAligner::IntersectingAligner(
  21. GenomeIndex *i_genomeIndex,
  22. unsigned i_confDiff,
  23. unsigned i_maxHitsToConsider,
  24. unsigned i_maxK,
  25. unsigned i_maxReadSize,
  26. unsigned i_maxSeedsToUse,
  27. unsigned i_lvCutoff) :
  28. genomeIndex(i_genomeIndex), confDiff((int)i_confDiff), maxHitsToConsider(i_maxHitsToConsider), maxK((int)i_maxK),
  29. maxReadSize((int)i_maxReadSize), maxSeedsToUse((int)i_maxSeedsToUse), lvCutoff(i_lvCutoff)
  30. /*++
  31. Routine Description:
  32. Constructor for the Aligner class. Aligners align reads against an indexed genome.
  33. Arguments:
  34. i_genomeIndex - The index against which to do the alignments
  35. i_confDiff - The string difference between two matches necessary to believe they're really different
  36. i_maxHitsToConsider - The maximum number of hits to use from a seed lookup. Any lookups that return more
  37. than this are ignored.
  38. i_maxK - The largest string difference to consider for any comparison.
  39. i_maxReadSize - Bound on the number of bases in any read. There's no reason to make it tight, it just affects a little memory allocation.
  40. i_maxSeedsToUse - The maximum number of seeds to use when aligning any read (not counting ones ignored because they resulted in too many
  41. hits). Once we've looked up this many seeds, we just score what we've got.
  42. --*/
  43. {
  44. #if MAINTAIN_HISTOGRAMS
  45. lvHistogram = new Histogram(20, true);
  46. lookupHistogram = new Histogram(20, true);
  47. lvHistogramForMulti = new Histogram(20, true);
  48. lvCountWhenBestFound = new Histogram(maxHitsToConsider*4,false);
  49. #endif // MAINTAIN_HISTOGRAMS
  50. nHashTableLookups = 0;
  51. nLocationsScored = 0;
  52. nHitsIgnoredBecauseOfTooHighPopularity = 0;
  53. nReadsIgnoredBecauseOfTooManyNs = 0;
  54. nIndelsMerged = 0;
  55. lookupResults = new LookupResult[maxSeedsToUse];
  56. rcLookupResults = new LookupResult[maxSeedsToUse];
  57. intersectionResultsSpaceSize = maxHitsToConsider * maxSeedsToUse * 2; // *2 is for RC
  58. intersectionResultSpace = new unsigned[intersectionResultsSpaceSize];
  59. intersectionResults = new IntersectionResult[(__max(maxK,maxSeedsToUse)+1) * 2]; // +1 is for 0 misses
  60. rcIntersectionResults = new IntersectionResult[maxSeedsToUse * 2]; // again, *2 is for RC
  61. intersectionOffsets = new int[maxSeedsToUse];
  62. alreadyIntersectedOffsets = new int[maxSeedsToUse];
  63. if (NULL == lookupResults || NULL == intersectionResultSpace || NULL == intersectionResults ||
  64. NULL == rcLookupResults || NULL == rcIntersectionResults || NULL == intersectionOffsets) {
  65. fprintf(stderr,"IntersectingAligner: allocation failed in constructor\n");
  66. exit(1);
  67. }
  68. //
  69. // Initialize the isRC fields of the lookup results, since they never change.
  70. //
  71. for (int i = 0; i < maxSeedsToUse; i++) {
  72. lookupResults[i].isRC = false;
  73. rcLookupResults[i].isRC = true;
  74. }
  75. genome = genomeIndex->getGenome();
  76. seedLen = genomeIndex->getSeedLength();
  77. rcReadData = new char[maxReadSize];
  78. if (NULL == rcReadData) {
  79. fprintf(stderr,"IntersectingAligner: unable to allocate rc read data.");
  80. exit(1);
  81. }
  82. rcTranslationTable['A'] = 'T';
  83. rcTranslationTable['G'] = 'C';
  84. rcTranslationTable['C'] = 'G';
  85. rcTranslationTable['T'] = 'A';
  86. rcTranslationTable['N'] = 'N';
  87. for (unsigned i = 0; i < 256; i++) {
  88. nTable[i] = 0;
  89. }
  90. nTable['N'] = 1;
  91. seedUsed = new BYTE[(maxReadSize + 7 + 128) / 8]; // +128 to make sure it extends at both before and after at least an _int64
  92. if (NULL == seedUsed) {
  93. fprintf(stderr,"IntersectingAligner: unable to allocate seedUsed array.\n");
  94. exit(1);
  95. }
  96. seedUsedAsAllocated = seedUsed; // Save the pointer for the delete.
  97. seedUsed += 8; // This moves the pointer up an _int64, so we now have the appropriate before buffer.
  98. }
  99. AlignmentResult
  100. IntersectingAligner::AlignRead(Read *read, unsigned *genomeLocation, bool *hitIsRC, int *finalScore)
  101. /*++
  102. Routine Description:
  103. Align a particular read.
  104. Arguments:
  105. read - the read to align
  106. genomeLocation - if this aligned to a SingleHit, the 0-based offset in the genome that this hit. The aligner treats the entire
  107. genome as a single string, even though it's really a set of chrosomes. It just makes the code simpler.
  108. The caller can convert to chromosome+offset by looking up the piece boudaries using the Genome object.
  109. hitIsRC - the aligner tries to align both the given read and its reverse compliment. If we found a SIngleHit this
  110. bool is set to indicate whether that hit was on the reverse compliment.
  111. Return Value:
  112. SingleHit, MultiHit or NotFound depending on how the alignment went.
  113. --*/
  114. {
  115. unsigned lookupsThisRun = 0;
  116. unsigned lvScores = 0;
  117. unsigned lvScoresAfterBestFound = 0;
  118. bool truncatedIntersection = false;
  119. //
  120. // A bitvector for used seeds, indexed on the starting location of the seed within the read.
  121. //
  122. if ((int)read->getDataLength() > maxReadSize) {
  123. fprintf(stderr,"IntersectingAligner:: got too big read (%d > %d)", read->getDataLength(),maxReadSize);
  124. exit(1);
  125. }
  126. if (read->getDataLength() < seedLen) {
  127. //
  128. // Too short to have any seeds, it's hopeless.
  129. //
  130. return NotFound;
  131. }
  132. //
  133. // Clear out the seed used array.
  134. //
  135. memset(seedUsed,0,(read->getDataLength() + 7) / 8);
  136. int rdl = (int)read->getDataLength();
  137. const char *readData = read->getData();
  138. int countOfNs = 0;
  139. for (int i = 0; i < rdl; i++) {
  140. char baseByte = readData[i];
  141. rcReadData[rdl - i - 1] = rcTranslationTable[baseByte];
  142. countOfNs += nTable[baseByte];
  143. }
  144. if (countOfNs > maxK) {
  145. nReadsIgnoredBecauseOfTooManyNs++;
  146. return NotFound;
  147. }
  148. //
  149. // Block off any seeds that would contain an N.
  150. //
  151. if (countOfNs > 0) {
  152. int minSeedToConsiderNing = 0; // In English, any word can be verbed. Including, apparently, "N."
  153. for (int i = 0; i < rdl; i++) {
  154. if (readData[i] == 'N') {
  155. int limit = __max(i + (int)seedLen - 1, rdl-1);
  156. for (int j = __max(minSeedToConsiderNing, i - (int)seedLen + 1); j <= limit; j++) {
  157. SetSeedUsed(j);
  158. }
  159. minSeedToConsiderNing = limit+1;
  160. }
  161. }
  162. }
  163. Read rcRead[1];
  164. rcRead->init(NULL,0,rcReadData,NULL,read->getDataLength());
  165. nLookupResults = 0;
  166. nRCLookupResults = 0;
  167. usedEntriesInIntersectionResultSpace = 0;
  168. nIntersectionResults = 0;
  169. nRCIntersectionResults = 0;
  170. //
  171. // Initialize the bases table, which represents which bases we've checked.
  172. // We have readSize - seeds size + 1 possible seeds.
  173. //
  174. unsigned nPossibleSeeds = read->getDataLength() - seedLen + 1;
  175. unsigned nextSeedToTest = 0;
  176. unsigned wrapCount = 0;
  177. unsigned mostSeedsContainingAnyParticularBase = 1; // Instead of tracking this for real, we're just conservative and use wrapCount+1. It's faster.
  178. unsigned mostRCSeedsContainingAnyParticularBase = 1;// ditto
  179. int nSeedsApplied = 0;
  180. int nRCSeedsApplied = 0;
  181. while (nSeedsApplied + nRCSeedsApplied < maxSeedsToUse) {
  182. //
  183. // Choose the next seed to use. Choose the first one that isn't used
  184. //
  185. if (nextSeedToTest >= nPossibleSeeds) {
  186. //
  187. // We're wrapping. We want to space the seeds out as much as possible, so if we had
  188. // a seed length of 20 we'd want to take 0, 10, 5, 15, 2, 7, 12, 17. To make the computation
  189. // fast, we use use a table lookup.
  190. //
  191. wrapCount++;
  192. if (wrapCount >= seedLen) {
  193. break;
  194. }
  195. nextSeedToTest = getWrappedNextSeedToTest(wrapCount);
  196. mostSeedsContainingAnyParticularBase = wrapCount + 1;
  197. mostRCSeedsContainingAnyParticularBase = wrapCount + 1;
  198. }
  199. while (nextSeedToTest < nPossibleSeeds && IsSeedUsed(nextSeedToTest)) {
  200. //
  201. // This seed is already used. Try the next one.
  202. //
  203. nextSeedToTest++;
  204. }
  205. if (nextSeedToTest >= nPossibleSeeds) {
  206. //
  207. // Unusable seeds have pushed us past the end of the read. Go back around the outer loop so we wrap properly.
  208. //
  209. continue;
  210. }
  211. if (wrapCount > 0) {
  212. //
  213. // Just finish, since all we're doing is looking at the set size.
  214. //
  215. break;
  216. }
  217. Seed seed(read->getData() + nextSeedToTest, seedLen);
  218. unsigned nHits; // Number of times this seed hits in the genome
  219. const unsigned *hits; // The actual hits (of size nHits)
  220. unsigned nRCHits; // Number of hits for the seed's reverse compliment
  221. const unsigned *rcHits; // The actual reverse compliment hits
  222. genomeIndex->lookupSeed(seed, &nHits, &hits, &nRCHits, &rcHits);
  223. nHashTableLookups++;
  224. lookupsThisRun++;
  225. SetSeedUsed(nextSeedToTest);
  226. bool appliedEitherSeed = false;
  227. if (nHits < maxHitsToConsider) {
  228. _ASSERT(nLookupResults < maxSeedsToUse);
  229. LookupResult *result = &lookupResults[nLookupResults];
  230. lookupResults[nLookupResults].nHits = nHits;
  231. lookupResults[nLookupResults].hits = hits;
  232. lookupResults[nLookupResults].offset = nextSeedToTest;
  233. appliedEitherSeed = true;
  234. nLookupResults++;
  235. }
  236. if (nRCHits < maxHitsToConsider) {
  237. _ASSERT(nRCLookupResults < maxSeedsToUse);
  238. LookupResult *result = &rcLookupResults[nRCLookupResults];
  239. rcLookupResults[nRCLookupResults].nHits = nRCHits;
  240. rcLookupResults[nRCLookupResults].hits = rcHits;
  241. rcLookupResults[nRCLookupResults].offset = read->getDataLength() - seedLen - nextSeedToTest;
  242. appliedEitherSeed = true;
  243. nRCLookupResults++;
  244. }
  245. //
  246. // Move us along.
  247. //
  248. nextSeedToTest += seedLen;
  249. }
  250. unsigned nIntersections = 0;
  251. unsigned nRCIntersections = 0;
  252. if (nLookupResults > 0) {
  253. nIntersections = __min(nLookupResults-1,(int)maxIntersections);
  254. computeIntersection(NULL,0,0,nIntersections,lookupResults,nLookupResults,intersectionResults,&truncatedIntersection);
  255. }
  256. if (nRCLookupResults > 0) {
  257. nRCIntersections = __min(nRCLookupResults-1,(int)maxIntersections);
  258. computeIntersection(NULL,0,0,nRCIntersections,rcLookupResults,nRCLookupResults,rcIntersectionResults,&truncatedIntersection);
  259. }
  260. //
  261. // Now score what we've got to find the best match.
  262. //
  263. int bestScore = maxK+1;
  264. int secondBestScore = maxK+1;
  265. bool bestScoreIsRC;
  266. unsigned bestScoreGenomeLocation;
  267. for (unsigned i = 0; i < __max(nIntersections,nRCIntersections); i++) {
  268. //
  269. // See if we can eliminate the need to score the next set of matches. We know that their
  270. // best possible score is nMisses, so if we've got a score that's less than nMisses + confDiff
  271. // we can quit now.
  272. //
  273. if (bestScore + confDiff <= intersectionResults[i].nMisses) {
  274. if (bestScore + confDiff < secondBestScore) {
  275. *genomeLocation = bestScoreGenomeLocation;
  276. *hitIsRC = bestScoreIsRC;
  277. if (NULL != finalScore) {
  278. *finalScore = bestScore;
  279. }
  280. if (truncatedIntersection) {
  281. return SingleHit;
  282. } else {
  283. return CertainHit;
  284. }
  285. }
  286. return MultipleHits;
  287. }
  288. for (unsigned j = 0; j < intersectionResults[i].nHits; j++) {
  289. int score = landauVishkin.computeEditDistance(
  290. genome->getSubstring(intersectionResults[i].hits[j],read->getDataLength()),
  291. read->getDataLength(),
  292. read->getData(),
  293. read->getDataLength(),
  294. __min(maxK,bestScore+confDiff));
  295. nLocationsScored++;
  296. lvScores++;
  297. lvScoresAfterBestFound++;
  298. if (score > -1) {
  299. if (score < bestScore) {
  300. secondBestScore = bestScore;
  301. bestScore = score;
  302. bestScoreIsRC = false;
  303. bestScoreGenomeLocation = intersectionResults[i].hits[j];
  304. if (secondBestScore <= confDiff) {
  305. return MultipleHits;
  306. }
  307. lvScoresAfterBestFound = 0;
  308. } else if (score < secondBestScore) {
  309. secondBestScore = score;
  310. if (secondBestScore <= confDiff) {
  311. return MultipleHits;
  312. }
  313. }
  314. }
  315. } // For each hit
  316. for (unsigned j = 0; j < rcIntersectionResults[i].nHits; j++) {
  317. int score = landauVishkin.computeEditDistance(
  318. genome->getSubstring(rcIntersectionResults[i].hits[j],rcRead->getDataLength()),
  319. rcRead->getDataLength(),
  320. rcRead->getData(),
  321. rcRead->getDataLength(),
  322. __min(maxK,bestScore+confDiff));
  323. nLocationsScored++;
  324. lvScores++;
  325. lvScoresAfterBestFound++;
  326. if (score > -1) {
  327. if (score < bestScore) {
  328. secondBestScore = bestScore;
  329. bestScore = score;
  330. bestScoreIsRC = true;
  331. bestScoreGenomeLocation = rcIntersectionResults[i].hits[j];
  332. if (secondBestScore <= confDiff) {
  333. return MultipleHits;
  334. }
  335. lvScoresAfterBestFound = 0;
  336. } else if (score < secondBestScore) {
  337. secondBestScore = score;
  338. if (secondBestScore <= confDiff) {
  339. return MultipleHits;
  340. }
  341. }
  342. }
  343. } // For each RC hit
  344. }
  345. if (bestScore >= maxK) {
  346. return NotFound;
  347. } else if (bestScore + confDiff <= secondBestScore) {
  348. *genomeLocation = bestScoreGenomeLocation;
  349. *hitIsRC = bestScoreIsRC;
  350. if (NULL != finalScore) {
  351. *finalScore = bestScore;
  352. }
  353. return SingleHit;
  354. }
  355. return MultipleHits;
  356. }
  357. IntersectingAligner::~IntersectingAligner()
  358. /*++
  359. Routine Description:
  360. Arguments:
  361. Return Value:
  362. --*/
  363. {
  364. #if MAINTAIN_HISTOGRAMS
  365. delete lvHistogram;
  366. delete lookupHistogram;
  367. delete lvHistogramForMulti;
  368. delete lvCountWhenBestFound;
  369. #endif // MAINTAIN_HISTOGRAMS
  370. delete [] seedUsedAsAllocated;
  371. delete [] rcReadData;
  372. }
  373. bool
  374. LookupResultComparitor(const IntersectingAligner::LookupResult& result1, const IntersectingAligner::LookupResult& result2)
  375. {
  376. return result1.nHits < result2.nHits;
  377. }
  378. void
  379. IntersectingAligner::computeIntersection(
  380. IntersectionResult *alreadyIntersected,
  381. int nAlreadyIntersected,
  382. int totalSetsRepresentedByAlreadyIntersected,
  383. int maxMisses,
  384. LookupResult *lookups,
  385. int nLookups,
  386. IntersectionResult *results,
  387. bool *truncatedIntersection
  388. )
  389. {
  390. _ASSERT(nLookups >= 1);
  391. _ASSERT(maxMisses < nLookups);
  392. //
  393. // Start by sorting the input (lookups) array.
  394. //
  395. sort(lookups, lookups+nLookups, LookupResultComparitor);
  396. //
  397. // Initialize the results arrays.
  398. //
  399. _ASSERT(maxMisses <= __max(maxK,maxSeedsToUse));
  400. for (int i = 0; i <= maxMisses; i++) { // Note: <= maxMisses results in initializing maxMisses+1 results (0 .. maxMisses inclusive)
  401. results[i].nMisses = i;
  402. results[i].nHits = 0;
  403. results[i].isRC = lookups[0].isRC;
  404. results[i].hits = intersectionResultSpace + usedEntriesInIntersectionResultSpace;
  405. usedEntriesInIntersectionResultSpace += maxHitsToConsider; // This is overkill, but sufficient
  406. _ASSERT(usedEntriesInIntersectionResultSpace <= intersectionResultsSpaceSize);
  407. }
  408. //
  409. // Initialize the working offsets into the sets to be intersected.
  410. //
  411. memset(intersectionOffsets,0,sizeof(*intersectionOffsets) * nLookups);
  412. memset(alreadyIntersectedOffsets,0,sizeof(*alreadyIntersectedOffsets) * nAlreadyIntersected);
  413. //
  414. // Run the intersection. At each step, find the largest of the entries among the
  415. // maxMisses - totalSetsRepresentedByAlreadyIntersected smallest sets and the already intersected sets,
  416. // and then look for that in all of the sets.
  417. // (Recall that the sets are all sorted from largest to smallest, so we find things backwards.)
  418. //
  419. for (;;) {
  420. unsigned valueToFind = 0;
  421. int setContainingValueToFind = maxMisses+1;
  422. int alreadyIntersectedContainingValueToFind = nAlreadyIntersected+1;
  423. //
  424. // Check the maxMisses
  425. for (int i = 0; i <= maxMisses - totalSetsRepresentedByAlreadyIntersected; i++) {
  426. //
  427. // Assert that they're in (descending) order.
  428. //
  429. _ASSERT(intersectionOffsets[i]+1 >= (int)lookups[i].nHits || lookups[i].hits[intersectionOffsets[i]] > lookups[i].hits[intersectionOffsets[i]+1]);
  430. if (intersectionOffsets[i] < (int)lookups[i].nHits && lookups[i].hits[intersectionOffsets[i]] - lookups[i].offset >= valueToFind) {
  431. valueToFind = lookups[i].hits[intersectionOffsets[i]] - lookups[i].offset;
  432. setContainingValueToFind = i;
  433. }
  434. }
  435. for (int i = 0; i < nAlreadyIntersected; i++) {
  436. //
  437. // Assert that they're in (descending) order.
  438. //
  439. _ASSERT(alreadyIntersectedOffsets[i]+1 >= (int)alreadyIntersected[i].nHits ||
  440. alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]] > alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]+1]);
  441. //
  442. // Assert that the alreadyIntersected sets are disjoint (or at least that valueToFind is not in both this one and
  443. // some other one).
  444. //
  445. _ASSERT(alreadyIntersectedOffsets[i] >= (int)alreadyIntersected[i].nHits ||
  446. alreadyIntersectedContainingValueToFind == maxMisses+1 ||
  447. valueToFind != alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]]);
  448. if (alreadyIntersectedOffsets[i] < (int)alreadyIntersected[i].nHits &&
  449. alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]] >= valueToFind) {
  450. if (valueToFind != alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]]) {
  451. setContainingValueToFind = maxMisses+1;
  452. }
  453. valueToFind = alreadyIntersected[i].hits[alreadyIntersectedOffsets[i]];
  454. alreadyIntersectedContainingValueToFind = i;
  455. }
  456. }
  457. if (setContainingValueToFind == maxMisses+1 && alreadyIntersectedContainingValueToFind == nAlreadyIntersected+1) {
  458. //
  459. // We've intersected them all, we're done.
  460. //
  461. return;
  462. }
  463. //
  464. // Count the misses. Do this in two stages, first for the maxMisses smallest sets (in which if there's
  465. // a hit it must be the next element to check because we selected the greatest of these to be
  466. // valueToFind). Then, run binary search on the remainder. We don't need to look through the
  467. // alreadyIntersected, because there's at most one of them containing valueToFind and we already
  468. // know which one it is.
  469. //
  470. int nMisses;
  471. if (alreadyIntersectedContainingValueToFind == nAlreadyIntersected+1) {
  472. //
  473. // It's not in alreadyIntersected, which means that it missed on all of the sets represented by alreadyIntersected.
  474. //
  475. nMisses = totalSetsRepresentedByAlreadyIntersected;
  476. } else {
  477. nMisses = alreadyIntersected[alreadyIntersectedContainingValueToFind].nMisses;
  478. }
  479. for (int i = 0; i <= maxMisses; i++) {
  480. if (intersectionOffsets[i] >= (int)lookups[i].nHits) {
  481. //
  482. // We've already checked this entire set. It's a miss.
  483. //
  484. nMisses++;
  485. continue;
  486. }
  487. _ASSERT(lookups[i].hits[intersectionOffsets[i]] - lookups[i].offset <= valueToFind); // Because we selected the largest one
  488. if (lookups[i].hits[intersectionOffsets[i]] - lookups[i].offset == valueToFind) {
  489. intersectionOffsets[i]++;
  490. } else {
  491. nMisses++;
  492. if (nMisses > maxMisses) {
  493. break;
  494. }
  495. }
  496. }
  497. _ASSERT(nMisses <= maxMisses); // It had to be in at least one set, that's why we selected it.
  498. for (int i = maxMisses+1; i < nLookups; i++) {
  499. LookupResult *result = &lookups[i];
  500. unsigned offsetValueToFind = valueToFind + result->offset;
  501. int min = intersectionOffsets[i];
  502. int max = result->nHits-1;
  503. int probe = min; // Need to init this because of the case where we've already read the whole thing and fall through the while loop
  504. while (min <= max) {
  505. probe = (min + max) / 2;
  506. if (result->hits[probe] > offsetValueToFind) {
  507. min = probe+1;
  508. } else if (result->hits[probe] < offsetValueToFind) {
  509. max = probe-1;
  510. } else {
  511. //
  512. // We have a hit.
  513. //
  514. break;
  515. }
  516. }
  517. if (min > max) {
  518. nMisses++;
  519. if (nMisses > maxMisses) {
  520. break;
  521. }
  522. }
  523. intersectionOffsets[i] = probe+1;
  524. } // checking the larger sets
  525. if (nMisses <= maxMisses) {
  526. //
  527. // Add the value to the appropriate intersection result set.
  528. //
  529. IntersectionResult *result = &results[nMisses];
  530. if (result->nHits >= maxHitsToConsider) {
  531. //
  532. // Too many hits in this one result. Truncate.
  533. //
  534. *truncatedIntersection = true;
  535. } else {
  536. result->hits[result->nHits] = valueToFind;
  537. _ASSERT(result->nHits == 0 || result->hits[result->nHits-1] > valueToFind); // Inserting in order
  538. result->nHits++;
  539. }
  540. }
  541. } // for ever
  542. }