Browse Source

Clean up `struct_fields_index_by_increasing_offset`

gingerBill 3 years ago
parent
commit
171a5d6476
1 changed files with 24 additions and 18 deletions
  1. 24 18
      src/types.cpp

+ 24 - 18
src/types.cpp

@@ -2759,23 +2759,30 @@ Selection lookup_field_with_selection(Type *type_, String field_name, bool is_ty
 	return sel;
 }
 
-GB_COMPARE_PROC(struct_field_cmp_by_offset) {
-	i64 x = *(i64 const *)(a);
-	i64 y = *(i64 const *)(b);
-	if (x < y) {
-		return -1;
-	} else if (x > y) {
-		return +1;
+bool are_struct_fields_reordered(Type *type) {
+	type = base_type(type);
+	GB_ASSERT(type->kind == Type_Struct);
+	type_set_offsets(type);
+	GB_ASSERT(type->Struct.offsets != nullptr);
+	
+	i64 prev_offset = 0;
+	for_array(i, type->Struct.fields) {
+		i64 offset = type->Struct.offsets[i];
+		if (prev_offset > offset) {
+			return true;
+		}
+		prev_offset = offset;
 	}
-	return 0;
-}
 
+	return false;
+}
 
-Slice<i32> struct_fields_index_by_increasing_offset(Type *type, gbAllocator allocator) {
+Slice<i32> struct_fields_index_by_increasing_offset(gbAllocator allocator, Type *type) {
 	type = base_type(type);
 	GB_ASSERT(type->kind == Type_Struct);
 	type_set_offsets(type);
 	GB_ASSERT(type->Struct.offsets != nullptr);
+	
 	auto indices = slice_make<i32>(allocator, type->Struct.fields.count);
 	
 	i64 prev_offset = 0;
@@ -2790,14 +2797,13 @@ Slice<i32> struct_fields_index_by_increasing_offset(Type *type, gbAllocator allo
 	}
 	if (!is_ordered) {
 		isize n = indices.count;
-		for (isize i = 0; i < n-1; i++) {
-			for (isize j = 0; j < n-i-1; j++) {
-				isize a = j;
-				isize b = j+1;
-				if (type->Struct.offsets[a] > type->Struct.offsets[b]) {
-					gb_swap(i32, indices[a], indices[b]);
-				}
-			}
+		for (isize i = 1; i < n; i++) {
+			isize j = i;
+			
+			while (j > 0 && type->Struct.offsets[indices[j-1]] > type->Struct.offsets[indices[j]]) {
+				gb_swap(i32, indices[j-1], indices[j]);
+				j -= 1;
+			}				
 		}
 	}