I have to use a sparse vector for some things in my current project. But, since I am not in charge of the project, I can not use whatever outside libraries I want. I only have STL and OpenCV available.
I have looked through several stackoverflow answered questions, but they either focus on a specific approach, comparison of a limited number of approaches (2) and outside libraries when they deal specifically with sparse vectors. There is also some excellent ideas for implementing a sparse matrix.
What I want is specifically a sparse vector (the indexes will always be in 1 dimension, the data is not relevant for this question). I would like something that will not be a project on it's own to implement, but could be used for more-than-demonstrative-purposes (e.g. I want to achieve a decent speed and not too much of a memory overhead) and hopefully re-used later. The options I have considered included:
- adapting the OpenCV implementation of SparseMat to my needs
- using a
std::map
to store the values (or maybe making a very simple wrapper that would return a default value in case of indexing a zero-element) - using a
std::vector< std::pair < int , data_type > >
where I could store the index and the data in thestd::pair
elements
Is any of these solutions better/worse for the general purpose use as a sparse vector? I know every approach to everything has it's ups and downs, but argumented suggestions on which approach to choose would be very much appreciated. Also, recommending an approach I have not considered would be more than welcome if anyone thinks he has a better suggestion.
The usage in my specific case is as follows:
- the vector will most likely not be modified after it is created (right now I don't see any need for this, but I can not guarantee 100% that it will not come up)
- a most common operation is anticipated to be dot product of two such vectors (so, more-or-less accessing the elements in a linear sequential way)
- only lookup I can foresee right now is (maybe) checking weather a certain element is a zero-element or not
- there will be around 500 non-zero elements expected
- in short, most of the time the sparse vector would be used as the mathematical concept of a vector (a multi-dimensional point) without much need to examine each coordinate separately
Still, as I have written in my original question, I would like suggestions for a general-purpose Sparse Vector implementation.