瀏覽代碼

Fix the implementation of `binary_search_by` to work with a normal `ordering` call, rather than the backwards version.

WHY THE HECK WAS IT THIS WAY IN THE FIRST PLACE?!
gingerBill 1 年之前
父節點
當前提交
c8cc130744
共有 1 個文件被更改,包括 4 次插入12 次删除
  1. 4 12
      core/slice/slice.odin

+ 4 - 12
core/slice/slice.odin

@@ -158,15 +158,7 @@ linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, f
 binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
 	where intrinsics.type_is_ordered(T) #no_bounds_check
 {
-	// I would like to use binary_search_by(array, key, cmp) here, but it doesn't like it:
-	// Cannot assign value 'cmp' of type 'proc($E, $E) -> Ordering' to 'proc(i32, i32) -> Ordering' in argument
-	return binary_search_by(array, key, proc(key: T, element: T) -> Ordering {
-		switch {
-			case element < key: return .Less
-			case element > key: return .Greater
-			case:               return .Equal
-		}
-	})
+	return binary_search_by(array, key, cmp_proc(T))
 }
 
 @(require_results)
@@ -194,9 +186,9 @@ binary_search_by :: proc(array: $A/[]$T, key: T, f: proc(T, T) -> Ordering) -> (
 		right = mid     if cmp == .Greater else right
 
 		switch cmp {
-			case .Equal:   return mid, true
-			case .Less:    left  = mid + 1
-			case .Greater: right = mid
+		case .Equal:   return mid, true
+		case .Less:    right = mid
+		case .Greater: left  = mid + 1
 		}
 
 		size = right - left