1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// this module is transparently re-exported by its parent `graph`
// It defines implementation of Graph and MutableGraph for existing types.

use std::collections::HashSet;
use std::hash::{BuildHasher, Hash};

use resiter::oks::*;

use super::*;
use crate::error::*;
use crate::term::*;
use crate::triple::stream::AsTripleSource;
use crate::triple::*;

impl<'a, T> Graph<'a> for [T]
where
    T: Triple<'a> + 'a,
{
    type Triple = &'a T;
    type Error = Never;

    #[inline]
    fn triples(&'a self) -> GTripleSource<Self> {
        Box::new(<[T]>::iter(self).as_triple_source())
    }
}

impl<'a, T> Graph<'a> for Vec<T>
where
    T: Triple<'a> + 'a,
{
    type Triple = &'a T;
    type Error = Never;

    #[inline]
    fn triples(&'a self) -> GTripleSource<Self> {
        Box::new(<[T]>::iter(self).as_triple_source())
    }
}

impl MutableGraph for Vec<[BoxTerm; 3]> {
    type MutationError = Never;

    fn insert<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        let s = BoxTerm::from(s);
        let p = BoxTerm::from(p);
        let o = BoxTerm::from(o);
        self.push([s, p, o]);
        Ok(true)
    }
    fn remove<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        let i = self
            .triples()
            .oks()
            .position(|t| s == t.s() && p == t.p() && o == t.o());
        if let Some(i) = i {
            self.swap_remove(i);
            Ok(true)
        } else {
            Ok(false)
        }
    }
}

impl<'a, T, BH> Graph<'a> for HashSet<T, BH>
where
    T: Eq + Hash + Triple<'a> + 'a,
    BH: BuildHasher,
{
    type Triple = &'a T;
    type Error = Never;

    #[inline]
    fn triples(&'a self) -> GTripleSource<Self> {
        Box::from(self.iter().as_triple_source())
    }
}

impl<BH> MutableGraph for HashSet<[BoxTerm; 3], BH>
where
    BH: BuildHasher,
{
    type MutationError = Never;

    fn insert<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        let s = BoxTerm::from(s);
        let p = BoxTerm::from(p);
        let o = BoxTerm::from(o);
        Ok(HashSet::insert(self, [s, p, o]))
    }
    fn remove<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
    where
        T: TermData,
        U: TermData,
        V: TermData,
    {
        let s = BoxTerm::from(s);
        let p = BoxTerm::from(p);
        let o = BoxTerm::from(o);
        Ok(HashSet::remove(self, &[s, p, o]))
    }
}

impl<'a, T, S: ::std::hash::BuildHasher> SetGraph for HashSet<T, S> where
    T: Eq + Hash + Triple<'a> + 'a
{
}

#[cfg(test)]
mod test {
    use resiter::oks::*;
    use std::collections::HashSet;

    use crate::graph::*;
    use crate::ns::*;
    use crate::term::BoxTerm;

    #[test]
    fn test_slice() {
        let g = [
            [rdf::type_, rdf::type_, rdf::Property],
            [rdf::Property, rdf::type_, rdfs::Class],
            [rdfs::Class, rdf::type_, rdfs::Class],
        ];
        let len = g.triples().oks().count();
        assert_eq!(len, 3);
        let len = g.triples_with_o(&rdfs::Class).oks().count();
        assert_eq!(len, 2);
    }

    type VecAsGraph = Vec<[BoxTerm; 3]>;
    test_graph_impl!(vec, VecAsGraph, false);

    type HashSetAsGraph = HashSet<[BoxTerm; 3]>;
    test_graph_impl!(hashset, HashSetAsGraph);
}