forked from LANDIS-II-Foundation/Library-Age-Cohort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
117 lines (109 loc) · 3.87 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
namespace Landis.Library.AgeOnlyCohorts
{
/// <summary>
/// Utility methods for age cohorts.
/// </summary>
public static class Util
{
/// <summary>
/// Gets the maximum age among a species' cohorts.
/// </summary>
/// <returns>
/// The age of the oldest cohort or 0 if there are no cohorts.
/// </returns>
public static ushort GetMaxAge(ISpeciesCohorts cohorts)
{
if (cohorts == null)
return 0;
ushort max = 0;
foreach (ICohort cohort in cohorts) {
// First cohort is the oldest
max = cohort.Age;
break;
}
return max;
}
//---------------------------------------------------------------------
/// <summary>
/// Gets the maximum age among all cohorts at a site.
/// </summary>
/// <returns>
/// The age of the oldest cohort or 0 if there are no cohorts.
/// </returns>
//public static ushort GetMaxAge(ISiteCohorts<ISpeciesCohorts<ICohort>> cohorts)
public static ushort GetMaxAge(ISiteCohorts cohorts)
{
if (cohorts == null)
return 0;
ushort max = 0;
foreach (ISpeciesCohorts speciesCohorts in cohorts)
{
ushort maxSpeciesAge = GetMaxAge(speciesCohorts);
if (maxSpeciesAge > max)
max = maxSpeciesAge;
}
return max;
}
//---------------------------------------------------------------------
/// <summary>
/// Compares the ages of two cohorts to determine which is older.
/// </summary>
/// <returns>
/// <list type="">
/// <item>
/// A negative value if x is older than y.
/// </item>
/// <item>
/// 0 if x and y are the same age.
/// </item>
/// <item>
/// A positive value if x is younger than y.
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// This method matches the signature for the System.Comparison
/// delegate so it can be used to sort an arrary or list from oldest
/// to youngest. Sort methods require that the delegate return a
/// negative value if x comes before y in the sort order, 0 if they are
/// equivalent, and a positive value is x comes after y in the sort
/// order.
/// </remarks>
public static int WhichIsOlderCohort(ICohort x,
ICohort y)
{
return WhichIsOlder(x.Age, y.Age);
}
//---------------------------------------------------------------------
/// <summary>
/// Compares two cohort ages to determine which is older.
/// </summary>
/// <returns>
/// <list type="">
/// <item>
/// A negative value if x is older than y.
/// </item>
/// <item>
/// 0 if x and y are the same age.
/// </item>
/// <item>
/// A positive value if x is younger than y.
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// This method matches the signature for the System.Comparison
/// delegate so it can be used to sort an arrary or list of ages from
/// oldest to youngest. Sort methods require that the delegate return
/// a negative value if x comes before y in the sort order, 0 if they
/// are equivalent, and a positive value is x comes after y in the sort
/// order.
/// </remarks>
public static int WhichIsOlder(ushort x,
ushort y)
{
return y - x;
}
}
}