Advertisement
GeneralGDA

FixedSizeList<T>

Sep 26th, 2019
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. // The MIT License (MIT)
  2.  
  3. // Copyright (c) 2019 Ltd Cadwise-N
  4.  
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11.  
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14.  
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. using System;
  23. using Guard;
  24. using JetBrains.Annotations;
  25.  
  26. namespace Cadwise.Collections
  27. {
  28.     public sealed class FixedSizeList<T>
  29.     {
  30.         private readonly T[] _backend;
  31.        
  32.         private int _firstFreeSlot;
  33.  
  34.         public FixedSizeList(int capacity)
  35.         {
  36.             ThrowIf.Argument.NegativeOrZero(capacity, nameof(capacity));
  37.  
  38.             _backend = new T[capacity];
  39.         }
  40.  
  41.         public void Add(T item)
  42.         {
  43.             ThrowIf.Precondition.Failed(false == Full(), "list is full");
  44.  
  45.             _backend[_firstFreeSlot] = item;
  46.             ++_firstFreeSlot;
  47.         }
  48.  
  49.         public void Clear()
  50.         {
  51.             for (int i = 0; i < _firstFreeSlot; ++i)
  52.             {
  53.                 _backend[i] = default(T);
  54.             }
  55.  
  56.             _firstFreeSlot = 0;
  57.         }
  58.  
  59.         [Pure]
  60.         public bool Full()
  61.         {
  62.             return _firstFreeSlot == _backend.Length;
  63.         }
  64.  
  65.         [Pure]
  66.         public int GetCapacity()
  67.         {
  68.             return _backend.Length;
  69.         }
  70.  
  71.         [Pure]
  72.         public int GetLength()
  73.         {
  74.             return _firstFreeSlot;
  75.         }
  76.  
  77.         [Pure]
  78.         public ReadOnlySpan<T> Access()
  79.         {
  80.             return _backend.AsSpan(0, _firstFreeSlot);
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement