Skip to content

Commit b39162b

Browse files
committed
SegmentLinkedList
1 parent cc1b25b commit b39162b

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

Foundation/.NetStandard-2.0/Collections/ReadOnly/IEnumerableExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public static ReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> source)
1515
return list.ToReadOnlyList();
1616
}
1717

18+
public static ReadOnlySegmentLinkedList<T> ToReadOnlySegmentLinkedList<T>(this IEnumerable<T> source, int segmentLength)
19+
{
20+
Assert.IsNotNull(source);
21+
var segmentLinkedListBuilder = new SegmentLinkedListBuilder<T>(segmentLength);
22+
23+
foreach (var item in source)
24+
segmentLinkedListBuilder.Add(item);
25+
26+
var readOnlySegmentLinkedList = segmentLinkedListBuilder.ToReadOnlySegmentLinkedList();
27+
return readOnlySegmentLinkedList;
28+
}
29+
1830
public static ReadOnlySortedList<TKey, TValue> ToReadOnlySortedList<TKey, TValue>(this IEnumerable<TValue> values, Func<TValue, TKey> keySelector)
1931
{
2032
Assert.IsNotNull(values);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Foundation.Assertions;
4+
5+
namespace Foundation.Collections.ReadOnly
6+
{
7+
public class ReadOnlySegmentLinkedList<T> : IEnumerable<T>
8+
{
9+
private readonly LinkedList<T[]> _linkedList;
10+
private readonly int _count;
11+
12+
public ReadOnlySegmentLinkedList(LinkedList<T[]> linkedList, int count)
13+
{
14+
Assert.IsNotNull(linkedList);
15+
Assert.IsInRange(count >= 0);
16+
17+
_linkedList = linkedList;
18+
_count = count;
19+
}
20+
21+
public int Count => _count;
22+
23+
public IEnumerator<T> GetEnumerator()
24+
{
25+
var linkedListNode = _linkedList.First;
26+
while (linkedListNode != null)
27+
{
28+
var segment = linkedListNode.Value;
29+
var count = linkedListNode != _linkedList.Last ? segment.Length : _count % segment.Length;
30+
for (var i = 0; i < count; ++i)
31+
yield return segment[i];
32+
linkedListNode = linkedListNode.Next;
33+
}
34+
}
35+
36+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using Foundation.Assertions;
3+
using Foundation.Collections.ReadOnly;
4+
5+
namespace Foundation.Collections
6+
{
7+
public class SegmentLinkedListBuilder<T>
8+
{
9+
private readonly LinkedList<T[]> _linkedList = new LinkedList<T[]>();
10+
private readonly int _segmentLength;
11+
private int _count = 0;
12+
13+
public SegmentLinkedListBuilder(int segmentLength)
14+
{
15+
Assert.IsInRange(segmentLength > 0);
16+
_segmentLength = segmentLength;
17+
}
18+
19+
public void Add(T item)
20+
{
21+
T[] segment;
22+
var index = _count % _segmentLength;
23+
if (index == 0)
24+
{
25+
segment = new T[_segmentLength];
26+
_linkedList.AddLast(segment);
27+
}
28+
else
29+
segment = _linkedList.Last.Value;
30+
31+
segment[index] = item;
32+
++_count;
33+
}
34+
35+
public ReadOnlySegmentLinkedList<T> ToReadOnlySegmentLinkedList() => new ReadOnlySegmentLinkedList<T>(_linkedList, _count);
36+
}
37+
}

Foundation/.NetStandard-2.0/Data/DbProviderFactoryExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static DataTable ExecuteDataTable(this DbProviderFactory factory, DbConne
2626
return table;
2727
}
2828

29+
public static DataTable ExecuteDataTable(this DbProviderFactory dbProviderFactory, string connectionString, string commandText)
30+
{
31+
using (var connection = dbProviderFactory.CreateConnection())
32+
{
33+
connection.ConnectionString = connectionString;
34+
connection.Open();
35+
return ExecuteDataTable(dbProviderFactory, connection, commandText);
36+
}
37+
}
38+
2939
public static void ExecuteReader(this DbProviderFactory dbProviderFactory, string connectionString, ExecuteReaderRequest request,
3040
Action<IDataReader> read)
3141
{

Foundation/.NetStandard-2.0/Data/DbQueryBuilding/DbRequestBuilder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ private string GetExecuteReaderMethod()
288288
next = "Next";
289289
}
290290

291-
stringBuilder.Append($" var {ToLower(result.FieldName)} = dataReader.Read{next}Result(Read{result.Name}).ToReadOnlyList();");
291+
stringBuilder.Append(
292+
$" var {ToLower(result.FieldName)} = dataReader.Read{next}Result(Read{result.Name}).ToReadOnlySegmentLinkedList(128);");
292293
}
293294

294295
stringBuilder.Append($"\r\n result = new {_request.Name}DbQueryResult(");
@@ -369,7 +370,7 @@ private string GetExecuteReaderAsyncMethodFragment()
369370
{
370371
var next = sequence.Next() == 0 ? null : "Next";
371372
stringBuilder.Append(
372-
$"var {ToLower(result.FieldName)} = (await dataReader.Read{next}ResultAsync(Read{result.Name}, request.CancellationToken)).ToReadOnlyList();\r\n");
373+
$"var {ToLower(result.FieldName)} = (await dataReader.Read{next}ResultAsync(Read{result.Name}, request.CancellationToken)).ToReadOnlySegmentLinkedList(128);\r\n");
373374
}
374375

375376
stringBuilder.Append($"result = new {_request.Name}DbQueryResult({GetResultVariableNames()});");
@@ -387,7 +388,7 @@ private string GetQueryResultClass()
387388
if (sequence.Next() > 0)
388389
stringBuilder.Append("\r\n");
389390

390-
stringBuilder.Append($" public readonly ReadOnlyList<{result.Name}> {result.FieldName};");
391+
stringBuilder.Append($" public readonly ReadOnlySegmentLinkedList<{result.Name}> {result.FieldName};");
391392
}
392393

393394
stringBuilder.Append("\r\n\r\n");
@@ -407,7 +408,7 @@ private string GetQueryResultClassConstructor(ReadOnlyList<DbQueryResult> result
407408
if (sequence.Next() > 0)
408409
stringBuilder.Append(", ");
409410

410-
stringBuilder.Append($"ReadOnlyList<{result.Name}> {ToLower(result.FieldName)}");
411+
stringBuilder.Append($"ReadOnlySegmentLinkedList<{result.Name}> {ToLower(result.FieldName)}");
411412
}
412413

413414
stringBuilder.Append(")\r\n");

0 commit comments

Comments
 (0)