-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/*! | ||
* pgvector-cpp v0.1.1 | ||
* https://github.com/pgvector/pgvector-cpp | ||
* MIT License | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ostream> | ||
#include <vector> | ||
|
||
namespace pgvector { | ||
class SparseVector { | ||
public: | ||
SparseVector() = default; | ||
|
||
SparseVector(int dimensions, const std::vector<int>& indices, const std::vector<float>& values) { | ||
if (values.size() != indices.size()) { | ||
throw std::invalid_argument("indices and values must be the same length"); | ||
} | ||
dimensions_ = dimensions; | ||
indices_ = indices; | ||
values_ = values; | ||
} | ||
|
||
SparseVector(const std::vector<float>& value) { | ||
dimensions_ = value.size(); | ||
for (size_t i = 0; i < value.size(); i++) { | ||
float v = value[i]; | ||
if (v != 0) { | ||
indices_.push_back(i); | ||
values_.push_back(v); | ||
} | ||
} | ||
} | ||
|
||
int dimensions() const { | ||
return dimensions_; | ||
} | ||
|
||
const std::vector<int>& indices() const { | ||
return indices_; | ||
} | ||
|
||
const std::vector<float>& values() const { | ||
return values_; | ||
} | ||
|
||
friend bool operator==(const SparseVector& lhs, const SparseVector& rhs) { | ||
return lhs.dimensions_ == rhs.dimensions_ && lhs.indices_ == rhs.indices_ && lhs.values_ == rhs.values_; | ||
} | ||
|
||
friend std::ostream& operator<<(std::ostream& os, const SparseVector& value) { | ||
os << "{"; | ||
for (size_t i = 0; i < value.indices_.size(); i++) { | ||
if (i > 0) { | ||
os << ","; | ||
} | ||
os << value.indices_[i] + 1; | ||
os << ":"; | ||
os << value.values_[i]; | ||
} | ||
os << "}/"; | ||
os << value.dimensions_; | ||
return os; | ||
} | ||
|
||
private: | ||
int dimensions_; | ||
std::vector<int> indices_; | ||
std::vector<float> values_; | ||
}; | ||
} // namespace pgvector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters