Changed LLUUID's implementation to be a struct wrapping 'Guid', for performance

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@1094 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
bushing
2007-04-01 19:20:08 +00:00
parent 9250dbe67f
commit 4bdfd53fd7
5 changed files with 79 additions and 212 deletions

View File

@@ -435,7 +435,7 @@ namespace libsecondlife.AssetSystem
try
{
uint id = UInt32.Parse(fields[0]);
LLUUID texture = LLUUID.Parse(fields[1]);
LLUUID texture = new LLUUID(fields[1]);
_Textures[id] = texture;
}

View File

@@ -129,7 +129,7 @@ namespace libsecondlife
}
else if (obj is LLUUID)
{
LLUUID u = obj as LLUUID;
LLUUID u = (LLUUID) obj;
writer.WriteStartElement(String.Empty, "uuid", String.Empty);
writer.WriteString(u.ToStringHyphenated());
writer.WriteEndElement();

View File

@@ -1,9 +1,8 @@
/*
* Copyright (c) 2006, Second Life Reverse Engineering Team
* All rights reserved.
* Copyright (c) 2006, 2007 Second Life Reverse Engineering Team
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
@@ -35,34 +34,23 @@ namespace libsecondlife
/// A 128-bit Universally Unique Identifier, used throughout the Second
/// Life networking protocol
/// </summary>
[Serializable]
public class LLUUID : IXmlSerializable
{
/// <summary>Get a byte array of the 16 raw bytes making up the UUID</summary>
public byte[] Data { get { return data; } }
/// <summary>The 16 bytes that make up the UUID</summary>
protected byte[] data;
/// <summary>
/// Default constructor
/// </summary>
public LLUUID()
{
data = new byte[16];
}
/// <summary>
/// Constructor that takes a string UUID representation
/// </summary>
/// <param name="val">A string representation of a UUID, case
/// insensitive and can either be hyphenated or non-hyphenated</param>
/// <example>LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489")</example>
public LLUUID(string val)
{
data = StringToBytes(val);
}
public struct LLUUID
{
/// <summary>The Guid we are wrapping to make an LLUUID</summary>
public Guid id;
/// <summary>Get a byte array of the 16 raw bytes making up the UUID</summary>
public byte[] Data { get { return id.ToByteArray(); } }
/// <summary>
/// Constructor that takes a string UUID representation
/// </summary>
/// <param name="val">A string representation of a UUID, case
/// insensitive and can either be hyphenated or non-hyphenated</param>
/// <example>LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489")</example>
public LLUUID(string val) {
id = new Guid(val);
}
/// <summary>
/// Constructor that takes a byte array containing a UUID
@@ -71,8 +59,9 @@ namespace libsecondlife
/// <param name="pos">Beginning offset in the array</param>
public LLUUID(byte[] byteArray, int pos)
{
data = new byte[16];
byte[] data = new byte[16];
Array.Copy(byteArray, pos, data, 0, 16);
id=new Guid(data);
}
/// <summary>
@@ -82,9 +71,10 @@ namespace libsecondlife
/// <param name="val">64-bit unsigned integer to convert to a UUID</param>
public LLUUID(ulong val)
{
data = new byte[16];
byte[] data = new byte[16];
byte[] bytes = BitConverter.GetBytes(val);
Array.Copy(bytes, data, bytes.Length);
id=new Guid(data);
}
/// <summary>
@@ -93,24 +83,24 @@ namespace libsecondlife
/// <returns>A 16 byte array containing this UUID</returns>
public byte[] GetBytes()
{
return data;
return Data;
}
/// <summary>
/// Calculate an LLCRC (cyclic redundancy check) for this LLUUID
/// </summary>
/// <returns>The CRC checksum for this LLUUID</returns>
public uint CRC()
{
uint retval = 0;
byte[] _data=GetBytes();
retval += (uint)((_data[3] << 24) + (_data[2] << 16) + (_data[1] << 8) + _data[0]);
retval += (uint)((_data[7] << 24) + (_data[6] << 16) + (_data[5] << 8) + _data[4]);
retval += (uint)((_data[11] << 24) + (_data[10] << 16) + (_data[9] << 8) + _data[8]);
retval += (uint)((_data[15] << 24) + (_data[14] << 16) + (_data[13] << 8) + _data[12]);
/// <summary>
/// Calculate an LLCRC (cyclic redundancy check) for this LLUUID
/// </summary>
/// <returns>The CRC checksum for this LLUUID</returns>
public uint CRC()
{
uint retval = 0;
retval += (uint)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]);
retval += (uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]);
retval += (uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]);
retval += (uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]);
return retval;
}
return retval;
}
/// <summary>
/// Combine two UUIDs together by taking the MD5 hash of a byte array
@@ -122,43 +112,12 @@ namespace libsecondlife
{
// Build the buffer to MD5
byte[] input = new byte[32];
Array.Copy(data, input, 16);
Array.Copy(other.Data, 0, input, 16, 16);
Array.Copy(GetBytes(), input, 16);
Array.Copy(other.GetBytes(), 0, input, 16, 16);
return new LLUUID(Helpers.MD5Builder.ComputeHash(input), 0);
}
/// <summary>
/// Parse the bytes for a LLUUID from a string
/// </summary>
/// <param name="val">String containing 32 (or 36 with hyphens) character UUID</param>
/// <returns></returns>
protected static byte[] StringToBytes(string val)
{
if (val.Length == 36) val = val.Replace("-", "");
if (val.Length != 32) throw new Exception("Malformed LLUUID: " + val);
byte[] parseData = new byte[16];
for (int i = 0; i < 16; ++i)
{
parseData[i] = Convert.ToByte(val.Substring(i * 2, 2), 16);
}
return parseData;
}
/// <summary>
/// Generate a LLUUID from a string
/// </summary>
/// <param name="val">A string representation of a UUID, case
/// insensitive and can either be hyphenated or non-hyphenated</param>
/// <example>LLUUID.Parse("11f8aa9c-b071-4242-836b-13b7abe0d489")</example>
public static LLUUID Parse(string val)
{
return new LLUUID(StringToBytes(val), 0);
}
/// <summary>
/// Generate a LLUUID from a string
/// </summary>
@@ -172,12 +131,12 @@ namespace libsecondlife
{
try
{
result = Parse(val);
result = new LLUUID(val);
return true;
}
catch (Exception)
{
result = null;
result = new LLUUID(0);
return false;
}
}
@@ -191,64 +150,6 @@ namespace libsecondlife
return new LLUUID(Guid.NewGuid().ToByteArray(), 0);
}
/// <summary>
/// Required implementation for XML serialization
/// </summary>
/// <returns>null</returns>
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Deserializes an XML UUID
/// </summary>
/// <param name="reader">XmlReader containing the UUID to deserialize</param>
public void ReadXml(System.Xml.XmlReader reader)
{
string val = reader.ReadString();
if (val.Length > 0)
{
if (val.Length == 36) val = val.Replace("-", "");
if (val.Length != 32) throw new Exception("Malformed data passed to LLUUID constructor: " + val);
for (int i = 0; i < 16; ++i)
{
data[i] = Convert.ToByte(val.Substring(i * 2, 2), 16);
}
}
reader.Skip();
}
/// <summary>
/// Serialize this UUID to XML
/// </summary>
/// <param name="writer">XmlWriter to serialize to</param>
public void WriteXml(System.Xml.XmlWriter writer)
{
if (this != LLUUID.Zero)
writer.WriteString(this.ToString());
}
/// <summary>
/// Return a hash code for this UUID, used by .NET for hash tables
/// </summary>
/// <returns>An integer composed of all the UUID bytes XORed together</returns>
public override int GetHashCode()
{
int hash = data[0];
for (int i = 1; i < 16; i++)
{
hash ^= data[i];
}
return hash;
}
/// <summary>
/// Comparison function
/// </summary>
@@ -261,12 +162,7 @@ namespace libsecondlife
LLUUID uuid = (LLUUID)o;
for (int i = 0; i < 16; ++i)
{
if (Data[i] != uuid.Data[i]) return false;
}
return true;
return uuid.id.Equals(id);
}
/// <summary>
@@ -275,38 +171,21 @@ namespace libsecondlife
/// <param name="lhs">First LLUUID for comparison</param>
/// <param name="rhs">Second LLUUID for comparison</param>
/// <returns>True if the UUIDs are byte for byte equal, otherwise false</returns>
public static bool operator==(LLUUID lhs, LLUUID rhs)
{
// If both are null, or both are same instance, return true
if (System.Object.ReferenceEquals(lhs, rhs))
{
return true;
}
// If one is null, but not both, return false.
if (((object)lhs == null) || ((object)rhs == null))
{
return false;
}
for (int i = 0; i < 16; ++i)
{
if (lhs.Data[i] != rhs.Data[i]) return false;
}
return true;
}
public static bool operator==(LLUUID lhs, LLUUID rhs)
{
return lhs.Equals(rhs);
}
/// <summary>
/// Not equals operator
/// </summary>
/// <param name="lhs">First LLUUID for comparison</param>
/// <param name="rhs">Second LLUUID for comparison</param>
/// <returns>True if the UUIDs are not equal, otherwise true</returns>
public static bool operator!=(LLUUID lhs, LLUUID rhs)
{
return !(lhs == rhs);
}
public static bool operator!=(LLUUID lhs, LLUUID rhs)
{
return !(lhs == rhs);
}
/// <summary>
/// XOR operator
@@ -332,10 +211,10 @@ namespace libsecondlife
/// <param name="val">A UUID in string form. Case insensitive,
/// hyphenated or non-hyphenated</param>
/// <returns>A UUID built from the string representation</returns>
public static implicit operator LLUUID(string val)
{
return new LLUUID(val);
}
public static implicit operator LLUUID(string val)
{
return new LLUUID(val);
}
/// <summary>
/// Get a string representation of this UUID
@@ -343,17 +222,10 @@ namespace libsecondlife
/// <returns>A string representation of this UUID, lowercase and
/// without hyphens</returns>
/// <example>11f8aa9cb0714242836b13b7abe0d489</example>
public override string ToString()
{
string uuid = String.Empty;
for (int i = 0; i < 16; ++i)
{
uuid += Data[i].ToString("x2");
}
return uuid;
}
public override string ToString()
{
return id.ToString("N");
}
/// <summary>
/// Get a hyphenated string representation of this UUID
@@ -361,27 +233,21 @@ namespace libsecondlife
/// <returns>A string representation of this UUID, lowercase and
/// with hyphens</returns>
/// <example>11f8aa9c-b071-4242-836b-13b7abe0d489</example>
public string ToStringHyphenated()
{
string uuid = String.Empty;
for (int i = 0; i < 16; ++i)
{
uuid += Data[i].ToString("x2");
}
uuid = uuid.Insert(20, "-");
uuid = uuid.Insert(16, "-");
uuid = uuid.Insert(12, "-");
uuid = uuid.Insert(8, "-");
return uuid;
}
public string ToStringHyphenated()
{
return id.ToString("D");
}
public override int GetHashCode()
{
return id.GetHashCode();
}
/// <summary>
/// An LLUUID with a value of all zeroes
/// </summary>
public static readonly LLUUID Zero = new LLUUID();
}
}
/// <summary>
/// A three-dimensional vector with floating-point values

View File

@@ -297,7 +297,7 @@ namespace libsecondlife.Utilities.Appearance
try
{
int id = Int32.Parse(fields[0]);
LLUUID texture = LLUUID.Parse(fields[1]);
LLUUID texture = new LLUUID(fields[1]);
Textures[id] = texture;
}

View File

@@ -114,11 +114,12 @@
</target>
<target name="test-dll" depends="init build" description="makes the test dll">
<csc target="library" output="tests/tests.dll">
<csc target="library" output="libsecondlife.Tests/tests.dll">
<sources>
<include name="tests/*.cs" />
<include name="libsecondlife.Tests/*.cs" />
</sources>
<references>
<include name="libsecondlife.dll" />
<include name="NUnit.dll" />
</references>
</csc>