public void Add(string input) { var baseNumber = Convert.ToInt32(input, 2); var root = baseNumber; var numberGenerator = new NumberGenerator(input); foreach (var num in numberGenerator) { if (unionFind.Contains(num)) { var newRoot = unionFind.Find(num); unionFind.Union(newRoot, root); root = newRoot; } else { unionFind.AddElement(num, root); } } }
private bool VerifyRoomAccessibility( DungeonLayout layout, Dictionary<int, Portal> portalIdToPortalMap) { UnionFind<RoomKey> roomUnion = new UnionFind<RoomKey>(); RoomKey targetRoomKey = new RoomKey(); bool success = true; // Do a first pass over the rooms to fill out the union and the portal map for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); // Add the room to the union set roomUnion.AddElement(room.room_key); } // Union together all of the rooms connected by portals for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); foreach (Portal portal in room.portals) { Portal targetPortal= portalIdToPortalMap[portal.target_portal_id]; targetRoomKey.game_id= layout.GameID; targetRoomKey.x= targetPortal.room_x; targetRoomKey.y= targetPortal.room_y; targetRoomKey.z= targetPortal.room_z; roomUnion.Union(room.room_key, targetRoomKey); } } // Verify that all rooms share the same connectivity id int sharedConnectivityId = -1; for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); int roomConnectivityId= roomUnion.FindRootIndex(room.room_key); if (sharedConnectivityId != -1) { if (sharedConnectivityId != roomConnectivityId) { _logger.WriteLine("DungeonValidator: FAILED: Found room not connected to other rooms in dungeon!"); _logger.WriteLine(string.Format(" game_id: {0}", layout.GameID)); _logger.WriteLine(string.Format(" room_key: {0},{1},{2}", room.room_key.x, room.room_key.y, room.room_key.z)); success= false; break; } } else { sharedConnectivityId= roomConnectivityId; } } return success; }
public bool RunTest() { string[] elements = new string[10] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }; UnionFind<string> unionFind = new UnionFind<string>(); bool success = true; // Add in entries for (int i = 0; i < 10; i++) { unionFind.AddElement(elements[i]); } // Verify that the roots are setup correctly for (int i = 0; i < 10; i++) { success &= unionFind.GetElement(i).Equals(elements[i]); Debug.Assert(success); success &= unionFind.FindRootIndex(i) == i; Debug.Assert(success); success &= unionFind.FindRootIndex(elements[i]) == i; Debug.Assert(success); } // Verify that no one is initially connected to anyone else for (int i = 1; i < 10; i++) { success &= !unionFind.AreElementsConnected(i - 1, i); Debug.Assert(success); success &= !unionFind.AreElementsConnected(elements[i - 1], elements[i]); Debug.Assert(success); } // Union the first half of the nodes together for (int i = 1; i < 5; i++) { unionFind.Union(elements[i - 1], elements[i]); } // Union the last half of the nodes together for (int i = 6; i < 10; i++) { unionFind.Union(elements[i - 1], elements[i]); } // Verify that every element in the first half is connected // to every other element in the first half for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { success &= unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } // Verify that every element in the second half is connected // to every other element in the second half for (int i = 5; i < 10; i++) { for (int j = i + 1; j < 10; j++) { success &= unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } // Verify that no element in the first half is connected // to any other element in the second half for (int i = 0; i < 5; i++) { for (int j = i + 5; j < 10; j++) { success &= !unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= !unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } return success; }
private bool VerifyRoomAccessibility( DungeonLayout layout, Dictionary <int, Portal> portalIdToPortalMap) { UnionFind <RoomKey> roomUnion = new UnionFind <RoomKey>(); RoomKey targetRoomKey = new RoomKey(); bool success = true; // Do a first pass over the rooms to fill out the union and the portal map for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); // Add the room to the union set roomUnion.AddElement(room.room_key); } // Union together all of the rooms connected by portals for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); foreach (Portal portal in room.portals) { Portal targetPortal = portalIdToPortalMap[portal.target_portal_id]; targetRoomKey.game_id = layout.GameID; targetRoomKey.x = targetPortal.room_x; targetRoomKey.y = targetPortal.room_y; targetRoomKey.z = targetPortal.room_z; roomUnion.Union(room.room_key, targetRoomKey); } } // Verify that all rooms share the same connectivity id int sharedConnectivityId = -1; for (DungeonLayout.RoomIndexIterator iterator = new DungeonLayout.RoomIndexIterator(layout.RoomGrid); iterator.Valid; iterator.Next()) { DungeonLayout.RoomIndex roomIndex = iterator.Current; Room room = layout.GetRoomByIndex(roomIndex); int roomConnectivityId = roomUnion.FindRootIndex(room.room_key); if (sharedConnectivityId != -1) { if (sharedConnectivityId != roomConnectivityId) { _logger.WriteLine("DungeonValidator: FAILED: Found room not connected to other rooms in dungeon!"); _logger.WriteLine(string.Format(" game_id: {0}", layout.GameID)); _logger.WriteLine(string.Format(" room_key: {0},{1},{2}", room.room_key.x, room.room_key.y, room.room_key.z)); success = false; break; } } else { sharedConnectivityId = roomConnectivityId; } } return(success); }
public bool RunTest() { string[] elements = new string[10] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" }; UnionFind <string> unionFind = new UnionFind <string>(); bool success = true; // Add in entries for (int i = 0; i < 10; i++) { unionFind.AddElement(elements[i]); } // Verify that the roots are setup correctly for (int i = 0; i < 10; i++) { success &= unionFind.GetElement(i).Equals(elements[i]); Debug.Assert(success); success &= unionFind.FindRootIndex(i) == i; Debug.Assert(success); success &= unionFind.FindRootIndex(elements[i]) == i; Debug.Assert(success); } // Verify that no one is initially connected to anyone else for (int i = 1; i < 10; i++) { success &= !unionFind.AreElementsConnected(i - 1, i); Debug.Assert(success); success &= !unionFind.AreElementsConnected(elements[i - 1], elements[i]); Debug.Assert(success); } // Union the first half of the nodes together for (int i = 1; i < 5; i++) { unionFind.Union(elements[i - 1], elements[i]); } // Union the last half of the nodes together for (int i = 6; i < 10; i++) { unionFind.Union(elements[i - 1], elements[i]); } // Verify that every element in the first half is connected // to every other element in the first half for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { success &= unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } // Verify that every element in the second half is connected // to every other element in the second half for (int i = 5; i < 10; i++) { for (int j = i + 1; j < 10; j++) { success &= unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } // Verify that no element in the first half is connected // to any other element in the second half for (int i = 0; i < 5; i++) { for (int j = i + 5; j < 10; j++) { success &= !unionFind.AreElementsConnected(i, j); Debug.Assert(success); success &= !unionFind.AreElementsConnected(elements[i], elements[j]); Debug.Assert(success); } } return(success); }
private void ComputeRoomConnectivity(UnionFind <RoomIndex> roomUnion) { IList <RoomIndex> roomIndices = GetRoomIndexList(); roomUnion = new UnionFind <RoomIndex>(); // Add all of the rooms into the union foreach (RoomIndex roomIndex in roomIndices) { roomUnion.AddElement(roomIndex); } // Start connecting to our neighbors foreach (RoomIndex roomIndex in roomIndices) { RoomLayout room = GetRoomByIndex(roomIndex); // Union with the neighbor on the +x side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.positive_x)) { roomUnion.Union(roomIndex, roomIndex.Offset(1, 0, 0)); } // Union with the neighbor on the -x side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.negative_x)) { roomUnion.Union(roomIndex, roomIndex.Offset(-1, 0, 0)); } // Union with the neighbor on the +y side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.positive_y)) { roomUnion.Union(roomIndex, roomIndex.Offset(0, 1, 0)); } // Union with the neighbor on the -y side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.negative_y)) { roomUnion.Union(roomIndex, roomIndex.Offset(0, -1, 0)); } // Union with the neighbor on the +z side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.positive_z)) { roomUnion.Union(roomIndex, roomIndex.Offset(0, 0, 1)); } // Union with the neighbor on the -z side if (room.RoomHasPortalOnSide(MathConstants.eSignedDirection.negative_z)) { roomUnion.Union(roomIndex, roomIndex.Offset(0, 0, -1)); } } // Store the final connectivity id on the room foreach (RoomIndex roomIndex in roomIndices) { RoomLayout room = GetRoomByIndex(roomIndex); room.connectivity_id = roomUnion.FindRootIndex(roomIndex); } }
private static void BuildNavCellConnectivityGrid( uint colomnCount, uint rowCount, BitArray navMeshData, out NavCell[] navCells, out uint nonEmptyNavCellCount) { UnionFind<uint> navCellUnion = new UnionFind<uint>(); // Create an initial set of nav cells // Any valid cell gets a connectivity id of 0 // Any invalid cell gets a connectivity id of EMPTY_NAV_CELL // Valid nav cells get added to the nav cell union set navCells = new NavCell[rowCount * colomnCount]; nonEmptyNavCellCount = 0; for (int navCellIndex = 0; navCellIndex < navMeshData.Length; ++navCellIndex) { if (navMeshData[navCellIndex]) { uint pvsCellIndex = nonEmptyNavCellCount++; navCells[navCellIndex] = new NavCell(0, pvsCellIndex); navCellUnion.AddElement((uint)navCellIndex); } else { navCells[navCellIndex] = new NavCell(); } } // Union together all neighboring nav cells for (int unionElementIndex = 0; unionElementIndex < navCellUnion.SetSize; ++unionElementIndex) { uint navCellIndex = navCellUnion.GetElement(unionElementIndex); for (MathConstants.eDirection direction = MathConstants.eDirection.first; direction < MathConstants.eDirection.count; ++direction) { uint neighborNavCellIndex; if (TryGetValidNeighborNavCellIndex( navCells, colomnCount, rowCount, navCellIndex, direction, out neighborNavCellIndex)) { navCellUnion.Union(navCellIndex, neighborNavCellIndex); } } } // Write the final connectivity IDs back into the nav cells for (int unionElementIndex = 0; unionElementIndex < navCellUnion.SetSize; ++unionElementIndex) { uint navCellIndex = navCellUnion.GetElement(unionElementIndex); int connectivityID = navCellUnion.FindRootIndex(unionElementIndex); navCells[navCellIndex].connectivityId = (short)connectivityID; } }