distinguish eq from structural_eq on strings

This commit is contained in:
Mark Thom
2018-09-03 00:06:01 -06:00
parent abcb53699a
commit c5fd13dfa0
9 changed files with 80 additions and 63 deletions

View File

@@ -1,5 +1,3 @@
use prolog::tabled_rc::*;
use std::cell::{Cell, Ref, RefCell};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
@@ -17,25 +15,28 @@ impl Hash for StringListWrapper {
// cursor is ignored if the double_quotes flag is set to atom
#[derive(Clone)]
pub struct StringList {
body: TabledRc<StringListWrapper>,
body: Rc<StringListWrapper>,
cursor: usize, // use this to generate a chars() iterator on the fly,
// and skip over the first cursor chars.
expandable: Rc<Cell<bool>>
}
impl Hash for StringList {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(self.borrow().as_str(), self.cursor, self.expandable.get()).hash(state);
}
}
impl PartialOrd for StringList {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.body.cmp(&other.body))
}
}
impl Ord for StringList {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self.expandable.get() && !self.expandable.get() {
Ordering::Greater
@@ -48,8 +49,9 @@ impl Ord for StringList {
}
impl PartialEq for StringList {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.borrow()[self.cursor ..] == other.borrow()[other.cursor ..] && self.expandable == other.expandable
Rc::ptr_eq(&self.body, &other.body)
}
}
@@ -57,8 +59,8 @@ impl Eq for StringList {}
impl StringList {
#[inline]
pub fn new(s: String, expandable: bool, string_tbl: TabledData<StringListWrapper>) -> Self {
let body = TabledRc::new(StringListWrapper(RefCell::new(s)), string_tbl);
pub fn new(s: String, expandable: bool) -> Self {
let body = Rc::new(StringListWrapper(RefCell::new(s)));
StringList {
cursor: 0,
@@ -76,12 +78,12 @@ impl StringList {
pub fn set_expandable(&self) {
self.expandable.set(true);
}
#[inline]
pub fn set_non_expandable(&self) {
self.expandable.set(false);
}
#[inline]
pub fn push_char(&mut self, c: char) -> Self {
if self.expandable.get() {
@@ -101,12 +103,12 @@ impl StringList {
self.body.0.borrow_mut().extend(s.borrow()[s.cursor ..].chars());
self.expandable.set(s.expandable.get());
}
#[inline]
pub fn cursor(&self) -> usize {
self.cursor
}
}
#[inline]
pub fn head(&self) -> Option<char> {
self.borrow()[self.cursor ..].chars().next()
@@ -127,7 +129,7 @@ impl StringList {
pub fn is_empty(&self) -> bool {
self.borrow().len() == self.cursor
}
#[inline]
pub fn borrow(&self) -> Ref<String> {
self.body.0.borrow()