Re: The code for "sorting in C sharp considered superior" in five parts for easier download
- From: spinoza1111 <spinoza1111@xxxxxxxxx>
- Date: Mon, 25 Feb 2008 01:44:35 -0800 (PST)
// ***** Part five of five: caution: starts with a tight brace *****
}
//
--------------------------------------------------------
// Return stopwatch value (milliseconds as a real
number)
//
//
public double StopwatchValue
{
get
{
if (!checkUsable_("StopwatchValue get",
"No change made: returning
zero")
||
!checkActive_("StopwatchValue get",
"No change made: returning
zero"))
return 0;
if (!OBJstopwatch.IsRunning)
{
utilities.errorHandler
("Stopwatch is not running",
CLASS_NAME,
"stopwatchValue()",
"Returning 0",
null);
return 0;
}
return OBJstopwatch.Elapsed.TotalMilliseconds;
}
}
//
--------------------------------------------------------
// Convert times to string
//
//
public string toString()
{
if (!checkUsable_("", ""))
return UNUSABILITY_MESSAGE;
if (!Active || !DataAvailable)
return INSUFFICIENT_INFORMATION;
return "Average time (milliseconds) per sort job:
" +
time2String_(DBLtotalTime /
(double)LNGtotalJobs) +
Environment.NewLine +
"Average time (milliseconds) " +
"per sorted element: " +
time2String_(DBLtotalTime /
(double)LNGtotalElements);
}
//
--------------------------------------------------------
// Tell caller if structure instance is usable
//
//
public bool Usable
{
get { return BOOusable; }
}
// ***** Private procedures
*******************************
//
--------------------------------------------------------
// Make sure structure instance is "active" and has a
// timer
//
//
private bool checkActive_(string strProcedure,
string strHelp)
{
if (Active) return true;
return utilities.errorHandler
("Structure instance is not active",
STRUCT_NAME,
strProcedure,
strHelp,
null);
}
//
--------------------------------------------------------
// Make sure structure instance is "usable"
//
//
private bool checkUsable_(string strProcedure,
string strHelp)
{
if (BOOusable) return true;
return utilities.errorHandler
("Structure instance is not usable",
STRUCT_NAME,
strProcedure,
strHelp,
null);
}
//
--------------------------------------------------------
// Set the overflow indicator values
//
//
private void indicateOverflow_()
{
DBLtotalTime = OVERFLOW;
LNGtotalElements = (long)OVERFLOW;
LNGtotalJobs = (long)OVERFLOW;
}
//
--------------------------------------------------------
// Internal inspection
//
//
private bool inspection_()
{
string strReport = "";
if (inspect(ref strReport)) return true;
return utilities.errorHandler
("Structure has failed an internal
inspection",
STRUCT_NAME,
"inspection_()",
"Structure instance is unusable",
null);
}
//
--------------------------------------------------------
// Make structure instance unusable
//
//
private bool mkUnusable_()
{
BOOusable = false; return true;
}
//
--------------------------------------------------------
// Convert time to string
//
//
private string time2String_(double dblTime)
{
if (dblTime < 10e-10) return "near zero";
return dblTime.ToString();
}
}
}
//
****************************************************************
//
* *
// *
utilities *
//
* *
//
****************************************************************
private static class utilities
{
//
------------------------------------------------------------
// Error handler
//
//
public static bool errorHandler
(string strMessage,
string strClassOrObject,
string strProcedure,
string strHelp)
{
return errorHandler(strMessage,
strClassOrObject,
strProcedure,
strHelp,
null,
false);
}
public static bool errorHandler
(string strMessage,
string strClassOrObject,
string strProcedure,
string strHelp,
Exception objException)
{
return errorHandler(strMessage,
strClassOrObject,
strProcedure,
strHelp,
objException,
false);
}
public static bool errorHandler
(string strMessage,
string strClassOrObject,
string strProcedure,
string strHelp,
Exception objException,
bool booFinal)
{
string strFull =
separateStrings
(separateStrings
("Error in " +
strClassOrObject +
"." +
strProcedure + ": ",
strMessage),
separateStrings
(strHelp,
objException == null
?
""
:
objException.ToString()));
if (booFinal)
MessageBox.Show(strFull);
else
{
Exception objInnerException = null;
try
{
objInnerException =
new Exception(strFull);
}
catch
{
MessageBox.Show
(separateStrings
("The following exception " +
"cannot be created: " +
"terminating",
strFull));
Application.Exit();
}
throw objInnerException;
}
return false;
}
//
----------------------------------------------------------------------
// Inspection and test append
//
//
public static bool inspectionAppend
(ref string strReport,
string strRule,
bool booRuleResult,
ref bool booInspection)
{
return inspectionAppend(ref strReport,
strRule,
booRuleResult,
ref booInspection,
"");
}
public static bool inspectionAppend
(ref string strReport,
string strRule,
bool booRuleResult,
ref bool booInspection,
string strComments)
{
strReport = separateStrings
(separateStrings
(strReport,
strRule + ": " +
(booRuleResult ? "OK" : "Failed")),
strComments);
booInspection = booRuleResult && booInspection;
return booRuleResult;
}
//
--------------------------------------------------------
// Separate two strings
//
//
// --- Separator defaults to one newline (if neither
string
// --- contains a newline) or two newlines
public static string separateStrings(string strString1,
string strString2)
{
return separateStrings
(strString1,
strString2,
Environment.NewLine +
(strString1.IndexOf(Environment.NewLine) == -1
&&
strString1.IndexOf(Environment.NewLine) == -1
?
""
:
Environment.NewLine));
}
// --- Separator is specified
public static string separateStrings(string strString1,
string strString2,
string strSeparator)
{
return strString1 +
(strString1.Trim() != ""
&&
strString2.Trim() != ""
?
strSeparator
:
"") +
strString2;
}
}
//
************************************************************
// *
*
// * windowsUtilities
*
// *
*
//
************************************************************
private static class windowsUtilities
{
//
--------------------------------------------------------
// Make button
//
//
public static Button mkButton(string strText,
int intLeft, int intTop,
int intWidth, int intHeight)
{
try
{
Button cmdNew = new Button();
cmdNew.Left = intLeft; cmdNew.Top = intTop;
cmdNew.Width = intWidth; cmdNew.Height =
intHeight;
cmdNew.Refresh();
return cmdNew;
}
catch (Exception objException)
{
utilities.errorHandler("Can't make button",
"windowsUtilities",
"mkButton",
"Returning null",
objException);
return null;
}
}
//
--------------------------------------------------------
// Return the screen height
//
//
// This method returns the height of the primary screen
as
// its function value (see also screenWidth and
// screenSize). It will return -1 for the height if there
// is no primary screen. It will return -2 for the height
// if for some sillyassed reason, there are multiple
// primary screens.
//
// The width will be the working width. In particular,
// note that the height will not include the space
occupied
// by a visible Task bar.
//
//
public static int screenHeight()
{
int intHeight = 0;
int intWidth = 0;
screenSize(ref intWidth, ref intHeight);
return intHeight;
}
//
--------------------------------------------------------
// Return the screen size
//
//
// This method returns the width and the height of the
// primary screen in reference parameters (see also
// screenWidth and screenHeight). It will return -1 for
// width and for height if there is no primary screen. It
// will return -2 for width and for height if for some
// sillyassed reason, there are multiple primary screens.
//
// Both the width and the height are the working width
// and height. In particular, note that the height will
not
// include the space occupied by a visible Task bar.
//
//
public static bool screenSize(ref int intWidth,
ref int intHeight)
{
int intIndex1;
System.Windows.Forms.Screen[] objScreens =
System.Windows.Forms.Screen.AllScreens;
intWidth = -1; intHeight = -1;
for (intIndex1 = 0;
intIndex1 <= objScreens.GetUpperBound(0);
intIndex1++)
{
if (objScreens[intIndex1].Primary)
{
if (intWidth == -1 && intHeight == -1)
{
intWidth =
objScreens[intIndex1].WorkingArea.Width;
intHeight =
objScreens[intIndex1].WorkingArea.Height;
}
else
{ intWidth = -2; intHeight = -2; }
}
}
switch (intWidth)
{
case -1:
{
return utilities.errorHandler
("Cannot find the screen
dimensions",
"windowsUtilities",
"screenSize()",
"Returning screen sizes as -1: "
+
"function return is false");
}
case -2:
{
return utilities.errorHandler
("Multiple screen dimensions
found",
"windowsUtilities",
"screenSize()",
"Returning screen sizes as -2: "
+
"function return is false");
}
default:
{
return true;
}
}
}
//
--------------------------------------------------------
// Return the screen height
//
//
// This method returns the height of the primary screen
as
// its function value (see also screenWidth and
// screenSize). It will return -1 for the height if there
// is no primary screen. It will return -2 for the height
// if for some sillyassed reason, there are multiple
// primary screens.
//
// The width will be the working width. In particular,
// note that the height will not include the space
occupied
// by a visible Task bar.
//
//
public static int screenWidth()
{
int intHeight = 0;
int intWidth = 0;
screenSize(ref intWidth, ref intHeight);
return intWidth;
}
}
}
}
.
- References:
- Prev by Date: Re: The code for "sorting in C sharp considered superior" in five parts for easier download
- Next by Date: Re: strlen(), K+1: clarification
- Previous by thread: Re: The code for "sorting in C sharp considered superior" in five parts for easier download
- Index(es):