Skip to content

Commit c220dcd

Browse files
committed
Stop using the "data box" concept
1 parent 1b1f461 commit c220dcd

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

DictionaryList/DictionaryList.cs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,7 @@ namespace Vectorial1024.Collections.Generic
1212
/// </summary>
1313
public class DictionaryList<TValue> : IEnumerable<KeyValuePair<int,TValue>>
1414
{
15-
/// <summary>
16-
/// The data-box for storing DictionaryList elements. This is to distinguish between a genuine null and a "no data" entry.
17-
/// </summary>
18-
/// <typeparam name="TData"></typeparam>
19-
internal struct DataBox<TData>
20-
{
21-
internal TData Value;
22-
23-
internal DataBox(TData value)
24-
{
25-
Value = value;
26-
}
27-
}
28-
29-
internal List<DataBox<TValue>?> _list = new List<DataBox<TValue>?>();
15+
internal List<TValue> _list = new List<TValue>();
3016

3117
internal bool[] _issetLookup;
3218

@@ -51,7 +37,7 @@ public DictionaryList()
5137
/// <param name="collection">The collection to take copies from.</param>
5238
public DictionaryList(DictionaryList<TValue> collection)
5339
{
54-
_list = new List<DataBox<TValue>?>(collection._list);
40+
_list = new List<TValue>(collection._list);
5541
_actualCount = collection._actualCount;
5642
_issetLookup = (bool[]) collection._issetLookup.Clone();
5743
}
@@ -62,7 +48,7 @@ public DictionaryList(DictionaryList<TValue> collection)
6248
/// <param name="capacity"></param>
6349
public DictionaryList(int capacity)
6450
{
65-
_list = new List<DataBox<TValue>?>(capacity);
51+
_list = new List<TValue>(capacity);
6652
_issetLookup = new bool[capacity];
6753
}
6854

@@ -85,16 +71,16 @@ public TValue this[int index]
8571
{
8672
get
8773
{
88-
var item = _list[index];
89-
if (item == null)
74+
var tempItem = _list[index];
75+
if (!IndexIsSet(index))
9076
{
9177
throw new KeyNotFoundException($"The given index {index} was unset in the list.");
9278
}
93-
return item.Value.Value;
79+
return tempItem;
9480
}
9581
set
9682
{
97-
_list[index] = new DataBox<TValue>(value);
83+
_list[index] = value;
9884
_issetLookup[index] = true;
9985
}
10086
}
@@ -107,7 +93,7 @@ public TValue this[int index]
10793
public void Add(TValue value)
10894
{
10995
var nextIndex = _list.Count;
110-
_list.Add(new DataBox<TValue>(value));
96+
_list.Add(value);
11197
_actualCount++;
11298
_version++;
11399

@@ -128,13 +114,12 @@ public void Add(TValue value)
128114
/// <seealso cref="CompactAndTrimExcess"/>
129115
public void UnsetAt(int index)
130116
{
131-
var box = _list[index];
132-
if (box == null)
117+
if (!IndexIsSet(index))
133118
{
134119
return;
135120
}
136121

137-
_list[index] = null;
122+
_list[index] = default!;
138123
_actualCount--;
139124
_issetLookup[index] = false;
140125
}
@@ -186,14 +171,14 @@ public void Clear()
186171
/// </summary>
187172
public void CompactAndTrimExcess()
188173
{
189-
var newList = new List<DataBox<TValue>?>(_actualCount);
190-
foreach (var item in _list)
174+
var newList = new List<TValue>(_actualCount);
175+
for (var index = 0; index < _list.Count; index++)
191176
{
192-
if (item == null)
177+
if (!IndexIsSet(index))
193178
{
194179
continue;
195180
}
196-
newList.Add(item);
181+
newList.Add(_list[index]);
197182
}
198183
_list = newList;
199184
_actualCount = newList.Count;

0 commit comments

Comments
 (0)