-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSparseVector.cs
More file actions
145 lines (116 loc) · 4.16 KB
/
SparseVector.cs
File metadata and controls
145 lines (116 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace Pgvector;
public class SparseVector
{
public int Dimensions { get; }
public ReadOnlyMemory<int> Indices { get; }
public ReadOnlyMemory<float> Values { get; }
// caller must ensure:
// 1. indices are sorted, unique, >= 0, and < dimensions
// 2. values does not contain zeros
public SparseVector(int dimensions, ReadOnlyMemory<int> indices, ReadOnlyMemory<float> values)
{
if (indices.Length != values.Length)
{
throw new ArgumentException("indices and values must be the same length");
}
Dimensions = dimensions;
Indices = indices;
Values = values;
}
public SparseVector(ReadOnlyMemory<float> v)
{
var dense = v.Span;
var count = 0;
var capacity = 0;
for (var i = 0; i < dense.Length; i++)
capacity += Convert.ToInt32(dense[i] != 0);
var indices = new int[capacity];
var values = new float[capacity];
for (var i = 0; i < dense.Length; i++)
{
if (dense[i] != 0)
{
indices[count] = i;
values[count] = dense[i];
count++;
}
}
Dimensions = v.Length;
Indices = indices;
Values = values;
}
public SparseVector(IDictionary<int, float> dictionary, int dimensions)
{
var count = 0;
var capacity = dictionary.Count;
var indices = new int[capacity];
var values = new float[capacity];
foreach (var e in dictionary)
{
if (e.Value != 0)
{
indices[count] = e.Key;
values[count] = e.Value;
count++;
}
}
Array.Sort(indices, values, 0, count);
Dimensions = dimensions;
Indices = new ReadOnlyMemory<int>(indices, 0, count);
Values = new ReadOnlyMemory<float>(values, 0, count);
}
public SparseVector(string s)
{
var parts = s.Split(['/'], 2);
var elements = parts[0].Substring(1, parts[0].Length - 2).Split(',');
var nnz = elements.Length;
var indices = new int[nnz];
var values = new float[nnz];
for (int i = 0; i < nnz; i++)
{
var ep = elements[i].Split([':'], 2);
indices[i] = Int32.Parse(ep[0], CultureInfo.InvariantCulture) - 1;
values[i] = float.Parse(ep[1], CultureInfo.InvariantCulture);
}
Dimensions = Int32.Parse(parts[1], CultureInfo.InvariantCulture);
Indices = indices;
Values = values;
}
public override string ToString()
{
var elements = Indices.ToArray().Zip(Values.ToArray(), (i, v) => string.Concat((i + 1).ToString(CultureInfo.InvariantCulture), ":", v.ToString(CultureInfo.InvariantCulture)));
return string.Concat("{", string.Join(",", elements), "}/", Dimensions);
}
public float[] ToArray()
{
var result = new float[Dimensions];
var indices = Indices.Span;
var values = Values.Span;
for (var i = 0; i < indices.Length; i++)
result[indices[i]] = values[i];
return result;
}
public bool Equals(SparseVector? other)
=> other is not null && Dimensions == other.Dimensions && Indices.Span.SequenceEqual(other.Indices.Span) && Values.Span.SequenceEqual(other.Values.Span);
public override bool Equals(object? obj)
=> obj is SparseVector vector && Equals(vector);
public static bool operator ==(SparseVector? x, SparseVector? y)
=> (x is null && y is null) || (x is not null && x.Equals(y));
public static bool operator !=(SparseVector? x, SparseVector? y) => !(x == y);
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(Dimensions);
var indices = Indices.Span;
for (var i = 0; i < indices.Length; i++)
hashCode.Add(indices[i]);
var values = Values.Span;
for (var i = 0; i < values.Length; i++)
hashCode.Add(values[i]);
return hashCode.ToHashCode();
}
}