Skip to content

Commit

Permalink
added IEnumerable getters GetXXXEnumerable to vectors in C#
Browse files Browse the repository at this point in the history
  • Loading branch information
yoniiny committed Aug 15, 2015
1 parent 2016992 commit 952428d
Show file tree
Hide file tree
Showing 19 changed files with 846 additions and 15 deletions.
14 changes: 7 additions & 7 deletions net/FlatBuffers/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public void Reset()

public int Position { get { return _pos; } }

// Pre-allocated helper arrays for convertion.
private float[] floathelper = new[] { 0.0f };
private int[] inthelper = new[] { 0 };
private double[] doublehelper = new[] { 0.0 };
private ulong[] ulonghelper = new[] { 0UL };

#if UNSAFE_BYTEBUFFER
// Helper functions for the unsafe version.
static public ushort ReverseBytes(ushort input)
{
Expand All @@ -78,8 +73,13 @@ static public ulong ReverseBytes(ulong input)
((input & 0x00FF000000000000UL) >> 40) |
((input & 0xFF00000000000000UL) >> 56));
}
#else
// Pre-allocated helper arrays for convertion.
private float[] floathelper = new[] { 0.0f };
private int[] inthelper = new[] { 0 };
private double[] doublehelper = new[] { 0.0 };
private ulong[] ulonghelper = new[] { 0UL };

#if !UNSAFE_BYTEBUFFER
// Helper functions for the safe (but slower) version.
protected void WriteLittleEndian(int offset, int count, ulong data)
{
Expand Down
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferDoubleEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
using ObjectType = Double;

public class FlatBufferDoubleEnumerable : IEnumerable<ObjectType>, IEnumerator<ObjectType>
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(ObjectType);

public FlatBufferDoubleEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<ObjectType> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public ObjectType Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = _buffer.GetDouble(_pos);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
using ObjectType = Byte;

public class FlatBufferEnumerable : IEnumerable<ObjectType>, IEnumerator<ObjectType>
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(ObjectType);

public FlatBufferEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<ObjectType> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public ObjectType Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = _buffer.Get(_pos);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferFloatEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
using ObjectType = Single;

public class FlatBufferFloatEnumerable : IEnumerable<ObjectType>, IEnumerator<ObjectType>
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(ObjectType);

public FlatBufferFloatEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<ObjectType> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public ObjectType Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = _buffer.GetFloat(_pos);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferIntEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
using ObjectType = Int32;

public class FlatBufferIntEnumerable : IEnumerable<ObjectType>, IEnumerator<ObjectType>
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(ObjectType);

public FlatBufferIntEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<ObjectType> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public ObjectType Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = _buffer.GetInt(_pos);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferLongEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
using ObjectType = Int64;

public class FlatBufferLongEnumerable : IEnumerable<ObjectType>, IEnumerator<ObjectType>
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(ObjectType);

public FlatBufferLongEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<ObjectType> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public ObjectType Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = _buffer.GetLong(_pos);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
60 changes: 60 additions & 0 deletions net/FlatBuffers/Enumerables/FlatBufferObjectEnumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FlatBuffers
{
public class FlatBufferObjectEnumerable<T> : IEnumerable<T>, IEnumerator<T>
where T : Table, new()
{
private int _pos;
private readonly int _end;
private readonly ByteBuffer _buffer;
private const int Size = sizeof(int);

public FlatBufferObjectEnumerable(int pos, int length, ByteBuffer buffer)
{
_pos = pos;
_end = pos + Size * length;
_buffer = buffer;
}

public IEnumerator<T> GetEnumerator()
{
return this;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public T Current { get; private set; }

public void Dispose()
{
}

object IEnumerator.Current
{
get { return Current; }
}

public bool MoveNext()
{
if (_pos >= _end)
{
return false;
}
Current = new T();
Current.Initialize(_pos, _buffer);
_pos += Size;
return true;
}

public void Reset()
{
throw new NotSupportedException();
}
}
}
Loading

0 comments on commit 952428d

Please sign in to comment.