Inserts the given data item into position @p pos, and updates the sorting @param data can be another empty item, that together with @p pos represents the closest enclosure of the target position @return Whether the item could be inserted. It is not inserted if
| 695 | ///@param data can be another empty item, that together with @p pos represents the closest enclosure of the target position |
| 696 | ///@return Whether the item could be inserted. It is not inserted if |
| 697 | bool insertSorted(const Data& data, int pos, int start, int end, bool force) |
| 698 | { |
| 699 | |
| 700 | if (pos < start) |
| 701 | start = pos; |
| 702 | if (pos >= end) |
| 703 | end = pos + 1; |
| 704 | |
| 705 | /* for(int a = start; a < end; ++a) { |
| 706 | if(a != pos) { |
| 707 | Q_ASSERT(!ItemHandler::isFree(m_items[a])); |
| 708 | } |
| 709 | }*/ |
| 710 | EmbeddedTreeAlgorithms<Data, ItemHandler> alg(m_items, m_itemCount, *m_centralFreeItem); |
| 711 | int bound = alg.lowerBound(data, start, end); |
| 712 | //Now find the position that should be taken |
| 713 | if (bound == -1) |
| 714 | bound = end; |
| 715 | |
| 716 | //Now our item should end up right before bound |
| 717 | |
| 718 | int target; |
| 719 | //bound cannot be pos, because pos is invalid |
| 720 | Q_ASSERT(bound != pos); |
| 721 | |
| 722 | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 800) |
| 723 | #pragma GCC diagnostic push |
| 724 | #pragma GCC diagnostic ignored "-Wclass-memaccess" |
| 725 | #endif |
| 726 | //Shuffle around the item at the free pos, so reference counting in constructors/destructors is not screwed up |
| 727 | char backup[sizeof(Data)]; |
| 728 | memcpy(backup, m_items + pos, sizeof(Data)); |
| 729 | |
| 730 | if (bound < pos) { |
| 731 | if (!force && pos - bound > maxMoveAround()) { |
| 732 | // qCDebug(UTIL) << "increasing because" << pos-bound << ">" << maxMoveAround() << "left free items:" << countFreeItems(*m_centralFreeItem) << "target free items:" << (m_itemCount-countFreeItems(*m_centralFreeItem))/increaseFraction; |
| 733 | return false; |
| 734 | } |
| 735 | //Move [bound, pos) one to right, and insert at bound |
| 736 | memmove(m_items + bound + 1, m_items + bound, sizeof(Data) * (pos - bound)); |
| 737 | target = bound; |
| 738 | } else { |
| 739 | Q_ASSERT(bound > pos); |
| 740 | if (!force && bound - pos - 1 > maxMoveAround()) { |
| 741 | // qCDebug(UTIL) << "increasing because" << bound-pos-1 << ">" << maxMoveAround() << "left free items:" << countFreeItems(*m_centralFreeItem)<< "target free items:" << (m_itemCount-countFreeItems(*m_centralFreeItem))/increaseFraction; |
| 742 | return false; |
| 743 | } |
| 744 | //Move (pos, bound) one to left, and insert at bound-1 |
| 745 | memmove(m_items + pos, m_items + pos + 1, sizeof(Data) * (bound - pos - 1)); |
| 746 | target = bound - 1; |
| 747 | } |
| 748 | memcpy(m_items + target, backup, sizeof(Data)); |
| 749 | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && (((__GNUC__ * 100) + __GNUC_MINOR__) >= 800) |
| 750 | #pragma GCC diagnostic pop |
| 751 | #endif |
| 752 | |
| 753 | ItemHandler::copyTo(data, m_items[target]); |
| 754 | return true; |
nothing calls this directly
no test coverage detected