def getPolygon2Ds(vertex_list): '''Finds all closed Polygon2Ds. The algorithm starts with Wedge A-B-C and looks for Wedge B-C-X1, C-X1-X2... A polygon is found when Xn==A''' wedgeliste=getWedges(vertex_list) from bisect import bisect_left as search allpolygons=[] def search(x): s1=x.b s2=x.c for wedge in wedgeliste: if wedge.a==s1 and wedge.b==s2: return wedge while len(wedgeliste)>0: start=x=wedgeliste[0] currentpolygon=[] while True: if not (np.pi - 0.001 < x.alpha < np.pi + 0.001) : currentpolygon.append(x) x=search(x) if x is not None: wedgeliste.remove(x) if x is start: allpolygons.append(currentpolygon) break return allpolygons
def getPolygon2Ds(vertex_list): '''Finds all closed Polygon2Ds. The algorithm starts with Wedge A-B-C and looks for Wedge B-C-X1, C-X1-X2... A polygon is found when Xn==A''' wedgeliste = getWedges(vertex_list) from bisect import bisect_left as search allpolygons = [] def search(x): s1 = x.b s2 = x.c for wedge in wedgeliste: if wedge.a == s1 and wedge.b == s2: return wedge while len(wedgeliste) > 0: start = x = wedgeliste[0] currentpolygon = [] while True: if not (np.pi - 0.001 < x.alpha < np.pi + 0.001): currentpolygon.append(x) x = search(x) if x is not None: wedgeliste.remove(x) if x is start: allpolygons.append(currentpolygon) break return allpolygons
def _loc(u, v, G): if u > v: v,u = u,v if degree(u,G) > degree(v,G): u,v = v,u ls = G[edge_index_aux_idx][u] loc = search(ls, (v,0)) idx = -1 if loc < len(ls) and \ ls[loc][edge_index_aux_target_idx] == v: idx = ls[loc][edge_index_aux_data_idx] else: assert loc >= 0 loc = -loc-1 return (loc, u, v, idx)
def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ # O(log n) time complexity from bisect import bisect_left as search if len(nums) == 0: return -1 # Binary search to find the peak left = 0 right = len(nums) - 1 while right - left > 1: mid = left + right >> 1 if nums[mid] < nums[left]: # Is not it monotonic? right = mid else: left = mid # The peak splits the array into two halves that are strictly increasing # Binary search individually in both halves low_half = search(nums, target, lo=right) high_half = search(nums, target, hi=right) if low_half < len(nums) and nums[low_half] == target: return low_half elif nums[high_half] == target: return high_half return -1