| 650 | |
| 651 | template <class TDescriptor, class F> |
| 652 | void TemplatedDatabase<TDescriptor, F>::queryL2(const BowVector& vec, |
| 653 | QueryResults& ret, |
| 654 | int max_results, |
| 655 | int max_id) const { |
| 656 | BowVector::const_iterator vit; |
| 657 | typename IFRow::const_iterator rit; |
| 658 | |
| 659 | std::map<EntryId, double> pairs; |
| 660 | std::map<EntryId, double>::iterator pit; |
| 661 | |
| 662 | // map<EntryId, int> counters; |
| 663 | // map<EntryId, int>::iterator cit; |
| 664 | |
| 665 | for (vit = vec.begin(); vit != vec.end(); ++vit) { |
| 666 | const WordId word_id = vit->first; |
| 667 | const WordValue& qvalue = vit->second; |
| 668 | |
| 669 | const IFRow& row = m_ifile[word_id]; |
| 670 | |
| 671 | // IFRows are sorted in ascending entry_id order |
| 672 | |
| 673 | for (rit = row.begin(); rit != row.end(); ++rit) { |
| 674 | const EntryId entry_id = rit->entry_id; |
| 675 | const WordValue& dvalue = rit->word_weight; |
| 676 | |
| 677 | if ((int)entry_id < max_id || max_id == -1) { |
| 678 | double value = -qvalue * dvalue; // minus sign for sorting trick |
| 679 | |
| 680 | pit = pairs.lower_bound(entry_id); |
| 681 | // cit = counters.lower_bound(entry_id); |
| 682 | if (pit != pairs.end() && !(pairs.key_comp()(entry_id, pit->first))) { |
| 683 | pit->second += value; |
| 684 | // cit->second += 1; |
| 685 | } else { |
| 686 | pairs.insert(pit, std::map<EntryId, double>::value_type(entry_id, value)); |
| 687 | |
| 688 | // counters.insert(cit, |
| 689 | // map<EntryId, int>::value_type(entry_id, 1)); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | } // for each inverted row |
| 694 | } // for each query word |
| 695 | |
| 696 | // move to vector |
| 697 | ret.reserve(pairs.size()); |
| 698 | // cit = counters.begin(); |
| 699 | for (pit = pairs.begin(); pit != pairs.end(); ++pit) //, ++cit) |
| 700 | { |
| 701 | ret.push_back(Result(pit->first, pit->second)); // / cit->second)); |
| 702 | } |
| 703 | |
| 704 | // resulting "scores" are now in [-1 best .. 0 worst] |
| 705 | |
| 706 | // sort vector in ascending order of score |
| 707 | std::sort(ret.begin(), ret.end()); |
| 708 | // (ret is inverted now --the lower the better--) |
| 709 | |