ln.types/odb/values/ODBList.cs

126 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Runtime.CompilerServices;
using ln.types.odb.ng;
namespace ln.types.odb.values
{
public class ODBList : ODBEntity, IEnumerable<ODBEntity>
{
Guid identity = Guid.NewGuid();
public override ODBValue Identity => Identity;
List<ODBEntity> items = new List<ODBEntity>();
public ODBList()
:base(0x02)
{
}
public ODBList(byte[] bytes,int offset,int length)
:this()
{
MemoryStream stream = new MemoryStream(bytes, offset, length);
identity = new Guid(stream.ReadBytes(16));
int nItems = stream.ReadInteger();
for (int n = 0; n < nItems; n++)
items.Add(ODBEntity.Read(stream));
}
public ODBEntity this[int i]
{
get => items[i];
set => items[i] = value;
}
public ODBEntity this[ODBEntity i]
{
get => items[i.As<int>()];
set => items[i.As<int>()] = value;
}
public void AddRange(IEnumerable<ODBEntity> values)
{
foreach (ODBEntity value in values)
Add(value);
}
public void Add(ODBEntity value)
{
items.Add(value);
}
public void Remove(ODBEntity value)
{
items.Remove(value);
}
public void RemoveAt(int i)
{
items.RemoveAt(i);
}
public int Count => items.Count;
public override ODBEntity Clone()
{
ODBList clone = new ODBList();
clone.identity = identity;
foreach (ODBEntity item in items)
clone.items.Add(item.Clone());
return clone;
}
public override byte[] GetStorageBytes()
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(identity.ToByteArray());
writer.Write(items.Count);
foreach (ODBEntity value in items)
value.Write(writer);
return stream.ToArray();
}
protected override int compare(ODBEntity other)
{
ODBList you = other as ODBList;
int d = Count - you.Count;
if (d != 0)
return d;
for (int n=0;n<Count;n++)
{
d = this[n].CompareTo(you[n]);
if (d != 0)
return d;
}
return 0;
}
public IEnumerator<ODBEntity> GetEnumerator()
{
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
public override T As<T>() => (T)Mapper.Default.UnmapValue(typeof(T), this);
static ODBList()
{
RegisterDeserializer(0x02, (b, o, l) => new ODBList(b,o,l));
}
}
}