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
Forum Updated to NodeBB v4.3 + New Features

Find first entry with key greater than given value in QMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 881 Views 1 Watching
  • 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 19 Mar 2019, 21:58 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?

    J K K 3 Replies Last reply 19 Mar 2019, 22:10
    0
    • K kitfox
      19 Mar 2019, 21:58

      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?

      J Offline
      J Offline
      JonB
      wrote on 19 Mar 2019, 22:10 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
        19 Mar 2019, 21:58

        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?

        K Offline
        K Offline
        KroMignon
        wrote on 19 Mar 2019, 22:22 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
          19 Mar 2019, 21:58

          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?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 19 Mar 2019, 23:56 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

          3/4

          19 Mar 2019, 22:22

          • Login

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