-
Notifications
You must be signed in to change notification settings - Fork 16
/
Vector.cs
44 lines (31 loc) · 1.25 KB
/
Vector.cs
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
using System;
using System.Globalization;
using System.Linq;
namespace Pgvector;
public class Vector : IEquatable<Vector>
{
public ReadOnlyMemory<float> Memory { get; }
public Vector(ReadOnlyMemory<float> v)
=> Memory = v;
public Vector(string s)
=> Memory = Array.ConvertAll(s.Substring(1, s.Length - 2).Split(','), v => float.Parse(v, CultureInfo.InvariantCulture));
public override string ToString()
=> string.Concat("[", string.Join(",", Memory.ToArray().Select(v => v.ToString(CultureInfo.InvariantCulture))), "]");
public float[] ToArray()
=> Memory.ToArray();
public bool Equals(Vector? other)
=> other is not null && Memory.Span.SequenceEqual(other.Memory.Span);
public override bool Equals(object? obj)
=> obj is Vector vector && Equals(vector);
public static bool operator ==(Vector? x, Vector? y)
=> (x is null && y is null) || (x is not null && x.Equals(y));
public static bool operator !=(Vector? x, Vector? y) => !(x == y);
public override int GetHashCode()
{
var hashCode = new HashCode();
var span = Memory.Span;
for (var i = 0; i < span.Length; i++)
hashCode.Add(span[i]);
return hashCode.ToHashCode();
}
}