vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 ------------------------------------------------------
2 -- GFWTable.lua
3 -- Utilities for manipulating tables
4 ------------------------------------------------------
5  
6 GFWTABLE_THIS_VERSION = 5;
7  
8 ------------------------------------------------------
9  
10 -- Mean: returns the mean value of a table of numbers
11 function GFWTable_temp_Mean(aTable)
12 if (aTable == nil or table.getn(aTable) == 0) then
13 return nil;
14 end
15 return GFWTable.Sum(aTable) / table.getn(aTable);
16 end
17  
18 -- Sum: returns the sum of a table of numbers
19 function GFWTable_temp_Sum(aTable)
20 if (aTable == nil or table.getn(aTable) == 0) then
21 return nil;
22 end
23 local sum = 0;
24 for i=1, table.getn(aTable) do
25 if (tonumber(aTable[i])) then
26 sum = sum + aTable[i];
27 end
28 end
29 return sum;
30 end
31  
32 -- Median: returns the median value (most useful in a table of numbers, but usable in any sorted table)
33 function GFWTable_temp_Median(aTable)
34 if (aTable == nil or table.getn(aTable) == 0) then
35 return nil;
36 end
37 if (table.getn(aTable) == 1) then
38 return aTable[1];
39 end
40 local sortedTable = GFWTable.Copy(aTable); -- leave the original table in whatever order it's in
41 table.sort(sortedTable);
42 local count = table.getn(sortedTable);
43 local median;
44 if (math.mod(count, 2) == 0) then -- even table size
45 local middleIndex1 = count / 2;
46 local middleIndex2 = middleIndex1 + 1;
47 median = (sortedTable[middleIndex1] + sortedTable[middleIndex2]) / 2; --average the two middle values
48 else -- the table size is odd
49 local trueMiddleIndex = (count + 1) / 2; -- calculate the middle index
50 median = sortedTable[trueMiddleIndex];
51 end
52 return median;
53 end
54  
55 -- Merge: returns the union of two tables (without repeated elements)
56 function GFWTable_temp_Merge(table1, table2)
57 local newTable = { };
58 if (table1) then
59 for i=1, table.getn(table1) do
60 table.insert(newTable, table1[i]);
61 end
62 end
63 if (table2) then
64 for i=1, table.getn(table2) do
65 if (GFWTable.IndexOf(newTable, table2[i]) == 0) then
66 table.insert(newTable, table2[i]);
67 end
68 end
69 end
70 return newTable;
71 end
72  
73 -- Diff: returns the difference of two tables (those elements which occur in either but not both)
74 function GFWTable_temp_Diff(table1, table2)
75 local newTable = { };
76 if (table1 == nil) then
77 table1 = {};
78 end
79 if (table2 == nil) then
80 table2 = {};
81 end
82 for i=1, table.getn(table1) do
83 if (GFWTable.IndexOf(table2, table1[i]) == 0) then
84 table.insert(newTable, table1[i]);
85 end
86 end
87 for i=1, table.getn(table2) do
88 if (GFWTable.IndexOf(table1, table2[i]) == 0) then
89 table.insert(newTable, table2[i]);
90 end
91 end
92 return newTable;
93 end
94  
95 -- Subtract: returns a table containing those items in table1 not present in table2
96 function GFWTable_temp_Subtract(table1, table2)
97 local newTable = { };
98 if (table1 == nil or table.getn(table1) == 0) then return newTable; end
99 if (table2 == nil or table.getn(table2) == 0) then return table1; end
100 for i=1, table.getn(table1) do
101 if (GFWTable.IndexOf(table2, table1[i]) == 0) then
102 table.insert(newTable, table1[i]);
103 end
104 end
105 return newTable;
106 end
107  
108 -- IndexOf: returns the index (1-based) of an item in a table
109 function GFWTable_temp_IndexOf(aTable, item)
110 return GFWTable.KeyOf(aTable, item) or 0;
111 end
112  
113 -- KeyOf: returns the key to an item in a table with numeric or non-numeric keys, or nil if not found
114 function GFWTable_temp_KeyOf(aTable, item)
115 if (aTable == nil or type(aTable) ~= "table") then
116 return nil; -- caller probably won't expect this, causing traceable error in their code
117 end
118 for key, value in aTable do
119 if (item == value) then
120 return key;
121 end
122 end
123 return nil;
124 end
125  
126 -- Copy: copies one table's elements into a new table (useful if you want to change them while preserving the first table).
127 -- Not a deep copy.
128 function GFWTable_temp_Copy(aTable)
129 local newTable = { };
130 for i=1, table.getn(aTable) do
131 newTable[i] = aTable[i];
132 end
133 return newTable;
134 end
135  
136 -- Count: returns number of items in a table regardless of whether it has numeric indices.
137 function GFWTable_temp_Count(aTable)
138 if (aTable == nil or type(aTable) ~= "table") then
139 return nil; -- caller probably won't expect this, causing traceable error in their code
140 end
141 local count = 0;
142 for key, value in aTable do
143 count = count + 1;
144 end
145 return count;
146 end
147  
148 ------------------------------------------------------
149 -- load only if not already loaded
150 ------------------------------------------------------
151  
152 if (GFWTable == nil) then
153 GFWTable = {};
154 end
155 local G = GFWTable;
156 if (G.Version == nil or (tonumber(G.Version) ~= nil and G.Version < GFWTABLE_THIS_VERSION)) then
157  
158 -- Functions
159 G.Mean = GFWTable_temp_Mean;
160 G.Sum = GFWTable_temp_Sum;
161 G.Median = GFWTable_temp_Median;
162 G.Merge = GFWTable_temp_Merge;
163 G.Diff = GFWTable_temp_Diff;
164 G.Subtract = GFWTable_temp_Subtract;
165 G.IndexOf = GFWTable_temp_IndexOf;
166 G.KeyOf = GFWTable_temp_KeyOf;
167 G.Copy = GFWTable_temp_Copy;
168 G.Count = GFWTable_temp_Count;
169  
170 -- Set version number
171 G.Version = GFWTABLE_THIS_VERSION;
172 end
173  
174