From 9e0abb48fbe5a5a7c460bfa659966191c41b1b06 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Wed, 30 Jul 2025 12:53:09 +0800 Subject: [PATCH] added LICENSE and cleaned source folder --- LICENSE | 18 ++++++ src/linked_list.rs | 153 --------------------------------------------- src/test.rs | 55 ---------------- 3 files changed, 18 insertions(+), 208 deletions(-) create mode 100644 LICENSE delete mode 100644 src/linked_list.rs delete mode 100644 src/test.rs diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8704769 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +MIT License + +Copyright (c) 2025 woggioni + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/linked_list.rs b/src/linked_list.rs deleted file mode 100644 index 5803494..0000000 --- a/src/linked_list.rs +++ /dev/null @@ -1,153 +0,0 @@ -use std::mem; -use std::ptr; - -pub struct List { - list_head: Option>>, - list_tail: Rawlink>, -} - -struct Rawlink { p: *mut T } - -impl Copy for Rawlink {} - -impl Clone for Rawlink { - fn clone(&self) -> Self { Rawlink { p: self.p } } -} - -pub struct Node { - next: Option>>, - prev: Rawlink>, - value: T, -} - -impl List { - pub fn is_empty(&self) -> bool { - self.list_head.is_none() - } - - pub fn len(&self) -> usize { - let mut node = &self.list_head; - let mut i = 0; - loop { - match *node { - Some(ref n) => { - i+=1; - node=&n.next; - } - None => { - return i; - } - } - } - } - - /// Create an empty DList - pub fn new() -> List { - List{list_head: None, list_tail: Rawlink::none()} - } - - pub fn push_front(&mut self, elt: T) { - self.push_front_node(Box::new(Node::new(elt))) - } - - pub fn push_front_node(&mut self, mut new_head: Box>) { - match self.list_head { - None => { - self.list_tail = Rawlink::some(&mut new_head); - new_head.prev = Rawlink::none(); - self.list_head = Some(new_head); - } - Some(ref mut head) => { - new_head.prev = Rawlink::none(); - head.prev = Rawlink::some(&mut new_head); - mem::swap(head, &mut new_head); - head.next = Some(new_head); - } - } - } - - /// Provide a forward iterator - #[inline] - pub fn iter<'a>(&'a self) -> ListIterator<'a, T> { - ListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail} - } -} - -impl Node { - fn new(v: T) -> Node { - Node{value: v, next: None, prev: Rawlink::none()} - } -} - -/// Rawlink is a type like Option but for holding a raw pointer -impl Rawlink { - /// Like Option::None for Rawlink - fn none() -> Rawlink { - Rawlink{p: ptr::null_mut()} - } - - /// Like Option::Some for Rawlink - fn some(n: &mut T) -> Rawlink { - Rawlink{p: n as *mut T} - } - - /// Convert the `Rawlink` into an Option value - fn resolve_immut<'a>(&self) -> Option<&'a T> { - unsafe { self.p.as_ref() } - } - - /// Convert the `Rawlink` into an Option value - fn resolve<'a>(&mut self) -> Option<&'a mut T> { - unsafe { self.p.as_mut() } - } - - /// Return the `Rawlink` and replace with `Rawlink::none()` - fn take(&mut self) -> Rawlink { - mem::replace(self, Rawlink::none()) - } -} - -pub struct ListIterator<'a, T: 'a> { - head: &'a Option>>, - tail: Rawlink>, - nelem: usize, -} - -impl<'a, A> Iterator for ListIterator<'a, A> { - type Item = &'a A; - - #[inline] - fn next(&mut self) -> Option<&'a A> { - if self.nelem == 0 { - return None; - } - self.head.as_ref().map(|head| { - self.nelem -= 1; - self.head = &head.next; - &head.value - }) - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - (self.nelem, Some(self.nelem)) - } -} - -impl<'a, A> DoubleEndedIterator for ListIterator<'a, A> { - #[inline] - fn next_back(&mut self) -> Option<&'a A> { - if self.nelem == 0 { - return None; - } - let tmp = self.tail.resolve_immut(); - tmp.as_ref().map(|prev| { - self.nelem -= 1; - self.tail = prev.prev; - &prev.value - }) - } -} - -fn main() { -} \ No newline at end of file diff --git a/src/test.rs b/src/test.rs deleted file mode 100644 index 157eaa9..0000000 --- a/src/test.rs +++ /dev/null @@ -1,55 +0,0 @@ - - -use std::collections::HashMap; - -// This struct has one lifetime parameter, 'src. The name is only used within the struct's definition. -#[derive(Debug)] -struct Config<'src> { - hostname: &'src str, - username: &'src str, -} - -// This function also has a lifetime parameter, 'cfg. 'cfg is attached to the "config" parameter, which -// establishes that the data in "config" lives at least as long as the 'cfg lifetime. -// The returned struct also uses 'cfg for its lifetime, so it can live at most as long as 'cfg. -fn parse_config<'cfg>(config: &'cfg str) -> Config<'cfg> { - let key_values: HashMap<_, _> = config - .lines() - .filter(|line| !line.starts_with('#')) - .filter_map(|line| line.split_once('=')) - .map(|(key, value)| (key.trim(), value.trim())) - .collect(); - Config { - hostname: key_values["hostname"], - username: key_values["username"], - } -} - -// fn main() { -// let config = parse_config( -// r#"hostname = foobar -// username=barfoo"#, -// ); -// println!("Parsed config: {:#?}", config); -// } - - -struct Foo {} - -fn foo(v : &[T]) { - for el in v.into_iter() { - - } -} - -// fn foo2(v : &mut Vec>) { -// let first = &v[0]; -// let second = &mut v[1]; - -// second[0] = first[1] + second[2]; -// } -fn main() { - let s = String::from("🤑😂🤩🤬"); - let c= s.chars().nth(1).unwrap(); - println!("{}", c); -} \ No newline at end of file