packed vs unpacked vectors in system verilog

This article gives more details about this issue: http://electrosofts.com/systemverilog/arrays.html, especially section 5.2. A packed array is a mechanism for subdividing a vector into subfields which can be conveniently accessed as array elements. Consequently, a packed array is guaranteed to be represented as a contiguous set of bits. An unpacked array may or may not be … Read more

What is the idiomatic way to remove the first N elements in a mutable Vec?

Use drain to remove multiple contiguous elements from a vector at once as efficiently as possible (the implementation uses ptr::copy to move the elements that remain): let mut v = vec![1, 2, 3, 4]; v.drain(0..2); assert!(v == vec![3, 4]); Regarding #2, it is not feasible to avoid shifting the remaining elements. That optimization would require … Read more

Using find_if on a vector of object

That’s not how predicates work. You have to supply either a free function bool Comparator(const MyClass & m) { … }, or build a function object, a class that overloads operator(): struct MyClassComp { explicit MyClassComp(int i) n(i) { } inline bool operator()(const MyClass & m) const { return m.myInt == n; } private: int … Read more

Vector Space Model: Cosine Similarity vs Euclidean Distance

One informal but rather intuitive way to think about this is to consider the 2 components of a vector: direction and magnitude. Direction is the “preference”https://stackoverflow.com/”style”https://stackoverflow.com/”sentiment”https://stackoverflow.com/”latent variable” of the vector, while the magnitude is how strong it is towards that direction. When classifying documents we’d like to categorize them by their overall sentiment, so we … Read more

Accessing the last element of a Vec or a slice

You can use slice::last: fn top(&mut self) -> Option<f64> { self.last().copied() } Option::copied (and Option::cloned) can be used to convert from an Option<&f64> to an Option<f64>, matching the desired function signature. You can also remove the mut from both the implementation and the trait definition.