]> git.mxchange.org Git - flightgear.git/commitdiff
Extender iterative search APIs with a 'has-next' return arg, to enable better iterati...
authorjmt <jmt>
Sun, 4 Oct 2009 22:14:14 +0000 (22:14 +0000)
committerTim Moore <timoore@redhat.com>
Tue, 6 Oct 2009 05:28:38 +0000 (07:28 +0200)
src/Navaids/positioned.cxx
src/Navaids/positioned.hxx

index e603e9eb0af3e3bc6c97f2a2773f683f96977235..38bce7d251914c30592c32a12dc39db53fa9be8d 100644 (file)
@@ -646,7 +646,7 @@ FGPositioned::findNextWithPartialId(FGPositionedRef aCur, const std::string& aId
 }
   
 FGPositionedRef
-FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset)
+FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
 {
   // see comment in findNextWithPartialId concerning upperBoundId
   std::string upperBoundId = aId;
@@ -654,6 +654,7 @@ FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOf
   NamedPositionedIndex::const_iterator upperBound = global_namedIndex.lower_bound(upperBoundId);
 
   NamedIndexRange range = global_namedIndex.equal_range(aId);
+  FGPositionedRef result;
   
   while (range.first != upperBound) {
     for (; range.first != range.second; ++range.first) {
@@ -668,8 +669,12 @@ FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOf
         }
       }
 
-      if (aOffset == 0) {
-        return candidate;
+      if (result) {
+        aNext = true;
+        return result;
+      } else if (aOffset == 0) {
+        // okay, found our result. we need to go around once more to set aNext
+        result = candidate;
       } else {
         --aOffset; // seen one more valid result, decrement the count
       }
@@ -679,7 +684,10 @@ FGPositioned::findWithPartialId(const std::string& aId, Filter* aFilter, int aOf
     range = global_namedIndex.equal_range(range.second->first);
   }
 
-  return NULL; // Reached the end of the valid sequence with no match.  
+  // if we fell out, we reached the end of the valid range. We might have a
+  // valid result, but we definitiely don't have a next result.
+  aNext = false;
+  return result;  
 }
 
 /**
@@ -715,16 +723,20 @@ private:
 };
 
 FGPositionedRef
-FGPositioned::findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset)
+FGPositioned::findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset, bool& aNext)
 {
   PartialIdentFilter pf(aId, aFilter);
-  List matches = spatialGetClosest(aPos, aOffset + 1, 1000.0, &pf);
+  // why aOffset +2 ? at offset=3, we want the fourth search result, but also
+  // to know if the fifth result exists (to set aNext flag for iterative APIs)
+  List matches = spatialGetClosest(aPos, aOffset + 2, 1000.0, &pf);
   
   if ((int) matches.size() <= aOffset) {
     SG_LOG(SG_GENERAL, SG_INFO, "FGPositioned::findClosestWithPartialId, couldn't match enough with prefix:" << aId);
+    aNext = false;
     return NULL; // couldn't find a match within the cutoff distance
   }
   
+  aNext = ((int) matches.size() >= (aOffset + 2));
   return matches[aOffset];
 }
 
index 8ee897aaaba13a6a1b70b06654dc3bc52e0ddd19..10591a21141ac9187501b41013a766489482048d 100644 (file)
@@ -161,7 +161,7 @@ public:
   /**
    * As above, but searches using an offset index
    */
-  static FGPositionedRef findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset);
+  static FGPositionedRef findWithPartialId(const std::string& aId, Filter* aFilter, int aOffset, bool& aNext);
   
   /**
    * Find all items with the specified ident, and return then sorted by
@@ -197,7 +197,7 @@ public:
    * Find the closest match based on partial id (with an offset to allow selecting the n-th closest).
    * Cutoff distance is limited internally, to avoid making this very slow.
    */
-  static FGPositionedRef findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset);
+  static FGPositionedRef findClosestWithPartialId(const SGGeod& aPos, const std::string& aId, Filter* aFilter, int aOffset, bool& aNext);
 
   /**
    * Map a candidate type string to a real type. Returns INVALID if the string