SparseVector

pull/1/head
Ogoun 5 years ago
parent 68699d2056
commit fd37d64d33

@ -0,0 +1,7 @@
namespace ZeroLevel.Models
{
public interface IEmptyable
{
bool IsEmpty { get; }
}
}

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using ZeroLevel.Models;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services.Semantic.Helpers namespace ZeroLevel.Services.Semantic.Helpers
@ -100,4 +101,48 @@ namespace ZeroLevel.Services.Semantic.Helpers
this.values = reader.ReadDoubleCollection().ToArray(); this.values = reader.ReadDoubleCollection().ToArray();
} }
} }
public sealed class SparseVector<T>
where T : IEmptyable
{
private readonly static int[] EmptyIndexes = new int[0];
private readonly static T[] EmptyValues = new T[0];
private int[] indexes;
private T[] values;
public int Count => indexes?.Length ?? 0;
public SparseVector()
{
indexes = EmptyIndexes;
values = EmptyValues;
}
public SparseVector(T[] vector)
{
var l = new List<int>();
for (int i = 0; i < vector.Length; i++)
{
if (!vector[i].IsEmpty)
{
l.Add(i);
}
}
indexes = l.ToArray();
values = new T[l.Count];
for (int i = 0; i < l.Count; i++)
{
values[i] = vector[indexes[i]];
}
}
public SparseVector(T[] vector, int[] indicies)
{
indexes = indicies;
values = vector;
}
public (T, int) this[int index] => (values[index], indexes[index]);
}
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.