vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | -- |
||
3 | -- Sea.table |
||
4 | -- |
||
5 | -- LUA Table manipulation functions |
||
6 | -- |
||
7 | -- $LastChangedBy: karlkfi $ |
||
8 | -- $Rev: 2849 $ |
||
9 | -- $Date: 2005-12-08 07:21:15 -0600 (Thu, 08 Dec 2005) $ |
||
10 | --]] |
||
11 | |||
12 | Sea.table = { |
||
13 | |||
14 | -- |
||
15 | -- getValueIndex(table, value) |
||
16 | -- |
||
17 | -- Returns the key associated with the value in the table/list. |
||
18 | -- |
||
19 | -- Args: |
||
20 | -- (table t, object item) |
||
21 | -- t - table to search |
||
22 | -- item - item to find the index of |
||
23 | -- |
||
24 | -- Returns: |
||
25 | -- nil - if empty |
||
26 | -- index - if found |
||
27 | getValueIndex = function (table, item) |
||
28 | if ( table ) then |
||
29 | for k,v in table do |
||
30 | if ( v == item ) then return k; end |
||
31 | end |
||
32 | end |
||
33 | return nil; |
||
34 | end; |
||
35 | |||
36 | -- |
||
37 | -- isInTable(table, value ) |
||
38 | -- |
||
39 | -- Returns: |
||
40 | -- true if the item is in the list/table |
||
41 | -- false if not in the list/table |
||
42 | -- |
||
43 | isInTable = function(table, value) |
||
44 | return (Sea.table.getValueIndex(table,value) ~= nil ) |
||
45 | end; |
||
46 | |||
47 | -- |
||
48 | -- isStringInTableValue(table[string] table, string word) |
||
49 | -- |
||
50 | -- Returns: |
||
51 | -- true if word exists in a string value of table |
||
52 | -- false if word is never used |
||
53 | -- |
||
54 | -- Aliases: |
||
55 | -- isWordInList() |
||
56 | -- |
||
57 | isStringInTableValue = function (table, word) |
||
58 | if ( type(table) == "nil" ) then return false; end |
||
59 | for k,v in table do |
||
60 | if ( type(v) == "string" ) then |
||
61 | if ( string.find(word,v) ~= nil ) then |
||
62 | return true; |
||
63 | end |
||
64 | end |
||
65 | end |
||
66 | return false; |
||
67 | end; |
||
68 | |||
69 | -- |
||
70 | -- Aliases: |
||
71 | -- Sea.table.getIndexInList |
||
72 | -- Sea.list.getIndexInList |
||
73 | -- |
||
74 | |||
75 | -- |
||
76 | -- push( table, value ) |
||
77 | -- |
||
78 | -- Adds a value to the end of the table. |
||
79 | -- |
||
80 | -- Arg: |
||
81 | -- table - the table |
||
82 | -- value - the value for the end of the table |
||
83 | -- |
||
84 | -- Written by Thott (thott@thottbot.com) |
||
85 | push = function (table,val) |
||
86 | if(not table or not table.n) then |
||
87 | Sea.IO.derror(nil, "Bad table passed to Sea.table.push by ", this); |
||
88 | return nil; |
||
89 | end |
||
90 | --table.insert(table, val); |
||
91 | table.n = table.n+1; |
||
92 | table[table.n] = val; |
||
93 | end; |
||
94 | |||
95 | -- |
||
96 | -- pop ( table ) |
||
97 | -- |
||
98 | -- Removes an indexed value from the end of the table and returns it. |
||
99 | -- Difference from table.remove: doesn't nil index - more memory usages, less GC |
||
100 | -- (Good for tables that are constantly filled and emptied with a consistant number of entries) |
||
101 | -- |
||
102 | -- Arg: |
||
103 | -- table - the table |
||
104 | -- |
||
105 | -- Written by Thott (thott@thottbot.com) |
||
106 | pop = function (table) |
||
107 | if(not table or not table.n) then |
||
108 | Sea.IO.derror(nil,"Bad table passed to Sea.table.pop by ", this); |
||
109 | return nil; |
||
110 | end |
||
111 | if ( table.n == 0 ) then |
||
112 | return nil; |
||
113 | end |
||
114 | |||
115 | local v = table[table.n]; |
||
116 | table.n = table.n - 1; |
||
117 | --v = table.remove(table); |
||
118 | return v; |
||
119 | end; |
||
120 | |||
121 | -- |
||
122 | -- getKeyList ( table ) |
||
123 | -- |
||
124 | -- Returns a table of keys (integer indexing), empty table if empty table. |
||
125 | -- |
||
126 | -- Arg: |
||
127 | -- table - the table |
||
128 | -- |
||
129 | -- Written by AnduinLothar (KarlKFI@cosmosui.org) |
||
130 | getKeyList = function (passedTable) |
||
131 | if( type(passedTable) ~= "table" ) then |
||
132 | return nil; |
||
133 | end |
||
134 | |||
135 | local keyList = { }; |
||
136 | for key, value in passedTable do |
||
137 | --Sea.io.print(key); |
||
138 | table.insert(keyList, key) |
||
139 | end |
||
140 | return keyList; |
||
141 | end; |
||
142 | |||
143 | -- |
||
144 | -- copy( table [, recursionList ] ) |
||
145 | -- Copies the values of a table |
||
146 | -- If you use tables as keys, it will probably fail. |
||
147 | -- |
||
148 | -- args: |
||
149 | -- table - the table you want copied |
||
150 | -- recursionList - don't use this |
||
151 | -- |
||
152 | -- returns: |
||
153 | -- table - the table containing the copy of the values |
||
154 | -- |
||
155 | copy = function ( t, recursionList ) |
||
156 | if ( not recursionList ) then recursionList = {} end |
||
157 | if ( type(t) ~= "table" ) then |
||
158 | return t; |
||
159 | end |
||
160 | |||
161 | local newTable = {}; |
||
162 | if ( recursionList[t] ) then |
||
163 | return recursionList[t]; |
||
164 | else |
||
165 | recursionList[t] = newTable; |
||
166 | for k,v in t do |
||
167 | --If it's a table we want to recurse. But the second half of this if checks to see if it |
||
168 | --is a reference to a frame, which looks like a table, and does a normal copy in such a |
||
169 | --case |
||
170 | if ( ( type(v) == "table" ) and not ( v[0] and ( type(v[0]) == "userdata" ) ) ) then |
||
171 | newTable[k] = Sea.table.copy(v, recursionList); |
||
172 | else |
||
173 | newTable[k] = v; |
||
174 | end |
||
175 | end |
||
176 | |||
177 | return newTable; |
||
178 | end |
||
179 | end; |
||
180 | |||
181 | -- |
||
182 | -- isEquivalent(table1, table2 [, recursedList]) |
||
183 | -- returns true if all of the keys in a single-layer |
||
184 | -- table point to the same values |
||
185 | -- |
||
186 | -- This is not fully tested using recursive tables |
||
187 | -- |
||
188 | -- args: |
||
189 | -- table1 - the first table |
||
190 | -- table2 - the second table |
||
191 | -- |
||
192 | isEquivalent = function (table1, table2, recursedList ) |
||
193 | if ( not recursedList ) then |
||
194 | recursedList = {}; |
||
195 | end |
||
196 | if ( type ( table1 ) ~= "table" or type(table2) ~= "table" ) then |
||
197 | if ( table1 == table2 ) then |
||
198 | return true; |
||
199 | else |
||
200 | return false; |
||
201 | end |
||
202 | end |
||
203 | recursedList[table1] = true; |
||
204 | recursedList[table2] = true; |
||
205 | |||
206 | local keyList1 = Sea.table.getKeyList(table1); |
||
207 | local keyList2 = Sea.table.getKeyList(table2); |
||
208 | |||
209 | if ( table.getn(keyList1) ~= table.getn(keyList2) ) then |
||
210 | return false; |
||
211 | else |
||
212 | for k,v in keyList1 do |
||
213 | local t1v, t2v = table1[v], table2[v]; |
||
214 | |||
215 | if ( type(t1v) ~= type(t2v) ) then |
||
216 | return false; |
||
217 | elseif ( type(t1v) ~= 'table' ) then |
||
218 | if (table1[v] ~= table2[v]) then |
||
219 | return false; |
||
220 | end |
||
221 | else |
||
222 | if ( recursedList[t1v] or recursedList[t2v] ) then |
||
223 | if ( t1v ~= t2v ) then |
||
224 | return false; |
||
225 | end |
||
226 | else |
||
227 | if ( not Sea.table.isEquivalent(t1v, t2v, recursedList) ) then |
||
228 | return false; |
||
229 | end |
||
230 | end |
||
231 | end |
||
232 | end |
||
233 | end |
||
234 | |||
235 | return true; |
||
236 | end; |
||
237 | }; |
||
238 | |||
239 | -- Aliases |
||
240 | Sea.table.getIndexInList = Sea.table.getValueIndex; |
||
241 | Sea.table.isWordInList = Sea.table.isStringInTableValue; |
||
242 | Sea.table.isInList = Sea.table.isInTable; |
||
243 | -- List and tables are equal in lua |
||
244 | Sea.list = Sea.table; |