Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find first entry with key greater than given value in QMap

Find first entry with key greater than given value in QMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 786 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I've created a QMap<int, MyDataType> myIndex; that is using the key as an index in a sparse array. I would like the user to be able to quickly look up the 'closest' value to a given index, even if that index is not in the map. That is, for any index I'm asked to look up, I'd like to find both the closest index that is less than or equal to it and the index that is greater than or equal to it. Is there a way to do this that is faster then just linearly scanning through all the keys?

    JonBJ KroMignonK kshegunovK 3 Replies Last reply
    0
    • K kitfox

      I've created a QMap<int, MyDataType> myIndex; that is using the key as an index in a sparse array. I would like the user to be able to quickly look up the 'closest' value to a given index, even if that index is not in the map. That is, for any index I'm asked to look up, I'd like to find both the closest index that is less than or equal to it and the index that is greater than or equal to it. Is there a way to do this that is faster then just linearly scanning through all the keys?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @kitfox
      I do not believe there is any fast method for this from a QMap.

      1 Reply Last reply
      0
      • K kitfox

        I've created a QMap<int, MyDataType> myIndex; that is using the key as an index in a sparse array. I would like the user to be able to quickly look up the 'closest' value to a given index, even if that index is not in the map. That is, for any index I'm asked to look up, I'd like to find both the closest index that is less than or equal to it and the index that is greater than or equal to it. Is there a way to do this that is faster then just linearly scanning through all the keys?

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #3

        @kitfox QMap keys are ascending ordered (cf. https://doc.qt.io/qt-5/qmap.html#details), so if you iter though map with QMapInterator with toBack() you can start for highest key value and then go to lowest with previous()

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • K kitfox

          I've created a QMap<int, MyDataType> myIndex; that is using the key as an index in a sparse array. I would like the user to be able to quickly look up the 'closest' value to a given index, even if that index is not in the map. That is, for any index I'm asked to look up, I'd like to find both the closest index that is less than or equal to it and the index that is greater than or equal to it. Is there a way to do this that is faster then just linearly scanning through all the keys?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          You'd be probably better off using an ordered array instead. Most of the time the tree rotations of QMap aren't worth it. Consider the following:

          struct SparseArrayEntry
          {
              int index;
              MyDataType data;
          };
          QVector<SparseArrayEntry> myIndex;
          

          Sort with a custom comparator before searching and use a binary search for log(N) complexity (the same complexity as with QMap).

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved