Skip to content

Commit 217679f

Browse files
authored
Add unrolled linked list (TheAlgorithms#251)
1 parent c78a72b commit 217679f

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using DataStructures.UnrolledList;
3+
using FluentAssertions;
4+
using NUnit.Framework;
5+
6+
namespace DataStructures.Tests.UnrolledList
7+
{
8+
public class UnrolledLinkedListNodeTests
9+
{
10+
[Test]
11+
public void GetAndSet_SetItemNodeAndGetIt_ReturnExpectedItem()
12+
{
13+
var node = new UnrolledLinkedListNode(6);
14+
node.Set(0, 1);
15+
16+
var result = node.Get(0);
17+
18+
result.Should().Be(1);
19+
}
20+
21+
[Test]
22+
public void Get_GetLowIndex_ThrowArgumentException()
23+
{
24+
var node = new UnrolledLinkedListNode(6);
25+
26+
Action action = () => node.Get(-1);
27+
28+
action.Should().Throw<ArgumentException>();
29+
}
30+
31+
[Test]
32+
public void Get_GetHighIndex_ThrowArgumentException()
33+
{
34+
var node = new UnrolledLinkedListNode(6);
35+
36+
Action action = () => node.Get(7);
37+
38+
action.Should().Throw<ArgumentException>();
39+
}
40+
41+
[Test]
42+
public void Set_SetLowIndex_ThrowArgumentException()
43+
{
44+
var node = new UnrolledLinkedListNode(6);
45+
46+
Action action = () => node.Set(-1, 0);
47+
48+
action.Should().Throw<ArgumentException>();
49+
}
50+
51+
[Test]
52+
public void Set_SetHighIndex_ThrowArgumentException()
53+
{
54+
var node = new UnrolledLinkedListNode(6);
55+
56+
Action action = () => node.Set(7, 0);
57+
58+
action.Should().Throw<ArgumentException>();
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using DataStructures.UnrolledList;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
5+
namespace DataStructures.Tests.UnrolledList
6+
{
7+
public class UnrolledLinkedListTests
8+
{
9+
[Test]
10+
public void Insert_LinkArrayToLinkedList_ReturnArrayHaveSameItems()
11+
{
12+
var linkedList = new UnrolledLinkedList(6);
13+
var contest = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
14+
foreach (var number in contest)
15+
{
16+
linkedList.Insert(number);
17+
}
18+
19+
var result = linkedList.GetRolledItems();
20+
21+
result.Should().BeEquivalentTo(contest);
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
3+
namespace DataStructures.UnrolledList
4+
{
5+
/// <summary>
6+
/// Unrolled linked list is a linked list of small arrays,
7+
/// all of the same size where each is so small that the insertion
8+
/// or deletion is fast and quick, but large enough to fill the cache line.
9+
/// </summary>
10+
public class UnrolledLinkedList
11+
{
12+
private readonly int sizeNode;
13+
14+
private UnrolledLinkedListNode start = null!;
15+
private UnrolledLinkedListNode end = null!;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="UnrolledLinkedList"/> class.
19+
/// Create a unrolled list with start chunk size.
20+
/// </summary>
21+
/// <param name="chunkSize">The size of signe chunk.</param>
22+
public UnrolledLinkedList(int chunkSize)
23+
{
24+
sizeNode = chunkSize + 1;
25+
}
26+
27+
/// <summary>
28+
/// Add value to list [O(n)].
29+
/// </summary>
30+
/// <param name="value">The entered value.</param>
31+
public void Insert(int value)
32+
{
33+
if (start == null)
34+
{
35+
start = new UnrolledLinkedListNode(sizeNode);
36+
start.Set(0, value);
37+
38+
end = start;
39+
return;
40+
}
41+
42+
if (end.Count + 1 < sizeNode)
43+
{
44+
end.Set(end.Count, value);
45+
}
46+
else
47+
{
48+
var pointer = new UnrolledLinkedListNode(sizeNode);
49+
var j = 0;
50+
for (var pos = end.Count / 2 + 1; pos < end.Count; pos++)
51+
{
52+
pointer.Set(j++, end.Get(pos));
53+
}
54+
55+
pointer.Set(j++, value);
56+
pointer.Count = j;
57+
58+
end.Count = end.Count / 2 + 1;
59+
end.Next = pointer;
60+
end = pointer;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Help method. Get all list inside to check the state.
66+
/// </summary>
67+
/// <returns>Items from all nodes.</returns>
68+
public IEnumerable<int> GetRolledItems()
69+
{
70+
UnrolledLinkedListNode pointer = start;
71+
List<int> result = new();
72+
73+
while (pointer != null)
74+
{
75+
for (var i = 0; i < pointer.Count; i++)
76+
{
77+
result.Add(pointer.Get(i));
78+
}
79+
80+
pointer = pointer.Next;
81+
}
82+
83+
return result;
84+
}
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
3+
namespace DataStructures.UnrolledList
4+
{
5+
/// <summary>
6+
/// Single node with array buffer for unrolled list.
7+
/// </summary>
8+
public class UnrolledLinkedListNode
9+
{
10+
private readonly int[] array;
11+
12+
public UnrolledLinkedListNode(int nodeSize)
13+
{
14+
Next = null!;
15+
16+
Count = 0;
17+
array = new int[nodeSize];
18+
}
19+
20+
public UnrolledLinkedListNode Next { get; set; }
21+
22+
public int Count { get; set; }
23+
24+
/// <summary>
25+
/// Set new item in array buffer.
26+
/// </summary>
27+
/// <param name="pos">Index in array.</param>
28+
/// <param name="val">The entered value.</param>
29+
/// <exception cref="ArgumentException">Index is out of scope.</exception>
30+
public void Set(int pos, int val)
31+
{
32+
if (pos < 0 || pos > array.Length - 1)
33+
{
34+
throw new ArgumentException("Position is out of size", nameof(pos));
35+
}
36+
37+
array[pos] = val;
38+
Count++;
39+
}
40+
41+
/// <summary>
42+
/// Get item from array buffer.
43+
/// </summary>
44+
/// <param name="pos">Index in array.</param>
45+
/// <exception cref="ArgumentException">Index is out of scope.</exception>
46+
public int Get(int pos)
47+
{
48+
if (pos < 0 || pos > array.Length - 1)
49+
{
50+
throw new ArgumentException("Position is out of size", nameof(pos));
51+
}
52+
53+
return array[pos];
54+
}
55+
}
56+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ This repository contains algorithms and data structures implemented in C# for ed
141141
* [Directed Weighted Graph Via Adjacency Matrix](./DataStructures/Graph/DirectedWeightedGraph.cs)
142142
* [Disjoint Set](./DataStructures/DisjointSet)
143143
* [SortedList](./DataStructures/SortedList.cs)
144+
* [Unrolled linked list](./DataStructures/UnrolledList/UnrolledLinkedList.cs)
144145

145146

146147
## Contributing

0 commit comments

Comments
 (0)