[源代码]List的增加与删除
2024-08-24 15:12:56
// Removes the element at the given index. The size of the list is
// decreased by one.
//
public void RemoveAt(int index) {
if ((uint)index >= (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException();
}
Contract.EndContractBlock();
_size--;
if (index < _size) {
Array.Copy(_items, index + , _items, index, _size - index);
}
_items[_size] = default(T);
_version++;
}
可以看到 Remove的操作里涉及对List的存储空间重新赋值的工作.而多余占用的空间用Default(T)来填充并未回收
// Inserts the elements of the given collection at a given index. If
// required, the capacity of the list is increased to twice the previous
// capacity or the new size, whichever is larger. Ranges may be added
// to the end of the list by setting index to the List's size.
//
public void InsertRange(int index, IEnumerable<T> collection) {
if (collection==null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
} if ((uint)index > (uint)_size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
}
Contract.EndContractBlock(); ICollection<T> c = collection as ICollection<T>;
if( c != null ) { // if collection is ICollection<T>
int count = c.Count;
if (count > ) {
EnsureCapacity(_size + count);
if (index < _size) {
Array.Copy(_items, index, _items, index + count, _size - index);
} // If we're inserting a List into itself, we want to be able to deal with that.
if (this == c) {
// Copy first part of _items to insert location
Array.Copy(_items, , _items, index, index);
// Copy last part of _items back to inserted location
Array.Copy(_items, index+count, _items, index*, _size-index);
}
else {
T[] itemsToInsert = new T[count];
c.CopyTo(itemsToInsert, );
itemsToInsert.CopyTo(_items, index);
}
_size += count;
}
}
else {
using(IEnumerator<T> en = collection.GetEnumerator()) {
while(en.MoveNext()) {
Insert(index++, en.Current);
}
}
}
_version++;
}
// Ensures that the capacity of this list is at least the given minimum
// value. If the currect capacity of the list is less than min, the
// capacity is increased to twice the current capacity or to min,
// whichever is larger.
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == ? _defaultCapacity : _items.Length * ;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
// Gets and sets the capacity of this list. The capacity is the size of
// the internal array used to hold items. When set, the internal
// array of the list is reallocated to the given capacity.
//
public int Capacity {
get {
Contract.Ensures(Contract.Result<int>() >= );
return _items.Length;
}
set {
if (value < _size) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_SmallCapacity);
}
Contract.EndContractBlock(); if (value != _items.Length) {
if (value > ) {
T[] newItems = new T[value];
if (_size > ) {
Array.Copy(_items, , newItems, , _size);
}
_items = newItems;
}
else {
_items = _emptyArray;
}
}
}
}
2.1 在尾部插入速度最快
2.2 在中间插入,涉及数组拷贝
2.3 元素不够时,涉及重新分配内在和数组拷贝
最新文章
- Oracle约束(Constraint)详解
- 【zz】matlab 均值方差
- [Doxygen]Doxygen
- windows 下 gvim/vim lua支持问题,neocomplete等插件支持
- python 多线程就这么简单
- boost.compressed_pair源码剖析
- Nodejs_day04
- 获取html上元素的真正坐标
- 包管理器Bower使用手册之一
- Rouh set 入门知识3(上下近似集,正负域,边界域)
- CSS块级元素与行级元素(转载)
- 使用mitmproxy嗅探双向认证ssl链接——嗅探AWS IoT SDK的mqtts
- IIC为什么要配置为开漏输出呢?
- Paper Reading: Stereo DSO
- Postgres通用翻页函数
- hdu3183 rmq求区间最值的下标
- 根据时间段获取时间段内所有时间点(js)
- crontab -e文件存放路径
- V-rep学习笔记:main script and child scripts
- centos7 --ngnix 常用命令: