00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004 using System.Linq;
00005
00006 namespace StephenAshley.Biostatistics
00007 {
00008 using NUMBER = Decimal;
00009
00013 public class GroupsCollection : IEnumerable
00014 {
00015 #region ****** fields ******
00019 protected Group[] groupsArray;
00020
00025 private Group elementsGroup;
00026 #endregion
00027 #region *** constructors ***
00031 public GroupsCollection()
00032 {
00033 elementsGroup = new Group(0);
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00052 public GroupsCollection(List<Group> lstGroups)
00053 : this()
00054 {
00055 if (lstGroups == null)
00056 throw new BiostatisticsException(
00057 "groups parameter in constructor for GroupsCollection is null.");
00058
00059 groupsArray = lstGroups.ToArray();
00060 foreach (Group group in lstGroups)
00061 elementsGroup.AddRange(group.ToArray());
00062 }
00063 #endregion
00064 #region ***** methods ******
00071 public virtual void Add(Group group)
00072 {
00073 if (group == null)
00074 throw new BiostatisticsException(
00075 "group parameter in Add method of GroupsCollection is null.");
00076
00077
00078 List<Group> groupList = new List<Group>();
00079
00080
00081 if (groupsArray != null)
00082 groupList = groupsArray.ToList<Group>();
00083
00084 groupList.Add(group);
00085
00086 groupsArray = groupList.ToArray();
00087
00088 elementsGroup.AddRange(group.ToArray());
00089 }
00090
00098 public virtual void AddRange(List<Group> groups)
00099 {
00100 if (groups == null)
00101 throw new BiostatisticsException(
00102 "groups parameter in AddRange method of GroupsCollection is null.");
00103 if (groupsArray == null)
00104 groupsArray = new Group[groups.Count];
00105 List<Group> groupList = groupsArray.ToList<Group>();
00106 groupList.AddRange(groups);
00107 groupsArray = groupList.ToArray();
00108 foreach (Group group in groups)
00109 elementsGroup.AddRange(group.ToArray());
00110 }
00111
00116 public IEnumerator GetEnumerator()
00117 {
00118 return groupsArray.GetEnumerator();
00119 }
00120
00127 public Int32 k()
00128 {
00129 if (groupsArray == null)
00130 return 0;
00131 else
00132 return groupsArray.Length;
00133 }
00134
00141 public Int32 nTotal()
00142 {
00143 return elementsGroup.n();
00144 }
00145
00153 public NUMBER MeanTotal()
00154 { return elementsGroup.Mean(); }
00155
00160 public NUMBER SumTotal()
00161 { return elementsGroup.Sum(); }
00162
00171 public Group this[int index]
00172 {
00173 get
00174 {
00175 Group group;
00176 try
00177 {
00178 group = groupsArray[index];
00179 }
00180 catch (Exception e)
00181 {
00182 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00183 }
00184 return groupsArray[index];
00185 }
00186 set
00187 {
00188 try
00189 {
00190 groupsArray[index] = value;
00191 }
00192 catch (Exception e)
00193 {
00194 throw new BiostatisticsException(e.Source + ": " + e.Message, e);
00195 }
00196 elementsGroup = new Group(0);
00197 foreach (Group group in elementsGroup)
00198 elementsGroup.AddRange(group.ToArray());
00199 }
00200 }
00201 #endregion
00202
00203 }
00204 }