Skip to content
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

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Pgvector/Npgsql/VectorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ public override void Write(PgWriter writer, Vector value)
if (writer.ShouldFlush(sizeof(ushort) * 2))
writer.Flush();

var vec = value.vec;
var dim = vec.Length;
var span = value.Memory.Span;
Copy link
Contributor Author

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.

var dim = span.Length;
writer.WriteUInt16(Convert.ToUInt16(dim));
writer.WriteUInt16(0);

for (int i = 0; i < dim; i++)
{
if (writer.ShouldFlush(sizeof(float)))
writer.Flush();
writer.WriteFloat(vec.Span[i]);
writer.WriteFloat(span[i]);
}
}

Expand All @@ -76,16 +76,16 @@ public override async ValueTask WriteAsync(
if (writer.ShouldFlush(sizeof(ushort) * 2))
await writer.FlushAsync(cancellationToken);

var vec = value.vec;
var dim = vec.Length;
var memory = value.Memory;
var dim = memory.Length;
writer.WriteUInt16(Convert.ToUInt16(dim));
writer.WriteUInt16(0);

for (int i = 0; i < dim; i++)
{
if (writer.ShouldFlush(sizeof(float)))
await writer.FlushAsync(cancellationToken);
writer.WriteFloat(vec.Span[i]);
writer.WriteFloat(memory.Span[i]);
}
}
}
36 changes: 28 additions & 8 deletions src/Pgvector/Vector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@

namespace Pgvector;

public class Vector
public class Vector : IEquatable<Vector>
{
internal ReadOnlyMemory<float> vec;
public ReadOnlyMemory<float> Memory { get; }
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}
}