Categories
Tags
algorithms APIT Arc arm assembly asynchronous base64 BitHacks Blogging box c clang-format client cmake compiler concat concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP dtruss Dynamic Example FFI flat_map format FP fsanitize Functional functions futures Fuwari GATs gccrs generics gitignore glibc GUI hacking hashmap haskell heap interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS Markdown math ML mmap nc OnceLock optimization OS panic parallels perf physics pin postgresql radare2 release reverse RPIT rust sanitizer science Science serialization server shift sin SmallProjects socket std strace String StringView strip strlen surrealdb SWAR swisstable synchronous tan toml traits triangulation UnsafeRust utf16 utf8 Video wsl x86_64 xilem zig
218 words
1 minutes
240702_shift_training
link
shift001(0 index를 삭제하기)
use std::ops::Shl;
// Newtype wrapper to enable Shl implementation
struct ShiftVec<T>(Vec<T>);
impl<T: Copy> Shl<usize> for ShiftVec<T> {
type Output = ShiftVec<T>;
fn shl(mut self, rhs: usize) -> Self::Output {
// Left shift: remove elements from front
for _ in 0..rhs {
if !self.0.is_empty() {
self.0.remove(0);
}
}
self
}
}
fn main() {
let my_arr = ShiftVec(vec![1, 2, 3, 4, 5]);
println!("before arr : {:?}", my_arr.0);
let res = my_arr << 1;
println!("after arr : {:?}", res.0);
}- result
before arr : [1, 2, 3, 4, 5]
after arr : [2, 3, 4, 5]shift002(0 index에 0추가하기)
use std::ops::Shl;
// Newtype wrapper to enable Shl implementation
struct ShiftVec<T>(Vec<T>);
impl<T: Copy + Default> Shl<usize> for ShiftVec<T> {
type Output = ShiftVec<T>;
fn shl(mut self, rhs: usize) -> Self::Output {
// Left shift: add zeros to front (push_front)
for _ in 0..rhs {
// insert 0 at the beginning (push_front equivalent)
self.0.insert(0, T::default());
}
self
}
}
fn main() {
let my_arr = ShiftVec(vec![1i32, 2, 3, 4, 5]);
println!("before arr : {:?}", my_arr.0);
let res = my_arr << 1;
println!("after arr : {:?}", res.0);
}- result
before arr : [1, 2, 3, 4, 5]
after arr : [0, 1, 2, 3, 4, 5]240702_shift_training
https://younghakim7.github.io/blog/posts/240702_shift_training/