forked from xupeng1206/ReaChord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReaChord_Util.lua
147 lines (129 loc) · 2.85 KB
/
ReaChord_Util.lua
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
--@noindex
--NoIndex: true
function ListX4(lst)
local newLst = {}
for i=1, 4 do
for idx, val in ipairs(lst) do
table.insert(newLst, val)
end
end
return newLst
end
function StringSplit(str, sp)
local result = {}
local idx = 0
while true do
idx, _ = string.find(str, sp)
if idx == nil then
table.insert(result, str)
break
else
table.insert(result, string.sub(str, 1, idx-1))
str = string.sub(str, idx+string.len(sp), string.len(str))
end
end
return result
end
function ListAddUniqValue(lst, val)
for idx, item in ipairs(lst) do
if item == val then
return lst
end
end
table.insert(lst, val)
return lst
end
function ListJoinToString(lst, sp)
local result = ""
for idx, item in ipairs(lst) do
if idx>1 then
result = result..sp..item
else
result = result..item
end
end
return result
end
function ListIndex (lst, val)
for idx, v in ipairs(lst) do
if v == val then
return idx
end
end
return -1
end
function AListAllInBList(aLst, bLst)
for _, aVal in ipairs(aLst) do
if ListIndex(bLst, aVal) < 0 then
return false
end
end
return true
end
function AListInBListLen(aLst, bLst)
local counter = 0
for _, aVal in ipairs(aLst) do
if ListIndex(bLst, aVal) > 0 then
counter = counter + 1
end
end
return counter
end
function ListExtend(aLst, bLst)
local newLst = {}
for _, val in ipairs(aLst) do
table.insert(newLst, val)
end
for _, val in ipairs(bLst) do
table.insert(newLst, val)
end
return newLst
end
function ListDeleteIndex(lst, index)
local new_lst = {}
for idx, item in ipairs(lst) do
if idx ~= index then
table.insert(new_lst, item)
end
end
return new_lst
end
function SplitListAtIndex(lst, index)
local l, r = {}, {}
for idx, item in ipairs(lst) do
if idx <=index then
table.insert(l, item)
else
table.insert(r, item)
end
end
return l, r
end
function DeepCopyList(lst)
local ret = {}
for i, v in ipairs(lst) do
table.insert(ret, v)
end
return ret
end
function PermuteList(lst)
local ret = {}
local length = #lst
local function backtrack(first)
if first == length then
table.insert(ret, DeepCopyList(lst))
end
for i=first,length do
lst[first], lst[i] = lst[i], lst[first]
backtrack(first+1)
lst[first], lst[i] = lst[i], lst[first]
end
end
backtrack(1)
return ret
end
function PrintList(lst)
for _, v in ipairs(lst) do
print(v.." ")
end
end