mod error_type;
mod fasts;
mod tools;
pub use error_type::*;
pub use fasts::*;
use std::{
collections::HashSet,
fmt::{Debug, Display},
ops::{Add, Index, Neg, Sub},
};
use tools::Eprinter;
#[derive(Clone)]
#[repr(Rust)]
pub struct List<T>(Option<(T, Box<List<T>>)>);
impl<T> List<T> {
pub const NEW: List<T> = Self::new();
pub const fn new() -> List<T> {
Self(None)
}
pub fn append_of_front(&mut self, front_t: T) -> &mut Self {
let all = self.0.take();
self.0 = Some((front_t, Box::new(List(all))));
self
}
pub fn append_of_end(&mut self, end_t: T) -> &mut Self {
match self.0 {
Some((_, ref mut child)) => child.append_of_end(end_t),
None => self.append_of_front(end_t),
}
}
pub fn append_of_index(&mut self, index: usize, add_t: T) -> &mut Self
where
T: Clone,
{
let mut x = Vec::<T>::new();
for _ in index..self.len() {
x.push(
self.remove_of_index(index)
.eprinter(&format!("Can't append of index: {}", index)),
);
}
self.append_of_end(add_t);
for i in x {
self.append_of_end(i);
}
self
}
#[deprecated(since = "0.1.8", note = "请转用`remove_of_index`,参数完全一样")]
pub fn remove_index() {
}
pub fn remove_of_index(&mut self, index: usize) -> Result<T, ErrorType>
where
T: Clone,
{
let mut new_self = self.to_vec();
match self.get_t_of_index(index) {
Ok(_) => (),
Err(e) => return Err(e),
}
let res = new_self.remove(index);
*self = Self::vec_to_list(new_self);
Ok(res)
}
pub fn remove_of_front(&mut self) -> Result<T, ErrorType>
where
T: Clone,
{
self.remove_of_index(0)
}
pub fn remove_of_end(&mut self) -> Result<T, ErrorType>
where
T: Clone,
{
self.remove_of_index(self.len() - 1)
}
pub const fn get_t_of_front(&self) -> Result<&T, ErrorType> {
self.get_t_of_index(0)
}
pub const fn get_t_of_index(&self, index: usize) -> Result<&T, ErrorType> {
let mut a = &self.0;
let mut i = 0;
loop {
if i == index {
match a {
Some((t, _)) => return Ok(t),
None => return Err(ErrorType::IndexNotFound(i)),
}
} else {
match a {
Some((_, b)) => a = &b.0,
None => return Err(ErrorType::IndexNotFound(i)),
}
}
i += 1;
}
}
pub const fn get_t_of_end(&self) -> Result<&T, ErrorType> {
self.get_t_of_index(self.len() - 1)
}
pub const fn len(&self) -> usize {
let mut a = &self.0;
let mut i = 0;
loop {
match a {
Some((_, b)) => a = &b.0,
None => return i,
}
i += 1;
}
}
pub fn to_vec(&self) -> Vec<T>
where
T: Clone,
{
let mut vec = Vec::<T>::new();
let mut a = &self.0;
loop {
match a {
Some((t, b)) => {
vec.push(t.clone());
a = &b.0;
}
None => break,
}
}
vec
}
pub fn vec_to_list(vec: Vec<T>) -> List<T> {
let mut res: List<T> = List::new();
for t in vec {
res.append_of_end(t);
}
res
}
pub fn clear(&mut self) {
*self = Self::new();
}
#[deprecated(since = "0.1.8", note = "请使用`slice_to_list`")]
pub fn from(t: &[T]) -> Self
where
T: Clone,
{
let mut res = Self::new();
for arg in t {
res.append_of_end(arg.clone());
}
res
}
pub fn reverse_mut(&mut self)
where
T: Clone,
{
let mut vec = self.clone().to_vec();
vec.reverse();
*self = vec.into();
}
pub fn reverse(&self) -> Self
where
T: Clone,
{
let mut l = self.to_vec();
l.reverse();
Self::vec_to_list(l)
}
pub fn search(&self, find_t: T) -> Vec<usize>
where
T: Clone + Ord,
{
let mut vec = Vec::new();
for (i, t) in self.to_vec().iter().enumerate() {
if t == &find_t {
vec.push(i)
}
}
vec
}
pub fn search_of_one(&self, find_t: T) -> Option<usize>
where
T: Clone + Ord,
{
match self.search(find_t).get(0) {
Some(t) => Some(*t),
None => None,
}
}
pub fn sorted(&self) -> Self
where
T: Clone + Ord,
{
let mut s = self.to_vec();
s.sort();
Self::vec_to_list(s)
}
pub fn mut_sorted(&mut self)
where
T: Ord + Clone,
{
*self = self.sorted();
}
pub fn slice_to_list(s: &[T]) -> Self
where
T: Clone,
{
Self::vec_to_list(s.to_vec())
}
pub fn swap_of_index<N>(&mut self, f_index: N, s_index: N)
where
T: Clone,
usize: From<N>,
{
let mut clone_self: Vec<T> = self.to_vec();
clone_self.swap(f_index.into(), s_index.into());
*self = Self::vec_to_list(clone_self);
}
pub fn swap_of_t(&mut self, f: T, s: T) -> Option<()>
where
T: Ord + Clone + ToString,
{
let f_index = self.search_of_one(f.clone())?;
let s_index = self.search_of_one(s.clone())?;
self.swap_of_index(f_index, s_index);
Some(())
}
}
impl<T: Ord + Clone> Ord for List<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let (self_vec, orther_vec) = (self.to_vec(), other.to_vec());
self_vec.cmp(&orther_vec)
}
}
impl<T: PartialOrd> PartialOrd for List<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<T: Eq> Eq for List<T> {}
impl<T: PartialEq> PartialEq for List<T> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<T: Clone + Debug> Display for List<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.to_vec())
}
}
impl<T: Clone + Debug> Debug for List<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.to_vec())
}
}
impl<T> Default for List<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Clone> From<List<T>> for Vec<T> {
fn from(value: List<T>) -> Self {
value.to_vec()
}
}
impl<T> From<Vec<T>> for List<T> {
fn from(value: Vec<T>) -> Self {
Self::vec_to_list(value)
}
}
impl<T: Clone> From<&[T]> for List<T> {
fn from(value: &[T]) -> Self {
List::slice_to_list(value)
}
}
impl<T: Clone> From<Box<[T]>> for List<T> {
fn from(value: Box<[T]>) -> Self {
List::slice_to_list(&value)
}
}
impl<T: Clone> From<List<T>> for Box<[T]> {
fn from(value: List<T>) -> Self {
value.to_vec().into_boxed_slice()
}
}
impl<T: Clone> Iterator for List<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match &self.0 {
Some(_) => {
let mut vec = self.to_vec();
let res = vec.remove(0);
*self = Self::vec_to_list(vec);
Some(res)
}
None => None,
}
}
}
impl<T: Clone> Iterator for &List<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.clone().next()
}
}
impl<T: Clone> Add for List<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
let mut copy_self = self;
for t in rhs.to_vec() {
copy_self.append_of_end(t);
}
copy_self
}
}
impl<T: Clone> Add for &List<T> {
type Output = List<T>;
fn add(self, rhs: Self) -> List<T> {
let mut copy_self = self.clone();
for t in rhs.to_vec() {
copy_self.append_of_end(t);
}
copy_self
}
}
impl<T: Ord + Clone + ToString> Sub for List<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
let mut res = List::new();
for index in rhs.clone() {
if let None = self.clone().search_of_one(index.clone()) {
res.append_of_end(index);
}
}
for index in self.clone() {
if let None = rhs.clone().search_of_one(index.clone()) {
if let None = res.search_of_one(index.clone()) {
res.append_of_end(index);
}
}
}
res.sorted()
}
}
impl<T: Ord + Clone + ToString> Sub for &List<T> {
type Output = List<T>;
fn sub(self, rhs: Self) -> Self::Output {
self.clone() - rhs.clone()
}
}
impl<T: Clone> Neg for List<T> {
type Output = Self;
fn neg(self) -> Self {
self.reverse()
}
}
impl<T: Clone> Neg for &List<T> {
type Output = List<T>;
fn neg(self) -> Self::Output {
self.reverse()
}
}
impl<T> Index<usize> for List<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.get_t_of_index(index)
.eprinter(&format!("Index out of len!(len: {})", self.len()))
}
}
impl<C> FromIterator<C> for List<C> {
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self {
let mut list = List::new();
for i in iter {
list.append_of_end(i);
}
list
}
}
pub trait ToList<T>
where
T: Clone,
{
fn to_list(self) -> List<T>;
}
impl<T: Clone> ToList<T> for Vec<T> {
fn to_list(self) -> List<T> {
List::vec_to_list(self)
}
}
impl<T: Clone> ToList<T> for &[T] {
fn to_list(self) -> List<T> {
self.into()
}
}
impl<T: Clone> ToList<T> for HashSet<T> {
fn to_list(self) -> List<T> {
self.into_iter().collect()
}
}