Skip to content

ConfidenceStack Class

Naveen Dharmathunga edited this page Jan 19, 2026 · 1 revision

ConfidenceStack<T> Class

Represents a collection that tracks unique values and their associated confidence scores, allowing items to be added, updated, removed, and enumerated in order of descending score.

Namespace: Rheo.Storage.Analyzing.Models.Result

Assembly: Rheo.Storage.dll

Definition

public class ConfidenceStack<T> : IEnumerable<Confidence<T>>, ICollection<Confidence<T>> 
    where T : notnull

Type Parameters

T

The type of values stored in the collection. Must be non-nullable.

Constructors

Constructor Description
ConfidenceStack() Initializes a new instance of the ConfidenceStack<T> class.

Properties

Property Type Description
Item[Int32] Confidence<T> Gets the element at the specified index in the collection.
TotalScore Int32 Gets the sum of the score values for all items in the collection.
Count Int32 Gets the number of elements contained in the collection.
Values IEnumerable<T> Gets an enumerable collection containing all values in the collection.
IsReadOnly Boolean Gets a value indicating whether the collection is read-only.

Methods

Method Description
Push(T, Int32) Adds the specified value to the collection with the given score, or increases its score if it already exists.
Pop() Removes and returns the item with the highest confidence score from the collection.
Pop(T) Removes the specified subject from the collection and returns its associated confidence value.
Add(Confidence<T>) Adds a confidence instance to the collection.
Clear() Removes all items from the collection.
Contains(Confidence<T>) Determines whether the collection contains a specific confidence instance.
CopyTo(Confidence<T>[], Int32) Copies the elements to an array, starting at a particular index.
Remove(Confidence<T>) Removes a specific confidence instance from the collection.
ToList() Creates a list of confidence values for each item, ordered from highest to lowest confidence.
GetEnumerator() Returns an enumerator that iterates through the collection.
ToString() Returns a string representation of the collection.

Remarks

Each value in the collection is associated with an integer confidence score. Adding a value that already exists increases its score by the specified amount. Items can be removed individually or all at once, and enumeration yields results ordered by descending score. This collection is not thread-safe.

The confidence percentage for each item is calculated dynamically based on its score relative to the total score of all items in the collection.


Property Details

Item[Int32]

Gets the element at the specified index in the collection.

public Confidence<T> this[int index] { get; }

Parameters

index Int32

The zero-based index of the element to retrieve.

Property Value

Confidence<T>

The element at the specified index.

Remarks

The index is based on the sorted list (by descending confidence), not insertion order.


TotalScore

Gets the sum of the score values for all items in the collection.

public int TotalScore { get; }

Property Value

Int32

The total score of all items.

Remarks

This value is used to calculate the confidence percentage for each item.


Count

Gets the number of elements contained in the collection.

public int Count { get; }

Property Value

Int32

The number of elements in the collection.


Values

Gets an enumerable collection containing all values in the collection.

public IEnumerable<T> Values { get; }

Property Value

IEnumerable<T>

An enumerable of all values without confidence information.


IsReadOnly

Gets a value indicating whether the collection is read-only.

public bool IsReadOnly { get; }

Property Value

Boolean

Always returns false.


Method Details

Push(T, Int32)

Adds the specified value to the collection with the given score, or increases its score if it already exists.

public void Push(T value, int score = 1)

Parameters

value T

The value to add to the collection or whose score to increase.

score Int32

The score to associate with the value. Defaults to 1. Must be a positive integer.


Pop()

Removes and returns the item with the highest confidence score from the collection.

public Confidence<T>? Pop()

Returns

Confidence<T>?

A Confidence<T> representing the item with the highest confidence score, or null if the collection is empty.


Pop(T)

Removes the specified subject from the collection and returns its associated confidence value.

public Confidence<T> Pop(T subject)

Parameters

subject T

The subject to remove from the collection. Must exist in the collection.

Returns

Confidence<T>

A Confidence<T> instance representing the removed subject and its confidence percentage.

Exceptions

KeyNotFoundException

Thrown if the specified subject does not exist in the collection.


Add(Confidence<T>)

Adds a confidence instance to the collection.

public void Add(Confidence<T> item)

Parameters

item Confidence<T>

The confidence instance to add.


Clear()

Removes all items from the collection.

public void Clear()

Contains(Confidence<T>)

Determines whether the collection contains a specific confidence instance.

public bool Contains(Confidence<T> item)

Parameters

item Confidence<T>

The confidence instance to locate.

Returns

Boolean

true if the item's subject is found in the collection; otherwise, false.


CopyTo(Confidence<T>[], Int32)

Copies the elements to an array, starting at a particular index.

public void CopyTo(Confidence<T>[] array, int arrayIndex)

Parameters

array Confidence<T>[]

The destination array.

arrayIndex Int32

The zero-based index at which copying begins.


Remove(Confidence<T>)

Removes a specific confidence instance from the collection.

public bool Remove(Confidence<T> item)

Parameters

item Confidence<T>

The confidence instance to remove.

Returns

Boolean

true if the item was successfully removed; otherwise, false.


ToList()

Creates a list of confidence values for each item, ordered from highest to lowest confidence.

public List<Confidence<T>> ToList()

Returns

List<Confidence<T>>

A list of Confidence<T> objects sorted by descending confidence value.

Remarks

The returned list is a snapshot of the collection at the time of access. Subsequent changes to the underlying collection are not reflected in the returned list.


GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<Confidence<T>> GetEnumerator()

Returns

IEnumerator<Confidence<T>>

An enumerator for the collection.


ToString()

Returns a string representation of the collection.

public override string ToString()

Returns

String

A string showing the count and total score.


Examples

Creating and Using a ConfidenceStack

The following example demonstrates basic usage:

using Rheo.Storage.Analyzing.Models.Result;

var stack = new ConfidenceStack<string>();

stack.Push("PNG", 1000);
stack.Push("JPEG", 500);
stack.Push("GIF", 250);

Console.WriteLine($"Total Score: {stack.TotalScore}");
Console.WriteLine($"Count: {stack.Count}");

foreach (var confidence in stack)
{
    Console.WriteLine(confidence);
}
// Output:
// PNG (57.14%)
// JPEG (28.57%)
// GIF (14.29%)

Updating Scores

The following example shows how to update scores:

using Rheo.Storage.Analyzing.Models.Result;

var stack = new ConfidenceStack<string>();

stack.Push("PNG", 100);
stack.Push("PNG", 50);  // Adds to existing score
stack.Push("JPEG", 75);

var topItem = stack[0];
Console.WriteLine($"{topItem.Subject}: {topItem.Value:F2}%");
// Output: PNG: 66.67%

Removing Items

The following example demonstrates removing items:

using Rheo.Storage.Analyzing.Models.Result;

var stack = new ConfidenceStack<string>();
stack.Push("PNG", 100);
stack.Push("JPEG", 75);
stack.Push("GIF", 50);

// Remove top item
var top = stack.Pop();
Console.WriteLine($"Removed: {top}");

// Remove specific item
var jpeg = stack.Pop("JPEG");
Console.WriteLine($"Removed: {jpeg}");

Console.WriteLine($"Remaining: {stack.Count} items");

Using with File Analysis

The following example shows typical usage with file analysis results:

using Rheo.Storage.Analyzing;
using Rheo.Storage.Analyzing.Models.Result;

string filePath = @"C:\Documents\image.png";
AnalysisResult result = FileAnalyzer.AnalyzeFile(filePath);

var definitions = result.Definitions;

Console.WriteLine($"Found {definitions.Count} matches");
Console.WriteLine($"Total Score: {definitions.TotalScore}");

// Get top 3 matches
var top3 = definitions.ToList().Take(3);
foreach (var match in top3)
{
    Console.WriteLine($"{match.Subject.FileType}: {match.Value:F2}%");
}

Thread Safety

This class is not thread-safe. When accessing instances from multiple threads, use appropriate synchronization mechanisms.

See Also

Clone this wiki locally