Struct TagVec

Source
pub struct TagVec<T, F = u32>
where T: Eq + Hash + Clone, F: BitField,
{ /* private fields */ }
Expand description

This is the main star of this crate. It is an efficient model of a vector of elements, where each element is just a set of tags.

This datatype is intended to handle requests for a huge set of elements whose tags fulfill a requirement, i.e. “all elements with the tag ‘fruit’ but not with the tag ‘apple’.

It is expected that the elements share a lot of tags, i.e. there are a lot fewer tags than elements.

It is not optimized for simply iterating over the tags of each element, hence it is not recommended to do such a thing with this datatype too much.

Implementations§

Source§

impl<T: Eq + Hash + Clone, F: BitField> TagVec<T, F>

Source

pub fn new() -> TagVec<T, F>

Creates an empty, new bit vector.

Source

pub fn len(&self) -> usize

The number of elements in the TagVec

Source

pub fn push<'a, I, Q>(&mut self, tags: I)
where I: IntoIterator<Item = &'a Q>, Q: ?Sized + Hash + Eq + 'a, T: From<&'a Q> + Borrow<Q>,

Pushes a new element onto the bitvec, where the new element is defined as an iterator of tags(borrows of tags specifically)

And OMG the generics on this function are crazy

Source

pub fn query<'a, Q>(&'a self, expr: Expression<'a, Q>) -> Query<'a, F>
where Q: ?Sized + Hash + Eq + 'a, T: Borrow<Q>,

Iterates over all elements who fulfill the given expression. The behind the scenes of this function are complete and utter black magic code, and that code is indeed very strange. Nonetheless, the use of this function is not strange, and in fact quite intuitive

use tag_vec::TagVec;

// Make it easier to construct an expression
use tag_vec::expressions::*;

// Construct a tag_vec
let mut tag_vec: TagVec<String> = TagVec::new();
tag_vec.push(vec!["hello", "world"]);
tag_vec.push(vec!["rust", "is", "good"]);
tag_vec.push(vec!["hello", "is", "good"]);
tag_vec.push(vec!["hello", "rust"]);

// Query something
let mut query = tag_vec.query(tag("hello"));
// The first element to contain the tag "hello" is number 0
assert_eq!(query.next(), Some(0)); 
// ... and so on
assert_eq!(query.next(), Some(2)); 
assert_eq!(query.next(), Some(3)); 
assert_eq!(query.next(), None); // Oops, we ran out!

// Query something more complicated
let mut query = tag_vec.query(and(tag("rust"), tag("good")));
// Element "1" is the only element with both the "rust" and "good" tags
assert_eq!(query.next(), Some(1)); 
assert_eq!(query.next(), None);
Source

pub fn iter_element<'a>(&'a self, index: usize) -> IterElement<'a, T, F>

Iterates over each tag of an element(an element is considered to be its tags). The iterator is unordered, so be careful. Will panic if the index is out of bounds.

Examples:

use tag_vec::TagVec;
// It is good to give the type of the key to
// the type, as it may be difficult for the compiler
// to infer it
let mut tag_vec: TagVec<String> = TagVec::new();
tag_vec.push(vec!["hello", "world"]);

// We should find a "hello" tag but not a "hi" tag
// in the iterator
let tags = tag_vec.iter_element(0);
assert!(tags.clone().any(|v| *v == "hello"));
assert!(!tags.clone().any(|v| *v == "hi"));

Auto Trait Implementations§

§

impl<T, F> Freeze for TagVec<T, F>

§

impl<T, F> RefUnwindSafe for TagVec<T, F>

§

impl<T, F> Send for TagVec<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for TagVec<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for TagVec<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnwindSafe for TagVec<T, F>
where T: UnwindSafe, F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.