Add an insert function to our list utility class.

Review URL: http://codereview.chromium.org/40105

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1415 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent f87d1530
...@@ -58,6 +58,18 @@ Vector<T> List<T, P>::AddBlock(const T& element, int count) { ...@@ -58,6 +58,18 @@ Vector<T> List<T, P>::AddBlock(const T& element, int count) {
} }
template<typename T, class P>
T& List<T, P>::Insert(int i, const T& element) {
int free_index = length_ - 1;
Add(last()); // Add grows the list if necessary.
while (free_index > i) {
data_[free_index] = data_[free_index - 1];
free_index--;
}
data_[free_index] = element;
}
template<typename T, class P> template<typename T, class P>
T List<T, P>::Remove(int i) { T List<T, P>::Remove(int i) {
T element = at(i); T element = at(i);
......
...@@ -79,9 +79,16 @@ class List { ...@@ -79,9 +79,16 @@ class List {
// until the next change is made to this list. // until the next change is made to this list.
Vector<T> AddBlock(const T& value, int count); Vector<T> AddBlock(const T& value, int count);
// Inserts a copy of the given element at index i in the list. All
// elements formerly at or above i are moved up and the length of
// the list increases by one. This function's complexity is linear
// in the size of the list.
T& Insert(int i, const T& element);
// Removes the i'th element without deleting it even if T is a // Removes the i'th element without deleting it even if T is a
// pointer type; moves all elements above i "down". Returns the // pointer type; moves all elements above i "down". Returns the
// removed element. // removed element. This function's complexity is linear in the
// size of the list.
T Remove(int i); T Remove(int i);
// Removes the last element without deleting it even if T is a // Removes the last element without deleting it even if T is a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment