From 0ede00f7df30f9ce94b6135dbf7730267f89a14d Mon Sep 17 00:00:00 2001 From: Harry5573 Date: Tue, 25 Aug 2015 23:19:09 +0100 Subject: [PATCH] Optimize common MemoryBoard operations This reduces the complexity of: 1. retrieving the total chunks owned by a faction, and 2. removing a faction from the board. --- .../factions/zcore/persist/MemoryBoard.java | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java index 705f241b..a5cb17c5 100644 --- a/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java +++ b/src/main/java/com/massivecraft/factions/zcore/persist/MemoryBoard.java @@ -1,5 +1,7 @@ package com.massivecraft.factions.zcore.persist; +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; import com.massivecraft.factions.*; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.util.AsciiCompass; @@ -11,8 +13,49 @@ import java.util.Map.Entry; public abstract class MemoryBoard extends Board { - public HashMap flocationIds = new HashMap(); + + public class MemoryBoardMap extends HashMap { + Multimap factionToLandMap = HashMultimap.create(); + @Override + public String put(FLocation floc, String factionId) { + String previousValue = super.put(floc, factionId); + if (previousValue != null) { + factionToLandMap.remove(previousValue, floc); + } + + factionToLandMap.put(factionId, floc); + return previousValue; + } + + @Override + public String remove(Object key) { + String result = super.remove(key); + if (result != null) { + FLocation floc = (FLocation) key; + factionToLandMap.remove(result, floc); + } + + return result; + } + + @Override + public void clear() { + super.clear(); + factionToLandMap.clear(); + } + + public int getOwnedLandCount(String factionId) { + return factionToLandMap.get(factionId).size(); + } + + public Collection getOwnedLand(String factionId) { + return factionToLandMap.get(factionId); + } + } + + public MemoryBoardMap flocationIds = new MemoryBoardMap(); + //----------------------------------------------// // Get and Set //----------------------------------------------// @@ -88,12 +131,9 @@ public abstract class MemoryBoard extends Board { } public void clean(String factionId) { - Iterator> iter = flocationIds.entrySet().iterator(); - while (iter.hasNext()) { - Entry entry = iter.next(); - if (entry.getValue().equals(factionId)) { - iter.remove(); - } + Collection keys = flocationIds.getOwnedLand(factionId); + for (FLocation key : keys) { + flocationIds.remove(key); } } @@ -166,13 +206,7 @@ public abstract class MemoryBoard extends Board { //----------------------------------------------// public int getFactionCoordCount(String factionId) { - int ret = 0; - for (String thatFactionId : flocationIds.values()) { - if (thatFactionId.equals(factionId)) { - ret += 1; - } - } - return ret; + return flocationIds.getOwnedLandCount(factionId); } public int getFactionCoordCount(Faction faction) {