Skip to content

Commit

Permalink
weighted graph refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Dermody committed Aug 3, 2024
1 parent 6f56351 commit 4645449
Show file tree
Hide file tree
Showing 17 changed files with 563 additions and 497 deletions.
1 change: 1 addition & 0 deletions BrightData.UnitTests/FixedSizeArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using BrightData.Types;
using BrightData.Types.Graph;
using FluentAssertions;
using Xunit;

Expand Down
41 changes: 41 additions & 0 deletions BrightData.UnitTests/SortedArrayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using BrightData.Types.Graph;
using BrightData.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

namespace BrightData.UnitTests
{
public class SortedArrayTests
{
[Fact]
public void TestIndexedSortedArray()
{
var array = new IndexedSortedArray<GraphNodeIndex>(4);
for (var i = 0U; i < 4; i++)
array.Add(new(4-i));
array.Size.Should().Be(4);
array[0].Index.Should().Be(1);
array[3].Index.Should().Be(4);
array.TryFind(1, out var index).Should().BeTrue();
index!.Value.Index.Should().Be(1);
}

[Fact]
public void TestSortedArray()
{
var array = new SortedArray<uint, float>(4);
for (var i = 0U; i < 4; i++)
array.Add(i, 4-i);
array.Size.Should().Be(4);
array[0].Weight.Should().Be(1);
array[3].Weight.Should().Be(4);
array.TryFind(1, out var value).Should().BeTrue();
value.Should().Be(3);
}
}
}
4 changes: 2 additions & 2 deletions BrightData.UnitTests/VectorSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using BrightData.UnitTests.Helper;
using System.Linq;
using BrightData.LinearAlgebra.VectorIndexing;
using BrightData.LinearAlgebra.VectorIndexing.Helper;
using BrightData.Types;
using BrightData.Types.Graph;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -67,7 +67,7 @@ public void Closest()
[Fact]
public void TestVectorGraphNode()
{
var node = new IndexedFixedSizeGraphNode<float, FixedSizeSortedAscending8Array<uint, float>>(1);
var node = new FixedSizeWeightedGraphNode<GraphNodeIndex, float, FixedSizeSortedAscending8Array<uint, float>>(new(1));
node.Index.Should().Be(1);
node.NeighbourIndices.Length.Should().Be(0);

Expand Down
19 changes: 13 additions & 6 deletions BrightData/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public interface IFixedSizeSortedArray<V, W> : IHaveSize
/// Returns a value and weight
/// </summary>
/// <param name="index">Index to return</param>
(V Value, W Weight) this[byte index] { get; }
(V Value, W Weight) this[uint index] { get; }

/// <summary>
/// Enumerates the values and weights
Expand Down Expand Up @@ -652,7 +652,7 @@ public interface IWeightedGraphNode<out T, W> : IGraphNode
{
public T Value { get; }

bool AddNeighbour(uint index, W weight);
bool TryAddNeighbour(uint index, W weight);

IEnumerable<(uint Index, W Weight)> WeightedNeighbours { get; }
}
Expand All @@ -663,14 +663,13 @@ public interface ICalculateNodeWeights<out W>
W GetWeight(uint fromIndex, uint toIndex);
}

public interface IWeightedGraph<T, W> : IHaveSize
public interface IWeightedGraph<out T, W> : IHaveSize
where T: IHaveSingleIndex
where W : unmanaged, INumber<W>, IMinMaxValue<W>
{
void Add(T value);
void Add(T value, ReadOnlySpan<(uint Index, W Weight)> neighbours);
T Find(uint nodeIndex);

T Get(uint index);
T this[uint nodePosition] { get; }

RAT Search<RAT, CAT>(uint q, uint entryPoint, ICalculateNodeWeights<W> distanceCalculator)
where RAT : struct, IFixedSizeSortedArray<uint, W>
Expand All @@ -681,4 +680,12 @@ RAT Search<RAT, CAT>(uint q, uint entryPoint, ICalculateNodeWeights<W> distanceC

bool AddNeighbour(uint nodeIndex, uint neighbourIndex, W weight);
}

public interface IWeightedDynamicGraph<T, W> : IWeightedGraph<T, W>
where T: IHaveSingleIndex
where W : unmanaged, INumber<W>, IMinMaxValue<W>
{
void Add(T value);
void Add(T value, ReadOnlySpan<(uint Index, W Weight)> neighbours);
}
}

This file was deleted.

141 changes: 0 additions & 141 deletions BrightData/LinearAlgebra/VectorIndexing/Helper/VectorGraph.cs

This file was deleted.

Loading

0 comments on commit 4645449

Please sign in to comment.