-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathReadonlyArrayBuffer.cs
More file actions
57 lines (47 loc) · 1.19 KB
/
ReadonlyArrayBuffer.cs
File metadata and controls
57 lines (47 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections;
using System.Collections.Generic;
namespace Npgsql.Replication.PgOutput;
sealed class ReadOnlyArrayBuffer<T> : IReadOnlyList<T>
{
public static readonly ReadOnlyArrayBuffer<T> Empty = new();
T[] _items;
int _size;
public ReadOnlyArrayBuffer()
=> _items = [];
ReadOnlyArrayBuffer(T[] items)
{
_items = items;
_size = items.Length;
}
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < _size; i++)
{
yield return _items[i];
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public int Count
{
get => _size;
internal set
{
if (_items.Length < value)
_items = new T[value];
_size = value;
}
}
public T this[int index]
{
get => index < _size ? _items[index] : throw new IndexOutOfRangeException();
internal set => _items[index] = value;
}
public ReadOnlyArrayBuffer<T> Clone()
{
var newItems = new T[_size];
if (_size > 0)
Array.Copy(_items, newItems, _size);
return new(newItems);
}
}