RandomSortStrategy

This commit is contained in:
naninunenoy 2020-11-17 02:43:02 +09:00
parent 2cba3e7e95
commit a55f46e7c3
14 changed files with 197 additions and 2 deletions

View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public class NodeGrid {
public readonly float GridSize;
readonly int gridSideCount;
public readonly int GridCount;
public NodeGrid(float nodeWidth, float nodeDistance, int nodeCount) {
GridSize = nodeDistance + nodeWidth / 2.0F;
gridSideCount = SquareValueProvider.ProvideNearestSquareValue(nodeCount);
GridCount = gridSideCount * gridSideCount;
}
public IReadOnlyList<Vector2> GridCenterPositions() {
var grids = new Vector2[GridCount];
var index = 0;
var y = 0.0F;
for (var i = 0; i < gridSideCount; i++) {
var x = 0.0F;
for (var j = 0; j < gridSideCount; j++) {
grids[index] = new Vector2(x, y);
x += GridSize;
index++;
}
y += GridSize;
}
return grids;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bf9e7d3635b441398a2011b98e35610b
timeCreated: 1605545271

View File

@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public class RandomSortStrategy : ISortStrategy {
readonly Vector2 originPosition;
readonly float basicDistance;
readonly float nodeWidth;
readonly float nodeHeight;
public RandomSortStrategy(Vector2 originPosition, float basicDistance, float nodeWidth, float nodeHeight) {
this.originPosition = originPosition;
this.basicDistance = basicDistance;
this.nodeWidth = nodeWidth;
this.nodeHeight = nodeHeight;
}
public IEnumerable<SortedNode> Sort(IEnumerable<IDependencyNode> nodes) {
var nodeArr = nodes
.Select(x => new SortedNode { Profile = x.Profile, Position = originPosition })
.ToArray();
var nodeGrid = new NodeGrid(nodeWidth, basicDistance, nodeArr.Length);
var positions = nodeGrid.GridCenterPositions();
var indexes = Enumerable.Range(0, positions.Count).ToList();
foreach (var node in nodeArr) {
var randomIndex = indexes[Random.Range(0, indexes.Count)];
node.Position = positions[randomIndex] + originPosition;
indexes.Remove(randomIndex);
}
return nodeArr;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f63fbb77026544bb82b0ddf7e2b507b0
timeCreated: 1605544752

View File

@ -0,0 +1,19 @@
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public static class SquareValueProvider {
public static int ProvideNearestSquareValue(int count) {
if (count < 1) return 0;
if (count == 1) return 1;
var value = 1;
while (value < 100) {
var square = value * value;
var nextValue = value + 1;
var nextSquare = nextValue * nextValue;
value = nextValue;
if (square <= count && count <= nextSquare) {
break;
}
}
return value;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b8521b362be64b7eb1bcf8174be0ecf8
timeCreated: 1605545553

View File

@ -7,7 +7,7 @@ using UnityEngine;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class LinerSortTest {
public class LinerSortStrategyTest {
const float d = 10.0F;
const float w = 10.0F;
const float h = 10.0F;
@ -20,7 +20,6 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
sortStrategy = new LinerSortStrategy(Vector2.zero, d, w, h);
}
[Test]
public void TestLinerNodeDependency() {
var nodes = new[] { Nodes._0, Nodes._1 };

View File

@ -0,0 +1,27 @@
using System.Collections;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using NUnit.Framework;
using UnityEditor;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class NodeGridTest {
[Test]
public void TestNodeGrid() {
const float e = 0.000001F;
var nodeGrid = new NodeGrid(10.0F, 10.0F, 4);
Assert.That(nodeGrid.GridCount, Is.EqualTo(4));
Assert.That(nodeGrid.GridSize, Is.EqualTo(15.0F).Within(e));
var grids = nodeGrid.GridCenterPositions();
Assert.That(grids[0].x, Is.EqualTo(0.0F).Within(e));
Assert.That(grids[0].y, Is.EqualTo(0.0F).Within(e));
Assert.That(grids[1].x, Is.EqualTo(15.0F).Within(e));
Assert.That(grids[1].y, Is.EqualTo(0.0F).Within(e));
Assert.That(grids[2].x, Is.EqualTo(0.0F).Within(e));
Assert.That(grids[2].y, Is.EqualTo(15.0F).Within(e));
Assert.That(grids[3].x, Is.EqualTo(15.0F).Within(e));
Assert.That(grids[3].y, Is.EqualTo(15.0F).Within(e));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 43541e9cd66e4df59c2cac11ada769fe
timeCreated: 1605546997

View File

@ -0,0 +1,34 @@
using System.Collections;
using System.Linq;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class RandomSortStrategyTest {
const float d = 10.0F;
const float w = 10.0F;
const float h = 10.0F;
const float e = 0.0001F;
ISortStrategy sortStrategy;
[SetUp]
public void SetUpBeforeEveryTest() {
Nodes.Init();
sortStrategy = new RandomSortStrategy(Vector2.zero, d, w, h);
}
[Test]
public void TestLinerNodeDependency() {
var result = sortStrategy.Sort(Nodes.All).ToArray();
foreach (var node in result) {
var others = result.Where(x => x.Profile != node.Profile);
foreach (var other in others) {
Assert.That(node.Position, Is.Not.EqualTo(other.Position).Within(e));
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fa8b199e711d454f92390170a758b146
timeCreated: 1605544903

View File

@ -0,0 +1,31 @@
using System;
using System.Collections;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using NUnit.Framework;
using UnityEditor;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class SquareValueProviderTest {
[Test]
public void TestProvideNearestSquareValue() {
var ans = SquareValueProvider.ProvideNearestSquareValue(0);
Assert.That(ans, Is.EqualTo(0));
ans = SquareValueProvider.ProvideNearestSquareValue(1);
Assert.That(ans, Is.EqualTo(1));
ans = SquareValueProvider.ProvideNearestSquareValue(4);
Assert.That(ans, Is.EqualTo(2));
ans = SquareValueProvider.ProvideNearestSquareValue(8);
Assert.That(ans, Is.EqualTo(3));
ans = SquareValueProvider.ProvideNearestSquareValue(9);
Assert.That(ans, Is.EqualTo(3));
ans = SquareValueProvider.ProvideNearestSquareValue(10);
Assert.That(ans, Is.EqualTo(4));
ans = SquareValueProvider.ProvideNearestSquareValue(10001);
Assert.That(ans, Is.EqualTo(100));
ans = SquareValueProvider.ProvideNearestSquareValue(-1);
Assert.That(ans, Is.EqualTo(0));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 917495e8285943d4a194235a789c7bc1
timeCreated: 1605545948