Skip to content

Commit

Permalink
Use quicksort
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Oct 26, 2024
1 parent e4b518a commit 92a7211
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Sat, 26 Oct 2024 15:00:21 +0800
Subject: [PATCH] Use parallelQuickSort in NearestLivingEntitySensor
Subject: [PATCH] Use QuickSort in NearestLivingEntitySensor


diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
index 5a059e1ec232d82e8e891ae78fea962bec2f878e..f6cdabf05b034fab5f72d098017bf1115e9ffb57 100644
index 5a059e1ec232d82e8e891ae78fea962bec2f878e..490c5322f8286f92f9ba4583f59ed60618ad33e5 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
@@ -16,10 +16,16 @@ public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T>
protected void doTick(ServerLevel world, T entity) {
AABB aABB = entity.getBoundingBox().inflate((double)this.radiusXZ(), (double)this.radiusY(), (double)this.radiusXZ());
List<LivingEntity> list = world.getEntitiesOfClass(LivingEntity.class, aABB, e -> e != entity && e.isAlive());
- list.sort(Comparator.comparingDouble(entity::distanceToSqr));
+ // Leaf start - Use parallelQuickSort in NearestLivingEntitySensor
+ LivingEntity[] sorted = list.toArray(new LivingEntity[]{});
+ it.unimi.dsi.fastutil.objects.ObjectArrays.parallelQuickSort(sorted, Comparator.comparingDouble(entity::distanceToSqr));
+ List<LivingEntity> sortedList = java.util.Arrays.asList(sorted);
+ // Leaf end - Use parallelQuickSort in NearestLivingEntitySensor
+ // Leaf start - Use QuickSort in NearestLivingEntitySensor
+ LivingEntity[] sortArray = list.toArray(new LivingEntity[]{});
+ it.unimi.dsi.fastutil.objects.ObjectArrays.quickSort(sortArray, Comparator.comparingDouble(entity::distanceToSqr));
+ List<LivingEntity> sortedList = java.util.Arrays.asList(sortArray);
+ // Leaf end - Use QuickSort in NearestLivingEntitySensor
Brain<?> brain = entity.getBrain();
- brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, list);
- brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, list));
+ // Leaf start - Use parallelQuickSort in NearestLivingEntitySensor
+ // Leaf start - Use QuickSort in NearestLivingEntitySensor
+ brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, sortedList);
+ brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, sortedList));
+ // Leaf end - Use parallelQuickSort in NearestLivingEntitySensor
+ // Leaf end - Use QuickSort in NearestLivingEntitySensor
}

protected int radiusXZ() {

0 comments on commit 92a7211

Please sign in to comment.