ln.types/odb/values/ODBList.cs

133 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Collections;
using System.Runtime.CompilerServices;
namespace ln.types.odb.values
{
public class ODBList : ODBValue, IEnumerable<ODBValue>
{
List<ODBValue> items = new List<ODBValue>();
public ODBList()
:base(0x02)
{
}
public ODBList(byte[] bytes,int offset,int length)
:this()
{
MemoryStream stream = new MemoryStream(bytes, offset, length);
int nItems = stream.ReadInteger();
for (int n = 0; n < nItems; n++)
items.Add(ODBValue.Read(stream));
}
public ODBValue this[int i]
{
get => items[i];
set => items[i] = value;
}
public void Add(ODBValue value)
{
items.Add(value);
}
public void Remove(ODBValue value)
{
items.Remove(value);
}
public void RemoveAt(int i)
{
items.RemoveAt(i);
}
public int Count => items.Count;
public override ODBValue Clone()
{
ODBList clone = new ODBList();
clone.items.AddRange(this.items);
return clone;
}
public override object Value {
get
{
object[] a = new object[items.Count];
((IList)items).CopyTo(a, 0);
return a;
}
protected set
{
throw new NotSupportedException();
}
}
public override int CompareLevel => 253;
public override byte[] ToStorage()
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(items.Count);
foreach (ODBValue value in items)
value.Store(writer);
return stream.ToArray();
}
public override int CompareInType(ODBValue 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 override bool ValueEquals(ODBValue other)
{
if (other is ODBList)
{
ODBList you = other as ODBList;
if (Count != you.Count)
return false;
for (int n = 0; n < Count; n++)
if (!this[n].ValueEquals(you[n]))
return false;
return true;
}
return false;
}
public IEnumerator<ODBValue> GetEnumerator()
{
return items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return items.GetEnumerator();
}
static ODBList()
{
RegisterDeserializer(0x02, (b, o, l) => new ODBList(b,o,l));
}
}
}