tree_automata/
lib.rs
1#![feature(trait_alias)]
2
3extern crate terms;
4
5use std::fmt;
6use std::hash::{Hash, Hasher};
7use std::cmp::{PartialOrd, Ord, Ordering};
8pub use terms::Ranked;
9
10mod state;
11mod label;
12mod language;
13mod inter;
14mod utils;
15pub mod bottom_up;
16pub mod alternating;
17
18pub use state::*;
19pub use label::*;
20pub use language::*;
21pub use inter::*;
22
23pub use utils::*;
24
25#[cfg(not(debug_assertions))]
26pub trait Symbol = Hash + Clone + Eq;
27
28#[cfg(debug_assertions)]
29pub trait Symbol = Hash + Clone + Eq + fmt::Display + fmt::Debug;
30
31pub struct Rank<T>(pub T, pub usize);
32
33impl<T> Ranked for Rank<T> {
34 fn arity(&self) -> usize {
35 self.1
36 }
37}
38
39impl<T: Ord> Ord for Rank<T> {
40 fn cmp(&self, other: &Rank<T>) -> Ordering {
41 match self.1.cmp(&other.1) {
42 Ordering::Equal => self.0.cmp(&other.0),
43 ord => ord
44 }
45 }
46}
47
48impl<T: PartialOrd> PartialOrd for Rank<T> {
49 fn partial_cmp(&self, other: &Rank<T>) -> Option<Ordering> {
50 match self.1.cmp(&other.1) {
51 Ordering::Equal => self.0.partial_cmp(&other.0),
52 ord => Some(ord)
53 }
54 }
55}
56
57impl<T: Eq> Eq for Rank<T> {}
58
59impl<T: PartialEq> PartialEq for Rank<T> {
60 fn eq(&self, other: &Rank<T>) -> bool {
61 self.1 == other.1 && self.0 == other.0
62 }
63}
64
65impl<T: Hash> Hash for Rank<T> {
66 fn hash<H: Hasher>(&self, state: &mut H) {
67 self.0.hash(state);
68 self.1.hash(state);
69 }
70}
71
72impl<T: Copy> Copy for Rank<T> {}
73
74impl<T: Clone> Clone for Rank<T> {
75 fn clone(&self) -> Rank<T> {
76 Rank(self.0.clone(), self.1)
77 }
78}
79
80impl<T: fmt::Display> fmt::Display for Rank<T> {
81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82 write!(f, "{}:{}", self.0, self.1)
83 }
84}
85
86impl<T: fmt::Debug> fmt::Debug for Rank<T> {
87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88 write!(f, "{:?}:{}", self.0, self.1)
89 }
90}
91
92pub trait SortedWith<T> {
94 fn sort(&self) -> &T;
95}
96
97impl<A, B> SortedWith<B> for (A, B) {
98 fn sort(&self) -> &B {
99 &self.1
100 }
101}
102
103pub struct Sorted<X, T>(pub X, pub T);
104
105impl<X, T> Sorted<X, T> {
106 pub fn new(x: X, t: T) -> Sorted<X, T> {
107 Sorted(x, t)
108 }
109}
110
111impl<X, T> SortedWith<T> for Sorted<X, T> {
112 fn sort(&self) -> &T {
113 &self.1
114 }
115}
116
117impl<X: Ord, T: Ord> Ord for Sorted<X, T> {
118 fn cmp(&self, other: &Sorted<X, T>) -> Ordering {
119 match self.1.cmp(&other.1) {
120 Ordering::Equal => self.0.cmp(&other.0),
121 ord => ord
122 }
123 }
124}
125
126impl<X: PartialOrd, T: PartialOrd> PartialOrd for Sorted<X, T> {
127 fn partial_cmp(&self, other: &Sorted<X, T>) -> Option<Ordering> {
128 match self.1.partial_cmp(&other.1) {
129 Some(Ordering::Equal) => self.0.partial_cmp(&other.0),
130 Some(ord) => Some(ord),
131 None => None
132 }
133 }
134}
135
136impl<X: Eq, T: Eq> Eq for Sorted<X, T> {}
137
138impl<X:PartialEq, T: PartialEq> PartialEq for Sorted<X, T> {
139 fn eq(&self, other: &Sorted<X, T>) -> bool {
140 self.1 == other.1 && self.0 == other.0
141 }
142}
143
144impl<X: Hash, T: Hash> Hash for Sorted<X, T> {
145 fn hash<H: Hasher>(&self, state: &mut H) {
146 self.0.hash(state);
147 self.1.hash(state);
148 }
149}
150
151impl<X: Copy, T: Copy> Copy for Sorted<X, T> {}
152
153impl<X: Clone, T: Clone> Clone for Sorted<X, T> {
154 fn clone(&self) -> Sorted<X, T> {
155 Sorted(self.0.clone(), self.1.clone())
156 }
157}
158
159impl<X: fmt::Display, T: fmt::Display> fmt::Display for Sorted<X, T> {
160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161 write!(f, "{}:{}", self.0, self.1)
162 }
163}
164
165impl<X: fmt::Debug, T: fmt::Debug> fmt::Debug for Sorted<X, T> {
166 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
167 write!(f, "{:?}:{:?}", self.0, self.1)
168 }
169}