Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 245290
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 17, 20222022-08-17T02:47:18+00:00 2022-08-17T02:47:18+00:00In: Rust

rust – Efficiently insert or replace multiple elements in the middle or at the beginning of a Vec?

  • 0

[ad_1]

I was trying to prepend to a vector in rust and found this closed question that was linked here, (despite this question being both prepend and insert AND efficiency. I think my answer would be better as an answer for that other, more precises question because I can’t attest to the efficiency), but the following code helped me prepend, (and the opposite.) [I’m sure that the other two answers are more efficient, but the way that I learn, I like having answers that can be cut-n-pasted with examples that demonstrate an application of the answer.]

pub trait Unshift<T> { fn unshift(&mut self, s: &[T]) -> (); }
pub trait UnshiftVec<T> { fn unshift_vec(&mut self, s: Vec<T>) -> (); }
pub trait UnshiftMemoryHog<T> { fn unshift_memory_hog(&mut self, s: Vec<T>) -> (); }
pub trait Shift<T> { fn shift(&mut self) -> (); }
pub trait ShiftN<T> { fn shift_n(&mut self, s: usize) -> (); }

impl<T: std::clone::Clone> ShiftN<T> for Vec<T> {
    fn shift_n(&mut self, s: usize) -> ()
    // where
    //    T: std::clone::Clone,
    {   
        self.drain(0..s);
    }
}

impl<T: std::clone::Clone> Shift<T> for Vec<T> {
    fn shift(&mut self) -> ()
    // where
    //    T: std::clone::Clone,
    {   
        self.drain(0..1);
    }
}

impl<T: std::clone::Clone> Unshift<T> for Vec<T> {
    fn unshift(&mut self, s: &[T]) -> ()
    // where
    //    T: std::clone::Clone,
    {   
        self.splice(0..0, s.to_vec());
    }
}
impl<T: std::clone::Clone> UnshiftVec<T> for Vec<T> {
    fn unshift_vec(&mut self, s: Vec<T>) -> ()
    where
        T: std::clone::Clone,
    {   
        self.splice(0..0, s);
    }
}

impl<T: std::clone::Clone> UnshiftMemoryHog<T> for Vec<T> {
    fn unshift_memory_hog(&mut self, s: Vec<T>) -> ()
    where
        T: std::clone::Clone,
    {
        let mut tmp: Vec<_> = s.to_owned();
        //let mut tmp: Vec<_> = s.clone(); // this also works for some data types
        /*
        let local_s: Vec<_> = self.clone(); // explicit clone()
        tmp.extend(local_s);                // to vec is possible
        */
        tmp.extend(self.clone());
        *self = tmp;
        //*self = (*tmp).to_vec(); // Just because it compiles, doesn't make it right.
    }
}

// this works for: v = unshift(v, &vec![8]);
// (If you don't want to impl Unshift for Vec<T>)

#[allow(dead_code)]
fn unshift_fn<T>(v: Vec<T>, s: &[T]) -> Vec<T>
where
    T: Clone,
{
    // create a mutable vec and fill it
    // with a clone of the array that we want
    // at the start of the vec.
    let mut tmp: Vec<_> = s.to_owned();
    // then we add the existing vector to the end
    // of the temporary vector.
    tmp.extend(v);
    // return the tmp vec that is identitcal
    // to unshift-ing the original vec.
    tmp
}

/*
    N.B. It is sometimes (often?) more memory efficient to reverse
    the vector and use push/pop, rather than splice/drain;
    Especially if you create your vectors in "stack order" to begin with.
*/

fn main() {
    let mut v: Vec<usize> = vec![1, 2, 3];
    println!("Before push:\t {:?}", v);
    v.push(0);
    println!("After push:\t {:?}", v);
    v.pop();
    println!("popped:\t\t {:?}", v);
    v.drain(0..1);
    println!("drain(0..1)\t {:?}", v);
    /*
        // We could use a function
    let c = v.clone();
    v = unshift_fn(c, &vec![0]);
    */
    v.splice(0..0, vec![0]);
    println!("splice(0..0, vec![0]) {:?}", v);
    v.shift_n(1);
    println!("shift\t\t {:?}", v);
    v.unshift_memory_hog(vec![8, 16, 31, 1]);
    println!("MEMORY guzzler unshift {:?}", v);
    //v.drain(0..3);
    v.drain(0..=2);
    println!("back to the start: {:?}", v);
    v.unshift_vec(vec![0]);
    println!("zerothed with unshift: {:?}", v);

    let mut w = vec![4, 5, 6];
    /*
    let prepend_this = &[1, 2, 3];
    w.unshift_vec(prepend_this.to_vec());
    */
    w.unshift(&[1, 2, 3]);

    assert_eq!(&w, &[1, 2, 3, 4, 5, 6]);
    println!("{:?} == {:?}", &w, &[1, 2, 3, 4, 5, 6]);
}

[ad_2]

  • 0 0 Answers
  • 8 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 3 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 5 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 4 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.