z3-z3-4.13.0.src.api.dotnet.Statistics.cs Maven / Gradle / Ivy
The newest version!
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
Statistics.cs
Abstract:
Z3 Managed API: Statistics
Author:
Christoph Wintersteiger (cwinter) 2012-03-22
Notes:
--*/
using System;
using System.Diagnostics;
namespace Microsoft.Z3
{
using Z3_context = System.IntPtr;
using Z3_stats = System.IntPtr;
///
/// Objects of this class track statistical information about solvers.
///
public class Statistics : Z3Object
{
///
/// Statistical data is organized into pairs of [Key, Entry], where every
/// Entry is either a DoubleEntry or a UIntEntry
///
public class Entry
{
///
/// The key of the entry.
///
readonly public string Key;
///
/// The uint-value of the entry.
///
public uint UIntValue { get { return m_uint; } }
///
/// The double-value of the entry.
///
public double DoubleValue { get { return m_double; } }
///
/// True if the entry is uint-valued.
///
public bool IsUInt { get { return m_is_uint; } }
///
/// True if the entry is double-valued.
///
public bool IsDouble { get { return m_is_double; } }
///
/// The string representation of the entry's value.
///
public string Value
{
get
{
if (IsUInt)
return m_uint.ToString();
else if (IsDouble)
return m_double.ToString();
else
throw new Z3Exception("Unknown statistical entry type");
}
}
///
/// The string representation of the Entry.
///
public override string ToString()
{
return Key + ": " + Value;
}
#region Internal
readonly private bool m_is_uint = false;
readonly private bool m_is_double = false;
readonly private uint m_uint = 0;
readonly private double m_double = 0.0;
internal Entry(string k, uint v)
{
Key = k;
m_is_uint = true;
m_uint = v;
}
internal Entry(string k, double v)
{
Key = k;
m_is_double = true;
m_double = v;
}
#endregion
}
///
/// A string representation of the statistical data.
///
public override string ToString()
{
return Native.Z3_stats_to_string(Context.nCtx, NativeObject);
}
///
/// The number of statistical data.
///
public uint Size
{
get { return Native.Z3_stats_size(Context.nCtx, NativeObject); }
}
///
/// The data entries.
///
public Entry[] Entries
{
get
{
return NativeEntries(Context.nCtx, NativeObject);
}
}
internal static Entry[] NativeEntries(Z3_context ctx, Z3_stats stats)
{
uint n = Native.Z3_stats_size(ctx, stats);
Entry[] res = new Entry[n];
for (uint i = 0; i < n; i++)
{
Entry e;
string k = Native.Z3_stats_get_key(ctx, stats, i);
if (Native.Z3_stats_is_uint(ctx, stats, i) != 0)
e = new Entry(k, Native.Z3_stats_get_uint_value(ctx, stats, i));
else if (Native.Z3_stats_is_double(ctx, stats, i) != 0)
e = new Entry(k, Native.Z3_stats_get_double_value(ctx, stats, i));
else
throw new Z3Exception("Unknown data entry value");
res[i] = e;
}
return res;
}
///
/// The statistical counters.
///
public string[] Keys
{
get
{
uint n = Size;
string[] res = new string[n];
for (uint i = 0; i < n; i++)
res[i] = Native.Z3_stats_get_key(Context.nCtx, NativeObject, i);
return res;
}
}
///
/// The value of a particular statistical counter.
///
/// Returns null if the key is unknown.
public Entry this[string key]
{
get
{
uint n = Size;
Entry[] es = Entries;
for (uint i = 0; i < n; i++)
if (es[i].Key == key)
return es[i];
return null;
}
}
#region Internal
internal Statistics(Context ctx, IntPtr obj)
: base(ctx, obj)
{
Debug.Assert(ctx != null);
}
internal override void IncRef(IntPtr o)
{
Native.Z3_stats_inc_ref(Context.nCtx, o);
}
internal override void DecRef(IntPtr o)
{
lock (Context)
{
if (Context.nCtx != IntPtr.Zero)
Native.Z3_stats_dec_ref(Context.nCtx, o);
}
}
#endregion
}
}