Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan 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/