123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Text;
- namespace Island.StandardLib.Storage
- {
- /// <summary>
- /// 表示一个每项长度固定的可序列化变长数组(固定项长可变数组)
- /// </summary>
- /// <typeparam name="T">定长项的类型</typeparam>
- [Serializable]
- public class StorableFixedArray<T> : IStorable, IEnumerable<T> where T : IStorable, new()
- {
- List<T> array;
- /// <summary>
- /// 初始化固定项长可变数组
- /// </summary>
- public StorableFixedArray()
- {
- array = new List<T>();
- }
- public void ReadFromData(DataStorage data)
- {
- data.Read(out int size);
- for (int i = 0; i < size; i++)
- array.Add(data.Read<T>());
- }
- public void WriteToData(DataStorage data)
- {
- data.Write(array.Count);
- for (int i = 0; i < array.Count; i++)
- data.Write(array[i]);
- }
- public void Add(T item)
- {
- array.Add(item);
- }
- public void AddRange(IEnumerable<T> item)
- {
- array.AddRange(item);
- }
- public void Remove(T item)
- {
- array.Remove(item);
- }
- public void RemoveAt(int index)
- {
- array.RemoveAt(index);
- }
- public void Clear()
- {
- array.Clear();
- }
- public IEnumerator<T> GetEnumerator()
- {
- List<T> tl = new List<T>();
- for (int i = 0; i < array.Count; i++)
- tl.Add(array[i]);
- return tl.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- List<T> tl = new List<T>();
- for (int i = 0; i < array.Count; i++)
- tl.Add(array[i]);
- return tl.GetEnumerator();
- }
- public int Count
- {
- get
- {
- return array.Count;
- }
- }
- public int Size
- {
- get
- {
- return array.Count;
- }
- }
- public int Length
- {
- get
- {
- return array.Count;
- }
- }
- public T this[int index]
- {
- get
- {
- return array[index];
- }
- }
- }
- }
|