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. How to find the nearest value in a QMap ?

How to find the nearest value in a QMap ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 7 Posters 3.4k 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.
  • kshegunovK kshegunov

    @JonB said in How to find the nearest value in a QMap ?:

    Purely OOI: someone has said QMap keys are stored sorted, so can we assume this method will take advantage of that and do a binary search? :)

    Strictly speaking all searches for a key in the map are binary searches. The QMap is a skip-list (similar to a linearized binary tree) red-black tree so it has a logarithmic complexity for search and insert operations.

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

    @kshegunov

    so it has a logarithmic complexity for search and insert operations

    Excellent! And where is that in the Qt docs? It has been commented elsewhere on the Interweb that Qt docs often do not say anything about search order complexity for many of its data structures, which is a shame.

    kshegunovK 1 Reply Last reply
    1
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #9

      Here is the comparison https://doc.qt.io/qt-5/containers.html

      (Z(:^

      JonBJ 1 Reply Last reply
      4
      • JonBJ JonB

        @kshegunov

        so it has a logarithmic complexity for search and insert operations

        Excellent! And where is that in the Qt docs? It has been commented elsewhere on the Interweb that Qt docs often do not say anything about search order complexity for many of its data structures, which is a shame.

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

        Yes, and a minor correction, it used to be skip-list in Qt4, now it's an ordinary red-black tree. I must get more coffee before I write ...

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          Here is the comparison https://doc.qt.io/qt-5/containers.html

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

          @sierdzio said in How to find the nearest value in a QMap ?:

          Here is the comparison https://doc.qt.io/qt-5/containers.html

          Ah ha, that is a useful link for all of us, thank you.

          Mind you, in the case of QMap I don't see where you can access it by index number (only by key), so unless lowerBound() can do that privately I don't see where a binary search can be performed?

          sierdzioS 1 Reply Last reply
          0
          • JonBJ JonB

            @sierdzio said in How to find the nearest value in a QMap ?:

            Here is the comparison https://doc.qt.io/qt-5/containers.html

            Ah ha, that is a useful link for all of us, thank you.

            Mind you, in the case of QMap I don't see where you can access it by index number (only by key), so unless lowerBound() can do that privately I don't see where a binary search can be performed?

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #12

            @JonB said in How to find the nearest value in a QMap ?:

            @sierdzio said in How to find the nearest value in a QMap ?:
            Mind you, in the case of QMap I don't see where you can access it by index number (only by key), so unless lowerBound() can do that privately I don't see where a binary search can be performed?

            QMap<a,b> map;
            const QList<a> keys = map.keys(); // This is sorted.
            

            (Z(:^

            JonBJ 1 Reply Last reply
            1
            • sierdzioS sierdzio

              @JonB said in How to find the nearest value in a QMap ?:

              @sierdzio said in How to find the nearest value in a QMap ?:
              Mind you, in the case of QMap I don't see where you can access it by index number (only by key), so unless lowerBound() can do that privately I don't see where a binary search can be performed?

              QMap<a,b> map;
              const QList<a> keys = map.keys(); // This is sorted.
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #13

              @sierdzio
              Ah, so if you had to, you could binary search map.keys() (instead of the QMap itself) for a key, right?

              kshegunovK 1 Reply Last reply
              0
              • JonBJ JonB

                @sierdzio
                Ah, so if you had to, you could binary search map.keys() (instead of the QMap itself) for a key, right?

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

                You could, but why would you? The point is that you get an iterator to some position, which you can move back or forth. There's no sense in requiring indexing as well.

                Read and abide by the Qt Code of Conduct

                JonBJ 1 Reply Last reply
                1
                • kshegunovK kshegunov

                  You could, but why would you? The point is that you get an iterator to some position, which you can move back or forth. There's no sense in requiring indexing as well.

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

                  @kshegunov
                  We (I) are discussing the efficiency of lowerBound(), or whatever function one might write, being able to take advantage of the fact that QMap keys are stored sorted. If I have million sorted qreals as the map keys, and I want to find the nearest to a certain value as per OP's question, I'd like to search binarily, wouldn't I? Not sequentially by iterator. So I'll need to be able to specify an index number to access the map. Which is what @sierdzio showed me above with

                  const QList<a> keys = map.keys(); // This is sorted.
                  
                  kshegunovK 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @kshegunov
                    We (I) are discussing the efficiency of lowerBound(), or whatever function one might write, being able to take advantage of the fact that QMap keys are stored sorted. If I have million sorted qreals as the map keys, and I want to find the nearest to a certain value as per OP's question, I'd like to search binarily, wouldn't I? Not sequentially by iterator. So I'll need to be able to specify an index number to access the map. Which is what @sierdzio showed me above with

                    const QList<a> keys = map.keys(); // This is sorted.
                    
                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #16

                    @JonB said in How to find the nearest value in a QMap ?:

                    @kshegunov
                    If I have million sorted qreals as the map keys,

                    If you have that you're inventing a square wheel. QMap isn't made for that stuff and is going to be outperformed by any contiguous-access container (vector).

                    and I want to find the nearest to a certain value as per OP's question, I'd like to search binarily, wouldn't I?

                    You already do with lowerBound. Binary trees have stable log key searching, which is why they're used so much for QMap and std::map and such.

                    Not sequentially by iterator.

                    The iterator is your pointer to the node when you traverse the tree. That's how the data's organized, you can't have random access on top of that, it's just not happening.

                    So I'll need to be able to specify an index number to access the map. Which is what @sierdzio showed me above with

                    Which traverses the whole tree (i.e. O(N) to get the keys) ... not very efficient.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    2
                    • VRoninV VRonin

                      mapValues.lowerBound(currentValue)-1

                      Q Offline
                      Q Offline
                      QtVik
                      wrote on last edited by
                      #17

                      @VRonin :Thank you Ronin.

                      1 Reply Last reply
                      0

                      • Login

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