2017-02-15 00:50:40 +00:00
|
|
|
// Copyright 2016 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::hash::Hash;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
/// An `InternSet` allows to "intern" some potentially large values, maintaining a single value
|
|
|
|
/// instance owned by the `InternSet` and leaving consumers with lightweight ref-counted handles to
|
|
|
|
/// the large owned value. This can avoid expensive clone() operations.
|
|
|
|
///
|
|
|
|
/// In Mentat, such large values might be strings or arbitrary [a v] pairs.
|
|
|
|
///
|
|
|
|
/// See https://en.wikipedia.org/wiki/String_interning for discussion.
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct InternSet<T> where T: Eq + Hash {
|
|
|
|
pub inner: HashSet<Rc<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> InternSet<T> where T: Eq + Hash {
|
|
|
|
pub fn new() -> InternSet<T> {
|
|
|
|
InternSet {
|
|
|
|
inner: HashSet::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 16:23:37 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.inner.len()
|
|
|
|
}
|
|
|
|
|
2017-02-15 00:50:40 +00:00
|
|
|
/// Intern a value, providing a ref-counted handle to the interned value.
|
2017-03-31 16:32:59 +00:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::rc::Rc;
|
|
|
|
/// use mentat_core::intern_set::InternSet;
|
|
|
|
///
|
|
|
|
/// let mut s = InternSet::new();
|
|
|
|
///
|
|
|
|
/// let one = "foo".to_string();
|
|
|
|
/// let two = Rc::new("foo".to_string());
|
|
|
|
///
|
|
|
|
/// let out_one = s.intern(one);
|
|
|
|
/// assert_eq!(out_one, two);
|
|
|
|
/// // assert!(!&out_one.ptr_eq(&two)); // Nightly-only.
|
|
|
|
///
|
|
|
|
/// let out_two = s.intern(two);
|
|
|
|
/// assert_eq!(out_one, out_two);
|
|
|
|
/// assert_eq!(1, s.inner.len());
|
|
|
|
/// // assert!(&out_one.ptr_eq(&out_two)); // Nightly-only.
|
|
|
|
/// ```
|
|
|
|
pub fn intern<R: Into<Rc<T>>>(&mut self, value: R) -> Rc<T> {
|
|
|
|
let key: Rc<T> = value.into();
|
2017-02-15 00:50:40 +00:00
|
|
|
if self.inner.insert(key.clone()) {
|
|
|
|
key
|
|
|
|
} else {
|
|
|
|
self.inner.get(&key).unwrap().clone()
|
|
|
|
}
|
|
|
|
}
|
2018-01-20 03:21:04 +00:00
|
|
|
}
|