Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
TODO--
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 26 Apr 2018 12:59:16 +0000 (14:59 +0200)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 26 Apr 2018 12:59:16 +0000 (14:59 +0200)
(SO says it won't improve performance though)

examples/java/dht/kademlia/Answer.java
examples/java/dht/kademlia/RoutingTable.java

index 0a30cf6..03a4923 100644 (file)
@@ -31,8 +31,9 @@ public class Answer {
     return nodes.size();
   }
 
-  public void remove(int index) {
-    nodes.remove(index);
+  public void trim() {
+    if (nodes.size() > Common.BUCKET_SIZE)
+      nodes.subList(nodes.size() - Common.BUCKET_SIZE, nodes.size()).clear();
   }
 
   public void add(Contact contact) {
@@ -51,9 +52,8 @@ public class Answer {
     }
     Collections.sort(nodes);
     //Trim the list
-    while (answer.size() > Common.BUCKET_SIZE) {
-      answer.remove(answer.size() - 1);
-    }
+    answer.trim();
+
     return nbAdded;
   }
 
index 78f80ad..d8fd0c9 100644 (file)
@@ -95,9 +95,8 @@ public class RoutingTable {
     //We sort the list
     Collections.sort(answer.getNodes());
     //We trim the list
-    while (answer.size() > Common.BUCKET_SIZE) {
-      answer.remove(answer.size() - 1); //TODO: Not the best thing.
-    }
+    answer.trim();
+
     return answer;
   }