Skip to content

Instantly share code, notes, and snippets.

@Konard
Created May 8, 2014 15:07
Show Gist options
  • Save Konard/d29f86aa17bf8c531a32 to your computer and use it in GitHub Desktop.
Save Konard/d29f86aa17bf8c531a32 to your computer and use it in GitHub Desktop.
Структура описывающая уникальную пространственно-временную зависимость.
using System;
namespace Links.Core.Structures
{
/// <summary>
/// Структура описывающая уникальную пространственно-временную зависимость.
/// </summary>
public struct Unique : IEquatable<Unique>
{
private static readonly Random SeedFactory = new Random((int)DateTime.UtcNow.Ticks);
public static readonly Unique Null = new Unique(0, 0);
public readonly ulong Time;
public readonly ulong Seed;
public Unique(ulong time, ulong seed)
{
Time = time;
Seed = seed;
}
static public Unique Create()
{
var time = (ulong)DateTime.UtcNow.Ticks;
var rndBytes = new byte[8];
SeedFactory.NextBytes(rndBytes);
var seed = BitConverter.ToUInt64(rndBytes, 0);
return new Unique(time, seed);
}
static public Unique Create(ulong source, ulong target)
{
return new Unique(source, target);
}
public override int GetHashCode()
{
var hash = 17;
hash = hash * 31 + (int)Time;
hash = hash * 31 + (int)Seed;
return hash;
}
public override bool Equals(object other)
{
return other is Unique && Equals((Unique)other);
}
public bool Equals(Unique other)
{
return Time == other.Time &&
Seed == other.Seed;
}
public override string ToString()
{
return string.Format("[{0},{1}]", new DateTime((long)Time), Seed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment