protected override List<Sector> GenerateSectors(List<IndexUnit> indexUnits) { SortedList<Address, List<IndexUnit>> groupings = new SortedList<Address, List<IndexUnit>>(); foreach (IndexUnit indexUnit in indexUnits) { if (!groupings.ContainsKey(indexUnit.Node)) groupings.Add(indexUnit.Node, new List<IndexUnit>()); groupings[indexUnit.Node].Add(indexUnit); } List<Sector> sectors = new List<Sector>(); List<List<IndexUnit>> newGroupings = new List<List<IndexUnit>>(groupings.Values); while (newGroupings.Count > 0) { Sector newSector = new Sector(); List<IndexUnit> initialGrouping = newGroupings[0], addedUnits = new List<IndexUnit>(); newGroupings.Remove(initialGrouping); while (initialGrouping.Count > 0 && newSector.IndexUnits.Count < Constants.INDEX_UNIT_ENTRIES_PER_SECTOR) { newSector.AddIndexUnit(initialGrouping[0]); addedUnits.Add(initialGrouping[0]); initialGrouping.RemoveAt(0); } if (initialGrouping.Count > 0) newGroupings.Add(initialGrouping); else initialGrouping = addedUnits; while (newSector.IndexUnits.Count < Constants.INDEX_UNIT_ENTRIES_PER_SECTOR && newGroupings.Count > 0) { List<List<IndexUnit>> eligibleGroupings = new List<List<IndexUnit>>(); foreach (List<IndexUnit> grouping in newGroupings) if (grouping.Count + newSector.IndexUnits.Count <= Constants.INDEX_UNIT_ENTRIES_PER_SECTOR) eligibleGroupings.Add(grouping); if (eligibleGroupings.Count == 0) break; List<IndexUnit> closestGrouping = PickClosestGrouping(initialGrouping, eligibleGroupings); foreach (IndexUnit i in closestGrouping) newSector.AddIndexUnit(i); initialGrouping.AddRange(closestGrouping); newGroupings.Remove(closestGrouping); } sectors.Add(newSector); } return sectors; }