remove extern crate declaration and fix outfall (macros now need to be imported into scope)
using use declarations in main.rs so that use paths don't need to be updated as well, this will be done in a later commit
This commit is contained in:
474
src/indexing.rs
474
src/indexing.rs
@@ -1,4 +1,5 @@
|
||||
use crate::prolog_parser_rebis::ast::*;
|
||||
use crate::prolog_parser_rebis::clause_name;
|
||||
use crate::prolog_parser_rebis::tabled_rc::*;
|
||||
|
||||
use crate::forms::*;
|
||||
@@ -43,14 +44,10 @@ impl OptArgIndexKey {
|
||||
#[inline]
|
||||
fn has_key_type(&self, key_type: OptArgIndexKeyType) -> bool {
|
||||
match (self, key_type) {
|
||||
(OptArgIndexKey::Constant(..), OptArgIndexKeyType::Constant) |
|
||||
(OptArgIndexKey::Structure(..), OptArgIndexKeyType::Structure) |
|
||||
(OptArgIndexKey::List(..), OptArgIndexKeyType::List) => {
|
||||
true
|
||||
}
|
||||
_ => {
|
||||
false
|
||||
}
|
||||
(OptArgIndexKey::Constant(..), OptArgIndexKeyType::Constant)
|
||||
| (OptArgIndexKey::Structure(..), OptArgIndexKeyType::Structure)
|
||||
| (OptArgIndexKey::List(..), OptArgIndexKeyType::List) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,16 +90,20 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
indexing_code: &'a mut Vec<IndexingLine>,
|
||||
append_or_prepend: AppendOrPrepend,
|
||||
) -> Self {
|
||||
Self { skeleton, indexing_code, offset: 0, append_or_prepend }
|
||||
Self {
|
||||
skeleton,
|
||||
indexing_code,
|
||||
offset: 0,
|
||||
append_or_prepend,
|
||||
}
|
||||
}
|
||||
|
||||
fn internalize_constant(&mut self, constant_ptr: IndexingCodePtr) {
|
||||
let constant_key =
|
||||
search_skeleton_for_first_key_type(
|
||||
self.skeleton,
|
||||
OptArgIndexKeyType::Constant,
|
||||
self.append_or_prepend,
|
||||
);
|
||||
fn internalize_constant(&mut self, constant_ptr: IndexingCodePtr) {
|
||||
let constant_key = search_skeleton_for_first_key_type(
|
||||
self.skeleton,
|
||||
OptArgIndexKeyType::Constant,
|
||||
self.append_or_prepend,
|
||||
);
|
||||
|
||||
let mut constants = IndexMap::new();
|
||||
|
||||
@@ -117,7 +118,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
|
||||
if let IndexingCodePtr::Internal(_) = constant_ptr {
|
||||
self.indexing_code.push(IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(constants)
|
||||
IndexingInstruction::SwitchOnConstant(constants),
|
||||
));
|
||||
|
||||
let last_index = self.indexing_code.len() - 1;
|
||||
@@ -126,34 +127,39 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
self.offset = self.indexing_code.len();
|
||||
|
||||
self.indexing_code.push(IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(constants)
|
||||
IndexingInstruction::SwitchOnConstant(constants),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn add_indexed_choice_for_constant(&mut self, external: usize, constant: Constant, index: usize)
|
||||
{
|
||||
let third_level_index =
|
||||
if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(external),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(external)
|
||||
]
|
||||
};
|
||||
fn add_indexed_choice_for_constant(
|
||||
&mut self,
|
||||
external: usize,
|
||||
constant: Constant,
|
||||
index: usize,
|
||||
) {
|
||||
let third_level_index = if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(external),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(external)
|
||||
]
|
||||
};
|
||||
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
self.indexing_code.push(IndexingLine::IndexedChoice(third_level_index));
|
||||
self.indexing_code
|
||||
.push(IndexingLine::IndexedChoice(third_level_index));
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(ref mut constants)
|
||||
) => {
|
||||
constants.insert(constant, IndexingCodePtr::Internal(indexing_code_len - self.offset));
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(ref mut constants)) => {
|
||||
constants.insert(
|
||||
constant,
|
||||
IndexingCodePtr::Internal(indexing_code_len - self.offset),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -164,10 +170,11 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
fn extend_indexed_choice(&mut self, index: usize) {
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::IndexedChoice(ref mut indexed_choice_instrs)
|
||||
if self.append_or_prepend.is_append() => {
|
||||
uncap_choice_seq_with_trust(indexed_choice_instrs);
|
||||
indexed_choice_instrs.push_back(IndexedChoiceInstruction::Trust(index));
|
||||
}
|
||||
if self.append_or_prepend.is_append() =>
|
||||
{
|
||||
uncap_choice_seq_with_trust(indexed_choice_instrs);
|
||||
indexed_choice_instrs.push_back(IndexedChoiceInstruction::Trust(index));
|
||||
}
|
||||
IndexingLine::IndexedChoice(ref mut indexed_choice_instrs) => {
|
||||
uncap_choice_seq_with_try(indexed_choice_instrs);
|
||||
indexed_choice_instrs.push_front(IndexedChoiceInstruction::Try(index));
|
||||
@@ -188,9 +195,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)) => {
|
||||
match *c {
|
||||
IndexingCodePtr::Fail => {
|
||||
*c = IndexingCodePtr::External(index);
|
||||
@@ -203,7 +208,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
*c = IndexingCodePtr::Internal(indexing_code_len);
|
||||
|
||||
self.indexing_code.push(IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(constants)
|
||||
IndexingInstruction::SwitchOnConstant(constants),
|
||||
));
|
||||
|
||||
self.offset = indexing_code_len;
|
||||
@@ -213,12 +218,11 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(constants)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(constants)) => {
|
||||
match constants.get(&overlapping_constant).cloned() {
|
||||
None | Some(IndexingCodePtr::Fail) => {
|
||||
constants.insert(overlapping_constant, IndexingCodePtr::External(index));
|
||||
constants
|
||||
.insert(overlapping_constant, IndexingCodePtr::External(index));
|
||||
}
|
||||
Some(IndexingCodePtr::External(o)) => {
|
||||
self.add_indexed_choice_for_constant(o, overlapping_constant, index);
|
||||
@@ -232,9 +236,9 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
break;
|
||||
}
|
||||
IndexingLine::IndexedChoice(_) => {
|
||||
self.internalize_constant(
|
||||
IndexingCodePtr::Internal(indexing_code_len - self.offset),
|
||||
);
|
||||
self.internalize_constant(IndexingCodePtr::Internal(
|
||||
indexing_code_len - self.offset,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -248,9 +252,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)) => {
|
||||
match *c {
|
||||
IndexingCodePtr::Fail => {
|
||||
*c = IndexingCodePtr::External(index);
|
||||
@@ -265,9 +267,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(constants)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(constants)) => {
|
||||
match constants.get(&constant).cloned() {
|
||||
None | Some(IndexingCodePtr::Fail) => {
|
||||
constants.insert(constant, IndexingCodePtr::External(index));
|
||||
@@ -284,9 +284,9 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
break;
|
||||
}
|
||||
IndexingLine::IndexedChoice(_) => {
|
||||
self.internalize_constant(
|
||||
IndexingCodePtr::Internal(indexing_code_len - self.offset),
|
||||
);
|
||||
self.internalize_constant(IndexingCodePtr::Internal(
|
||||
indexing_code_len - self.offset,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -295,13 +295,12 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn internalize_structure(&mut self, structure_ptr: IndexingCodePtr) {
|
||||
let structure_key =
|
||||
search_skeleton_for_first_key_type(
|
||||
self.skeleton,
|
||||
OptArgIndexKeyType::Structure,
|
||||
self.append_or_prepend,
|
||||
);
|
||||
fn internalize_structure(&mut self, structure_ptr: IndexingCodePtr) {
|
||||
let structure_key = search_skeleton_for_first_key_type(
|
||||
self.skeleton,
|
||||
OptArgIndexKeyType::Structure,
|
||||
self.append_or_prepend,
|
||||
);
|
||||
|
||||
let mut structures = IndexMap::new();
|
||||
|
||||
@@ -316,7 +315,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
|
||||
if let IndexingCodePtr::Internal(_) = structure_ptr {
|
||||
self.indexing_code.push(IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(structures)
|
||||
IndexingInstruction::SwitchOnStructure(structures),
|
||||
));
|
||||
|
||||
let last_index = self.indexing_code.len() - 1;
|
||||
@@ -325,34 +324,39 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
self.offset = self.indexing_code.len();
|
||||
|
||||
self.indexing_code.push(IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(structures)
|
||||
IndexingInstruction::SwitchOnStructure(structures),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn add_indexed_choice_for_structure(&mut self, external: usize, key: PredicateKey, index: usize)
|
||||
{
|
||||
let third_level_index =
|
||||
if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(external),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(external)
|
||||
]
|
||||
};
|
||||
fn add_indexed_choice_for_structure(
|
||||
&mut self,
|
||||
external: usize,
|
||||
key: PredicateKey,
|
||||
index: usize,
|
||||
) {
|
||||
let third_level_index = if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(external),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(external)
|
||||
]
|
||||
};
|
||||
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
self.indexing_code.push(IndexingLine::IndexedChoice(third_level_index));
|
||||
self.indexing_code
|
||||
.push(IndexingLine::IndexedChoice(third_level_index));
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(ref mut structures)
|
||||
) => {
|
||||
structures.insert(key, IndexingCodePtr::Internal(indexing_code_len - self.offset));
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref mut structures)) => {
|
||||
structures.insert(
|
||||
key,
|
||||
IndexingCodePtr::Internal(indexing_code_len - self.offset),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -365,26 +369,26 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, _, ref mut s)
|
||||
) => {
|
||||
match *s {
|
||||
IndexingCodePtr::Fail => {
|
||||
*s = IndexingCodePtr::External(index);
|
||||
break;
|
||||
}
|
||||
IndexingCodePtr::External(o) => {
|
||||
*s = IndexingCodePtr::Internal(indexing_code_len - self.offset);
|
||||
self.internalize_structure(IndexingCodePtr::External(o));
|
||||
}
|
||||
IndexingCodePtr::Internal(o) => {
|
||||
self.offset += o;
|
||||
}
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
ref mut s,
|
||||
)) => match *s {
|
||||
IndexingCodePtr::Fail => {
|
||||
*s = IndexingCodePtr::External(index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(structures)
|
||||
) => {
|
||||
IndexingCodePtr::External(o) => {
|
||||
*s = IndexingCodePtr::Internal(indexing_code_len - self.offset);
|
||||
self.internalize_structure(IndexingCodePtr::External(o));
|
||||
}
|
||||
IndexingCodePtr::Internal(o) => {
|
||||
self.offset += o;
|
||||
}
|
||||
},
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(structures)) => {
|
||||
match structures.get(&key).cloned() {
|
||||
None | Some(IndexingCodePtr::Fail) => {
|
||||
structures.insert(key, IndexingCodePtr::External(index));
|
||||
@@ -404,9 +408,9 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
// replace this value, at self.offset, with
|
||||
// SwitchOnStructures, and swap this IndexedChoice
|
||||
// vector to the end of self.indexing_code.
|
||||
self.internalize_structure(
|
||||
IndexingCodePtr::Internal(indexing_code_len - self.offset),
|
||||
);
|
||||
self.internalize_structure(IndexingCodePtr::Internal(
|
||||
indexing_code_len - self.offset,
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
@@ -419,9 +423,7 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
let indexing_code_len = self.indexing_code.len();
|
||||
|
||||
match &mut self.indexing_code[self.offset] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, ref mut l, _)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, _, ref mut l, _)) => {
|
||||
match *l {
|
||||
IndexingCodePtr::Fail => {
|
||||
*l = IndexingCodePtr::External(index);
|
||||
@@ -429,22 +431,20 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
IndexingCodePtr::External(o) => {
|
||||
*l = IndexingCodePtr::Internal(indexing_code_len - self.offset);
|
||||
|
||||
let third_level_index =
|
||||
if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(o),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(o)
|
||||
]
|
||||
};
|
||||
let third_level_index = if self.append_or_prepend.is_append() {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(o),
|
||||
IndexedChoiceInstruction::Trust(index)
|
||||
]
|
||||
} else {
|
||||
sdeq![
|
||||
IndexedChoiceInstruction::Try(index),
|
||||
IndexedChoiceInstruction::Trust(o)
|
||||
]
|
||||
};
|
||||
|
||||
self.indexing_code.push(
|
||||
IndexingLine::IndexedChoice(third_level_index),
|
||||
);
|
||||
self.indexing_code
|
||||
.push(IndexingLine::IndexedChoice(third_level_index));
|
||||
}
|
||||
IndexingCodePtr::Internal(o) => {
|
||||
self.offset += o;
|
||||
@@ -462,24 +462,16 @@ impl<'a> IndexingCodeMergingPtr<'a> {
|
||||
pub fn merge_clause_index(
|
||||
target_indexing_code: &mut Vec<IndexingLine>,
|
||||
skeleton: &mut [ClauseIndexInfo], // the clause to be merged is the last element in the skeleton.
|
||||
new_clause_loc: usize, // the absolute location of the new clause in the code vector.
|
||||
new_clause_loc: usize, // the absolute location of the new clause in the code vector.
|
||||
append_or_prepend: AppendOrPrepend,
|
||||
) {
|
||||
let opt_arg_index_key =
|
||||
match append_or_prepend {
|
||||
AppendOrPrepend::Append => {
|
||||
skeleton.last_mut().unwrap().opt_arg_index_key.take()
|
||||
}
|
||||
AppendOrPrepend::Prepend => {
|
||||
skeleton.first_mut().unwrap().opt_arg_index_key.take()
|
||||
}
|
||||
};
|
||||
let opt_arg_index_key = match append_or_prepend {
|
||||
AppendOrPrepend::Append => skeleton.last_mut().unwrap().opt_arg_index_key.take(),
|
||||
AppendOrPrepend::Prepend => skeleton.first_mut().unwrap().opt_arg_index_key.take(),
|
||||
};
|
||||
|
||||
let mut merging_ptr = IndexingCodeMergingPtr::new(
|
||||
skeleton,
|
||||
target_indexing_code,
|
||||
append_or_prepend,
|
||||
);
|
||||
let mut merging_ptr =
|
||||
IndexingCodeMergingPtr::new(skeleton, target_indexing_code, append_or_prepend);
|
||||
|
||||
match &opt_arg_index_key {
|
||||
OptArgIndexKey::Constant(_, index_loc, ref constant, ref overlapping_constants) => {
|
||||
@@ -488,7 +480,9 @@ pub fn merge_clause_index(
|
||||
|
||||
for overlapping_constant in overlapping_constants {
|
||||
merging_ptr.index_overlapping_constant(
|
||||
constant, overlapping_constant.clone(), offset,
|
||||
constant,
|
||||
overlapping_constant.clone(),
|
||||
offset,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -514,10 +508,7 @@ pub fn merge_clause_index(
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn remove_instruction_with_offset(
|
||||
code: &mut SliceDeque<IndexedChoiceInstruction>,
|
||||
offset: usize,
|
||||
) {
|
||||
fn remove_instruction_with_offset(code: &mut SliceDeque<IndexedChoiceInstruction>, offset: usize) {
|
||||
for (index, line) in code.iter().enumerate() {
|
||||
if offset == line.offset() {
|
||||
code.remove(index);
|
||||
@@ -537,9 +528,7 @@ pub fn remove_constant_indices(
|
||||
let iter = once(constant).chain(overlapping_constants.iter());
|
||||
|
||||
match &mut indexing_code[index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)) => {
|
||||
match *c {
|
||||
IndexingCodePtr::External(_) => {
|
||||
*c = IndexingCodePtr::Fail;
|
||||
@@ -560,12 +549,13 @@ pub fn remove_constant_indices(
|
||||
|
||||
let mut constants_index = 0;
|
||||
|
||||
for constant in iter { // (constant, index_loc) in iter.zip(index_locs.iter()) {
|
||||
for constant in iter {
|
||||
// (constant, index_loc) in iter.zip(index_locs.iter()) {
|
||||
loop {
|
||||
match &mut indexing_code[index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(ref mut constants)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(
|
||||
ref mut constants,
|
||||
)) => {
|
||||
constants_index = index;
|
||||
|
||||
match constants.get(constant).cloned() {
|
||||
@@ -586,18 +576,21 @@ pub fn remove_constant_indices(
|
||||
|
||||
if indexed_choice_instrs.len() == 1 {
|
||||
let ext = IndexingCodePtr::External(
|
||||
indexed_choice_instrs.pop_back().unwrap().offset()
|
||||
indexed_choice_instrs.pop_back().unwrap().offset(),
|
||||
);
|
||||
|
||||
match &mut indexing_code[constants_index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(
|
||||
_,
|
||||
_,
|
||||
ref mut c,
|
||||
..,
|
||||
)) => {
|
||||
*c = ext;
|
||||
}
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(ref mut constants)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(
|
||||
ref mut constants,
|
||||
)) => {
|
||||
constants.insert(constant.clone(), ext);
|
||||
}
|
||||
_ => {
|
||||
@@ -616,13 +609,11 @@ pub fn remove_constant_indices(
|
||||
}
|
||||
|
||||
match &indexing_code[constants_index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnConstant(ref constants)
|
||||
) if constants.is_empty() => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnConstant(ref constants))
|
||||
if constants.is_empty() =>
|
||||
{
|
||||
match &mut indexing_code[0] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, ref mut c, ..)) => {
|
||||
*c = IndexingCodePtr::Fail;
|
||||
}
|
||||
_ => {
|
||||
@@ -630,8 +621,7 @@ pub fn remove_constant_indices(
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -644,9 +634,7 @@ pub fn remove_structure_index(
|
||||
let mut index = 0;
|
||||
|
||||
match &mut indexing_code[index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, _, ref mut s)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, _, _, ref mut s)) => {
|
||||
match *s {
|
||||
IndexingCodePtr::External(_) => {
|
||||
*s = IndexingCodePtr::Fail;
|
||||
@@ -669,9 +657,7 @@ pub fn remove_structure_index(
|
||||
|
||||
loop {
|
||||
match &mut indexing_code[index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(ref mut structures)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref mut structures)) => {
|
||||
structures_index = index;
|
||||
|
||||
match structures.get(&(name.clone(), arity)).cloned() {
|
||||
@@ -692,18 +678,22 @@ pub fn remove_structure_index(
|
||||
|
||||
if indexed_choice_instrs.len() == 1 {
|
||||
let ext = IndexingCodePtr::External(
|
||||
indexed_choice_instrs.pop_back().unwrap().offset()
|
||||
indexed_choice_instrs.pop_back().unwrap().offset(),
|
||||
);
|
||||
|
||||
match &mut indexing_code[structures_index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, _, ref mut s)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
ref mut s,
|
||||
)) => {
|
||||
*s = ext;
|
||||
}
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(ref mut structures)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(
|
||||
ref mut structures,
|
||||
)) => {
|
||||
structures.insert((name.clone(), arity), ext);
|
||||
}
|
||||
_ => {
|
||||
@@ -721,13 +711,17 @@ pub fn remove_structure_index(
|
||||
}
|
||||
|
||||
match &indexing_code[structures_index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnStructure(ref structures)
|
||||
) if structures.is_empty() => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnStructure(ref structures))
|
||||
if structures.is_empty() =>
|
||||
{
|
||||
match &mut indexing_code[0] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, _, ref mut s)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
ref mut s,
|
||||
)) => {
|
||||
*s = IndexingCodePtr::Fail;
|
||||
}
|
||||
_ => {
|
||||
@@ -735,21 +729,15 @@ pub fn remove_structure_index(
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_list_index(
|
||||
indexing_code: &mut Vec<IndexingLine>,
|
||||
offset: usize,
|
||||
) {
|
||||
pub fn remove_list_index(indexing_code: &mut Vec<IndexingLine>, offset: usize) {
|
||||
let mut index = 0;
|
||||
|
||||
match &mut indexing_code[index] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, ref mut l, _)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, _, _, ref mut l, _)) => {
|
||||
match *l {
|
||||
IndexingCodePtr::External(_) => {
|
||||
*l = IndexingCodePtr::Fail;
|
||||
@@ -773,14 +761,17 @@ pub fn remove_list_index(
|
||||
remove_instruction_with_offset(indexed_choice_instrs, offset);
|
||||
|
||||
if indexed_choice_instrs.len() == 1 {
|
||||
let ext = IndexingCodePtr::External(
|
||||
indexed_choice_instrs.pop_back().unwrap().offset()
|
||||
);
|
||||
let ext =
|
||||
IndexingCodePtr::External(indexed_choice_instrs.pop_back().unwrap().offset());
|
||||
|
||||
match &mut indexing_code[0] {
|
||||
IndexingLine::Indexing(
|
||||
IndexingInstruction::SwitchOnTerm(_, _, _, ref mut l, _)
|
||||
) => {
|
||||
IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(
|
||||
_,
|
||||
_,
|
||||
_,
|
||||
ref mut l,
|
||||
_,
|
||||
)) => {
|
||||
*l = ext;
|
||||
}
|
||||
_ => {
|
||||
@@ -836,8 +827,7 @@ pub fn remove_index(
|
||||
fn second_level_index<IndexKey: Eq + Hash>(
|
||||
indices: IndexMap<IndexKey, SliceDeque<IndexedChoiceInstruction>>,
|
||||
prelude: &mut SliceDeque<IndexingLine>,
|
||||
) -> IndexMap<IndexKey, IndexingCodePtr>
|
||||
{
|
||||
) -> IndexMap<IndexKey, IndexingCodePtr> {
|
||||
let mut index_locs = IndexMap::new();
|
||||
|
||||
for (key, mut code) in indices.into_iter() {
|
||||
@@ -868,10 +858,11 @@ fn switch_on<IndexKey: Eq + Hash>(
|
||||
|
||||
IndexingCodePtr::Internal(1)
|
||||
} else {
|
||||
index.into_iter()
|
||||
.next()
|
||||
.map(|(_, v)| v)
|
||||
.unwrap_or(IndexingCodePtr::Fail)
|
||||
index
|
||||
.into_iter()
|
||||
.next()
|
||||
.map(|(_, v)| v)
|
||||
.unwrap_or(IndexingCodePtr::Fail)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -884,9 +875,10 @@ fn switch_on_list(
|
||||
prelude.push_back(IndexingLine::from(lists));
|
||||
IndexingCodePtr::Internal(1)
|
||||
} else {
|
||||
lists.first()
|
||||
.map(|i| IndexingCodePtr::External(i.offset()))
|
||||
.unwrap_or(IndexingCodePtr::Fail)
|
||||
lists
|
||||
.first()
|
||||
.map(|i| IndexingCodePtr::External(i.offset()))
|
||||
.unwrap_or(IndexingCodePtr::Fail)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -978,8 +970,7 @@ pub fn constant_key_alternatives(constant: &Constant, atom_tbl: TabledData<Atom>
|
||||
constants.push(Constant::Fixnum(n));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
constants
|
||||
@@ -1001,7 +992,7 @@ impl CodeOffsets {
|
||||
constants: IndexMap::new(),
|
||||
lists: sdeq![],
|
||||
structures: IndexMap::new(),
|
||||
optimal_index
|
||||
optimal_index,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1011,20 +1002,15 @@ impl CodeOffsets {
|
||||
}
|
||||
|
||||
fn index_constant(&mut self, constant: &Constant, index: usize) -> Vec<Constant> {
|
||||
let overlapping_constants =
|
||||
constant_key_alternatives(constant, self.atom_tbl.clone());
|
||||
let overlapping_constants = constant_key_alternatives(constant, self.atom_tbl.clone());
|
||||
|
||||
let code = self.constants
|
||||
.entry(constant.clone())
|
||||
.or_insert(sdeq![]);
|
||||
let code = self.constants.entry(constant.clone()).or_insert(sdeq![]);
|
||||
|
||||
let is_initial_index = code.is_empty();
|
||||
code.push_back(compute_index(is_initial_index, index));
|
||||
|
||||
for constant in &overlapping_constants {
|
||||
let code = self.constants
|
||||
.entry(constant.clone())
|
||||
.or_insert(sdeq![]);
|
||||
let code = self.constants.entry(constant.clone()).or_insert(sdeq![]);
|
||||
|
||||
let is_initial_index = code.is_empty();
|
||||
let index = compute_index(is_initial_index, index);
|
||||
@@ -1062,25 +1048,21 @@ impl CodeOffsets {
|
||||
self.index_structure(name, terms.len(), index);
|
||||
}
|
||||
&Term::Cons(..) | &Term::Constant(_, Constant::String(_)) => {
|
||||
clause_index_info.opt_arg_index_key =
|
||||
OptArgIndexKey::List(self.optimal_index, 0);
|
||||
clause_index_info.opt_arg_index_key = OptArgIndexKey::List(self.optimal_index, 0);
|
||||
|
||||
self.index_list(index);
|
||||
}
|
||||
&Term::Constant(_, ref constant) => {
|
||||
let overlapping_constants =
|
||||
self.index_constant(constant, index);
|
||||
let overlapping_constants = self.index_constant(constant, index);
|
||||
|
||||
clause_index_info.opt_arg_index_key =
|
||||
OptArgIndexKey::Constant(
|
||||
self.optimal_index,
|
||||
0,
|
||||
constant.clone(),
|
||||
overlapping_constants,
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
clause_index_info.opt_arg_index_key = OptArgIndexKey::Constant(
|
||||
self.optimal_index,
|
||||
0,
|
||||
constant.clone(),
|
||||
overlapping_constants,
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1117,8 +1099,7 @@ impl CodeOffsets {
|
||||
IndexingCodePtr::Internal(ref mut i) => {
|
||||
*i += con_loc.is_internal() as usize;
|
||||
}
|
||||
_ => {
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
match &mut lst_loc {
|
||||
@@ -1126,15 +1107,18 @@ impl CodeOffsets {
|
||||
*i += con_loc.is_internal() as usize;
|
||||
*i += str_loc.is_internal() as usize;
|
||||
}
|
||||
_ => {
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
let var_offset = 1 + skip_stub_try_me_else as usize;
|
||||
|
||||
prelude.push_front(IndexingLine::from(
|
||||
IndexingInstruction::SwitchOnTerm(self.optimal_index, var_offset, con_loc, lst_loc, str_loc)
|
||||
));
|
||||
prelude.push_front(IndexingLine::from(IndexingInstruction::SwitchOnTerm(
|
||||
self.optimal_index,
|
||||
var_offset,
|
||||
con_loc,
|
||||
lst_loc,
|
||||
str_loc,
|
||||
)));
|
||||
|
||||
prelude.into_iter().collect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user