diff globalneighbors/distance.py @ 7:254195d0bac2

partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 09:13:48 -0700
parents 316e1d54ffd4
children e3d6919130ca
line wrap: on
line diff
--- a/globalneighbors/distance.py	Sat Jun 24 15:47:59 2017 -0700
+++ b/globalneighbors/distance.py	Sun Jun 25 09:13:48 2017 -0700
@@ -38,6 +38,9 @@
     keeping distances in order with maximum of size `k`
     """
 
+    if len(distances) == k and new_distance >= distances[-1][-1]:
+        return
+
      # TODO: Binary Search Tree
     for _index, (geoid, old_distance) in enumerate(distances):
         if new_distance < old_distance:
@@ -48,14 +51,18 @@
     else:
         distances.append((i, new_distance))
 
-# def insert_distance(distances, i, new_distance, k):
-#     if not distances:
-#          distances.append((i, new_distance))
-#          return
-#     if new_distance >= distances[-1][-1]:
-#            if len(distances) < k:
-#                distances.append((i, new_distance))
-#            return
+
+def insert_distance_bisect(distances, i, new_distance, k):
+    if not distances:
+        distances.append((i, new_distance))
+        return
+    if new_distance >= distances[-1][-1]:
+        if len(distances) < k:
+            distances.append((i, new_distance))
+        return
+    indices = [0, len(distances)]
+    while True:
+        midpoint = int((indices[-1] - indices[0])/2)
 
 
 def calculate_distances(locations, r=Rearth):
@@ -124,8 +131,6 @@
             # insert in order
             for i in (id1, id2):
                 distances = neighbors.setdefault(i, [])
-                if len(distances) == k and new_distance >= distances[-1][-1]:
-                    continue
 
                 insert_distance(distances, i, new_distance, k)