|
# region Heading
/**************************************************************************************************************/
/* */
/* UniqueInt.cs */
/* */
/* Implements a class to hold an int with a unique value. */
/* */
/* This is free code, use it as you require. If you modify it please use your own namespace. */
/* */
/* If you like it or have suggestions for improvements please let me know at: PIEBALDconsult@aol.com */
/* */
/* Modification history: */
/* 2009-07-20 Sir John E. Boucher Created */
/* */
/**************************************************************************************************************/
# endregion
namespace PIEBALD.Types
{
/**
<summary>
Implements a class to hold an int with a unique value.
</summary>
<remarks>
Uses a System.Collections.Generic.HashSet<int> to keep track of used values.
</remarks>
*/
public sealed class UniqueInt : System.IDisposable
{
private System.Collections.Generic.HashSet<int> taken ;
private readonly int current ;
/**
<summary>
Instantiates a new UniqueInt.
</summary>
<param name="Value">
The initial value to try.
If the provided value is in use, the value will be incremented until an unused value is reached.
</param>
<param name="Taken">
The HashSet to use for checking uniqueness.
</param>
<exception cref="System.ArgumentNullException">
If Taken is null.
</exception>
*/
public UniqueInt
(
int Value
,
System.Collections.Generic.HashSet<int> Taken
)
{
if ( Taken == null )
{
throw ( new System.ArgumentNullException
(
"Taken"
,
"Taken must not be null"
) ) ;
}
this.taken = Taken ;
lock ( this.taken )
{
while ( this.taken.Contains ( Value ) )
{
Value++ ;
}
this.taken.Add ( this.current = Value ) ;
}
return ;
}
/**
<summary>
Removes the value from the HashSet and releases the HashSet.
</summary>
*/
public void
Dispose
(
)
{
if ( this.taken != null )
{
this.taken.Remove ( this.current ) ;
this.taken = null ;
}
return ;
}
/**
<summary>
Accesses the value.
</summary>
<value>
The value of the encapsulated int.
</value>
<exception cref="System.ObjectDisposedException">
If the instance has been disposed.
</exception>
*/
public int
Value
{
get
{
if ( this.taken == null )
{
throw ( new System.ObjectDisposedException
( "" , "This instance has been disposed" ) ) ;
}
return ( this.current ) ;
}
}
/**
<summary>
The Value as a string.
</summary>
<returns>
The Value as a string.
</returns>
<exception cref="System.ObjectDisposedException">
If the instance has been disposed.
</exception>
*/
public override string
ToString
(
)
{
return ( this.Value.ToString() ) ;
}
/**
<summary>
This is an implicit conversion operator from UniqueInt to int.
</summary>
<returns>
The Value.
</returns>
<exception cref="System.ObjectDisposedException">
If the instance has been disposed.
</exception>
*/
public static implicit operator
int
(
UniqueInt Op
)
{
return ( Op.Value ) ;
}
}
/**
<summary>
Implements a class to make working with UniqueInts a little easier.
</summary>
<remarks>
All int values from a particular factory will be unique.
</remarks>
*/
public sealed class UniqueIntFactory
{
private readonly System.Collections.Generic.HashSet<int> taken ;
/**
<summary>
Instantiate a HashSet and wrap it in a UniqueIntFactory.
</summary>
*/
public UniqueIntFactory
(
)
{
this.taken = new System.Collections.Generic.HashSet<int>() ;
return ;
}
/**
<summary>
Instantiate a new UniqueInt with this factory's HashSet and an initial value of zero (0).
</summary>
*/
public UniqueInt
NewValue
(
)
{
return ( new UniqueInt ( 0 , this.taken ) ) ;
}
/**
<summary>
Instantiate a new UniqueInt with this factory's HashSet.
</summary>
<param name="Value">
The initial value for the new UniqueInt.
</param>
*/
public UniqueInt
NewValue
(
int Value
)
{
return ( new UniqueInt ( Value , this.taken ) ) ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.