-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define equality/GetHashCode for the Vector class #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,42 @@ | |
|
||
namespace Pgvector; | ||
|
||
public class Vector | ||
public class Vector : IEquatable<Vector> | ||
{ | ||
internal ReadOnlyMemory<float> vec; | ||
public ReadOnlyMemory<float> Memory { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exposes the ReadOnlyMemory publicly, so that users (as well as the plugin's converter) can access the data without copying (which is what ToArray does below) |
||
|
||
public Vector(ReadOnlyMemory<float> v) | ||
=> vec = v; | ||
|
||
public Vector(float[] v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that arrays are implicitly convertible to Memory/ReadOnlyMemory, so there's no need for this constructor. |
||
=> vec = new ReadOnlyMemory<float>(v); | ||
=> Memory = v; | ||
|
||
public Vector(string s) | ||
=> new Vector(Array.ConvertAll(s.Substring(1, s.Length - 2).Split(','), v => float.Parse(v, CultureInfo.InvariantCulture))); | ||
|
||
public override string ToString() | ||
=> string.Concat("[", string.Join(",", vec.ToArray().Select(v => v.ToString(CultureInfo.InvariantCulture))), "]"); | ||
=> string.Concat("[", string.Join(",", Memory.ToArray().Select(v => v.ToString(CultureInfo.InvariantCulture))), "]"); | ||
|
||
public float[] ToArray() | ||
=> vec.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.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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This accesses the data directly, removing an additional memory allocation and copy.