FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

insert for cashew.utils.Array?

 
Post new topic   Reply to topic     Forum Index -> Cashew
View previous topic :: View next topic  
Author Message
baxissimo



Joined: 23 Oct 2006
Posts: 241
Location: Tokyo, Japan

PostPosted: Tue Oct 02, 2007 9:08 pm    Post subject: insert for cashew.utils.Array? Reply with quote

I think I sent you an implementation of insert before, but it doesn't seem to have made it into Cashew. Here it is again (or maybe for the first time?):

Code:

/***********************************************************************************
 *  Insert item x into an array at (just before) index.
 */
void insert (T) (ref T[] haystack, size_t index, T x)
in {
  assert(index < haystack.length+1, ".insert() called with index greater than array length");
}
body {
    if (index == haystack.length) {
        haystack ~= x;
    }
    else {
        haystack.length = haystack.length+1;
        memmove(&haystack[index+1], &haystack[index],
                T.sizeof * (haystack.length - index - 1));
        haystack[index] = x;
    }
}
unittest {
  int[] foo = [0, 1, 2, 3, 4, 5];
  foo.insert(3_U, 3);
  assert(foo == [0, 1, 2, 3,3, 4, 5]);
  foo.insert(7_U, 9);
  assert(foo == [0, 1, 2, 3,3, 4, 5, 9]);
}

Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Wed Oct 03, 2007 6:53 pm    Post subject: Reply with quote

Honestly, I don't remember if you did or not.... but regardless, its in there now. Smile
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Cashew All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group