using System;
using System.Collections.Generic;
using System.Text;
namespace fastBinaryJSON
{
internal sealed class SafeDictionary<TKey, TValue>
{
private readonly object _Padlock = new object();
private readonly Dictionary<TKey, TValue> _Dictionary = new Dictionary<TKey, TValue>();
public bool TryGetValue(TKey key, out TValue value)
{
return _Dictionary.TryGetValue(key, out value);
}
public TValue this[TKey key]
{
get
{
return _Dictionary[key];
}
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey, TValue>>)_Dictionary).GetEnumerator();
}
public void Add(TKey key, TValue value)
{
lock (_Padlock)
{
if (_Dictionary.ContainsKey(key) == false)
_Dictionary.Add(key, value);
}
}
}
internal static class Helper
{
internal static unsafe int ToInt32(byte[] value, int startIndex, bool reverse)
{
if (reverse)
{
byte[] b = new byte[4];
Buffer.BlockCopy(value, startIndex, b, 0, 4);
Array.Reverse(b);
return ToInt32(b, 0);
}
return ToInt32(value, startIndex);
}
internal static unsafe int ToInt32(byte[] value, int startIndex)
{
fixed (byte* numRef = &(value[startIndex]))
{
return *((int*)numRef);
}
}
internal static unsafe long ToInt64(byte[] value, int startIndex, bool reverse)
{
if (reverse)
{
byte[] b = new byte[8];
Buffer.BlockCopy(value, startIndex, b, 0, 8);
Array.Reverse(b);
return ToInt64(b, 0);
}
return ToInt64(value, startIndex);
}
internal static unsafe long ToInt64(byte[] value, int startIndex)
{
fixed (byte* numRef = &(value[startIndex]))
{
return *(((long*)numRef));
}
}
internal static unsafe short ToInt16(byte[] value, int startIndex, bool reverse)
{
if (reverse)
{
byte[] b = new byte[2];
Buffer.BlockCopy(value, startIndex, b, 0, 2);
Array.Reverse(b);
return ToInt16(b, 0);
}
return ToInt16(value, startIndex);
}
internal static unsafe short ToInt16(byte[] value, int startIndex)
{
fixed (byte* numRef = &(value[startIndex]))
{
return *(((short*)numRef));
}
}
internal static unsafe byte[] GetBytes(long num, bool reverse)
{
byte[] buffer = new byte[8];
fixed (byte* numRef = buffer)
{
*((long*)numRef) = num;
}
if (reverse)
Array.Reverse(buffer);
return buffer;
}
public static unsafe byte[] GetBytes(int num, bool reverse)
{
byte[] buffer = new byte[4];
fixed (byte* numRef = buffer)
{
*((int*)numRef) = num;
}
if (reverse)
Array.Reverse(buffer);
return buffer;
}
}
}