List implementation to pull the list manipulation logic out of the codebase
This commit is contained in:
parent
fb71e0d7dc
commit
ac727ad742
2 changed files with 188 additions and 0 deletions
104
src/List.ahk
Normal file
104
src/List.ahk
Normal file
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
* AHK List implementation
|
||||
* Copyright (c) 2012 Joshua Fuhs
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is an admittedly poor list implementation, but I was new to AHK,
|
||||
* and I had seen several examples of this already used.
|
||||
* The lists being operated on should not be larger than a few hundred,
|
||||
* and preferably no larger than one hundred.
|
||||
*/
|
||||
|
||||
|
||||
List_new() {
|
||||
Global
|
||||
Return ""
|
||||
}
|
||||
|
||||
List_prepend( ByRef l, e ) {
|
||||
l := e . "`;" . l
|
||||
Return
|
||||
}
|
||||
|
||||
List_append(ByRef l, e) {
|
||||
l := l . e . "`;"
|
||||
Return
|
||||
}
|
||||
|
||||
; Insert at position immediately preceding index p
|
||||
List_insert(ByRef l, e, p) {
|
||||
Local arr, search, replace
|
||||
If ( p = 0)
|
||||
return List_prepend(l, e)
|
||||
StringSplit arr, l, `;
|
||||
if ( p >= arr0 - 1 )
|
||||
return List_append(l, e)
|
||||
p += 1
|
||||
search := arr%p% . ";"
|
||||
replace := e . ";" . search
|
||||
StringReplace, l, l, %search%, %replace%
|
||||
}
|
||||
|
||||
List_remove(ByRef l, e) {
|
||||
Local search
|
||||
search := "" . e . ";"
|
||||
StringReplace, l, l, %search%,
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
List_removeAt(ByRef l, p) {
|
||||
Local arr, search
|
||||
StringSplit arr, l, `;
|
||||
if( p >= arr0 - 1)
|
||||
Return
|
||||
p += 1
|
||||
search := arr%p% . ";"
|
||||
StringReplace, l, l, %search%,
|
||||
}
|
||||
|
||||
List_find(ByRef l, e) {
|
||||
Local arr, arr0
|
||||
StringSplit arr, l, `;
|
||||
Loop, % (arr0 - 1) {
|
||||
If arr%A_Index% = %e%
|
||||
Return (A_Index - 1)
|
||||
}
|
||||
Return -1
|
||||
}
|
||||
|
||||
List_dump(l) {
|
||||
Local result
|
||||
StringReplace, result, l, `;, %A_Space%, All
|
||||
Return result
|
||||
}
|
||||
|
||||
List_get(l, p) {
|
||||
Local arr
|
||||
StringSplit arr, l, `;
|
||||
If( p >= arr0 )
|
||||
Return ""
|
||||
p += 1
|
||||
Return arr%p%
|
||||
}
|
||||
|
||||
List_size(l) {
|
||||
Local arr, arr0
|
||||
StringSplit arr, l, `;
|
||||
Return (arr0 - 1)
|
||||
}
|
84
src/List.test.ahk
Normal file
84
src/List.test.ahk
Normal file
|
@ -0,0 +1,84 @@
|
|||
|
||||
Test_check(Condition, msg) {
|
||||
Global
|
||||
If Condition
|
||||
Return
|
||||
Log_bare(msg)
|
||||
Exit, 1
|
||||
}
|
||||
|
||||
|
||||
Log_init("List.test.txt", True)
|
||||
|
||||
testlist := List_new()
|
||||
Log_bare("new list: " . List_dump(testlist))
|
||||
Test_check(List_dump(testlist) = "", "Test 1 failure")
|
||||
If Not (List_dump(testlist) = "" )
|
||||
Log_bare("Test 1 failure")
|
||||
List_append(testlist, "abc")
|
||||
Log_bare("appended 'abc': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "abc " )
|
||||
Log_bare("Test 2 failure")
|
||||
List_append(testlist, "def")
|
||||
Log_bare("appended 'def': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "abc def " )
|
||||
Log_bare("Test 3 failure")
|
||||
List_append(testlist, "ghi")
|
||||
List_remove(testlist, "abc")
|
||||
Log_bare("removed 'abc': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "def ghi " )
|
||||
Log_bare("Test 4 failure")
|
||||
List_append(testlist, "jkl")
|
||||
List_remove(testlist, "ghi")
|
||||
Log_bare("add 'jkl', remove 'ghi': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "def jkl " )
|
||||
Log_bare("Test 5 failure")
|
||||
List_append(testlist, "mno")
|
||||
List_remove(testlist, "mno")
|
||||
Log_bare("add and remove 'mno': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "def jkl " )
|
||||
Log_bare("Test 6 failure")
|
||||
List_prepend(testlist, "12345")
|
||||
Log_bare("prepend '12345': " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "12345 def jkl ")
|
||||
Log_bare("Test 7 failure")
|
||||
List_insert(testlist, "xyz", 0)
|
||||
List_insert(testlist, "Happy", 1)
|
||||
List_insert(testlist, "Blah", 5)
|
||||
List_insert(testlist, "10", 10)
|
||||
Log_bare("Attempt multiple inserts by index: " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "xyz Happy 12345 def jkl Blah 10 ")
|
||||
Log_bare("Test 8 failure")
|
||||
List_removeAt(testlist, 0)
|
||||
List_removeAt(testlist, 1)
|
||||
List_removeAt(testlist, 2)
|
||||
List_removeAt(testlist, 3)
|
||||
List_removeAt(testlist, 10)
|
||||
Log_bare("Attempt multiple removals by index: " . List_dump(testlist))
|
||||
If Not (List_dump(testlist) = "Happy def Blah ")
|
||||
Log_bare("Test 9 failure")
|
||||
If Not (List_find(testlist, "Happy") = 0)
|
||||
Log_bare("Test 10 failure")
|
||||
If Not (List_find(testlist, "def") = 1)
|
||||
Log_bare("Test 11 failure")
|
||||
If Not (List_find(testlist, "Blah") = 2)
|
||||
Log_bare("Test 12 failure")
|
||||
If Not (List_find(testlist, "nonexistent") = -1)
|
||||
Log_bare("Test 13 failure")
|
||||
If Not (List_size(testlist) = 3)
|
||||
Log_bare("Test 14 failure")
|
||||
If Not (List_get(testlist, 0) = "Happy")
|
||||
Log_bare("Test 15 failure")
|
||||
If Not (List_get(testlist, 1) = "def")
|
||||
Log_bare("Test 16 failure")
|
||||
If Not (List_get(testlist, 2) = "Blah")
|
||||
Log_bare("Test 17 failure")
|
||||
If Not (List_get(testlist, 3) = "")
|
||||
Log_bare("Test 18 failure")
|
||||
|
||||
|
||||
|
||||
Return
|
||||
|
||||
#Include Log.ahk
|
||||
#Include List.ahk
|
Loading…
Reference in a new issue