-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommented.jl
412 lines (314 loc) · 13.2 KB
/
commented.jl
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#module commented
msg = "unexpected error occured"
euclidDist( a :: Int64, b :: Int64 ) = -(a,b)+1
#TODO: change this function
@inline function swapContent(aContent :: Float32 , bContent :: Float32, arr::Array{Int64,1}) # ; offset=1) #new! # a,b,indicies in arr #the less arguments the better
contentSwapped = nothing
a = findall(x -> x == aContent, arr)
#a = a[offset]
println("a = ", a, " length = ", length(a))
a= firstindex(a) #first(a)
println("firstindex a = ", a)
## a = a[a+1] ## a[firstindex(arr)]] # first index
## a = first(a) +1
b = findall(x -> x == bContent, arr)
println("b = ", b, " length = ", length(b))
## if length(a) == 0 || length(b) == 0
# return
## end
## b = lastindex(b) #last(b)
#b = b[b-1]
#b = b[lastindex(b)] # copy(b[length(b)]) # -offset])
b = lastindex #last(b)
if aContent < bContent
# arr[a], arr[b] = arr[a], arr[b] # nothing
contentSwapped = false
println(arr[a], arr[b], contentSwapped)
elseif aContent == bContent
# personal preference solution: first one, closest to lower bound is at first
contentSwapped = false
println(arr[a], arr[b], contentSwapped)
elseif aContent > bContent
contentSwapped = true
arr[a], arr[b] = arr[b], arr[a] #swap
println(arr[a], arr[b], contentSwapped)
end
## _first = arr[a]
# _last = arr[b]
#if condition
#= if aContent > bContent
arr[a], arr[b] = arr[b], arr[a] #swap
contentSwapped = true
# _first = arr[a]
# _last = arr[b]
println(arr[a], arr[b], contentSwapped)
elseif aContent < bContent
# arr[a], arr[b] = arr[a], arr[b] # nothing
contentSwapped = false
println(arr[a], arr[b], contentSwapped)
elseif aContent == bContent
#personal preference solution , the first one close to lower bound is at first
contentSwapped = false
println(arr[a], arr[b], contentSwapped)
end =#
##
return a, b, contentSwapped #returns index (more practical)
end
""" Catch an error on the `backtrace` with specified error, and display elaboration message, by default """
function writeError( msg :: String ; _error :: String = UnexpectedError, elaboration ::String =": please check then try again ")
@error msg + elaboration + exception = (UnexpectedError, catch_backtrace())
#@error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
function remap( a :: Int64 , b :: Int64 ) # 1 2 abs(max(a, b) - min(a, b)) + 1 ; 2 -1 = 1 + 1 = 2
b = euclidDist(a, b) + 1 # + 1 #warning you added 1 to the end: recheck new bounds (are all ranges fit) - some got to be out
a = 1 #always start from this index #or offset
return a, b
end
#= #CauseEffect.jl
function stoppingCondition(_view::SubArray) #depreciate
# m1, m2, isWhole = callMiddle!(first(_view), last(_view), _view)
isWhole = getIsWhole(_view)
#calcUnexplored
# # length(arr) - 2 - isWhole= 1 : 2
formula = calcTotalMiddles(_view)
formula -= getSubtractedValue(isWhole)
if formula == 0 #useful for a recursion #check if done
#stoppage condition
return true #0
#end
else
#instead
#callMiddle!(a, m1,) #TODO: compllete this logic
#create view
#callMiddle!(a,b,view)
#isWhole == true ? callMiddle(m1, b) : callMiddle(m2, b)
return false
end
end
=#
function stoppingCondition( a :: Int64 , b :: Int64 , _view)#depreciate
m1, m2, isWhole = callMiddle!(a, b, _view)
#calcUnexplored
if calcTotalMiddles(_view) - getSubtractedValue(isWhole) == 0 #useful for a recursion #check if done
#stoppage condition
return true #0
else
## instead
#callMiddle!(a, m1,) #TODO: compllete this logic
#create view
#callMiddle!(a,b,view)
## isWhole == true ? callMiddle(m1, b) : callMiddle(m2, b)
return false
end
end
#no method matching getIndex(::Int64, ::Vector{Int64})
function getindices( a , b , arr )
firstIndex = firstindex(findall(x -> x == a, arr ))
lastIndex = lastindex(findall(x -> x == b, arr ))
idxA, idxB = firstIndex, lastIndex
idxA, idxB
end
#from index.jl
include("index.jl")
function getindices2( a , b , arr )
firstIndex = getIndex( a , arr ) #firstIndex(findall(x -> x == a, arr ))
lastIndex = getIndex( b , arr ) #lastindex(findall(x -> x == b, arr ))
idxA, idxB = firstIndex, lastIndex
idxA, idxB
end
ar = [3,2,1]
println( CartesianIndices(axes(ar) ))
a1, a2 = getindices2(1,2, ar )
println("a1,a2: a1 = ", a1, " a2 = ", a2 )
#=
function swapContents( a :: Int64 , b :: Int64, lst ; first ::Int64 = 1 ) # check?
#0. Init
idxA, idxB = 0, 0
n = length(lst)
nMax = n - 1 + first
is_swapped = nothing
#1. Get indicies
idxA, idxB = getindices(a,b, lst)
## if a < nMax || b < nMax
# if collection is ordered
if lst[idxA] < lst[idxB]
is_swapped = false # no swap
print(" Content is not swappable\n")
# If collection is not ordered
elseif lst[idxA] > lst[idxB]
lst[idxA], lst[idxB] = lst[idxB], lst[idxA]
## a , b = b, a
## lst[idxB], lst[idxA] = lst[idxA], lst[idxB] #swap
is_swapped = true # swap
print(" Content Swapped\n")
end
a, b, is_swapped
end
=#
function swap( a :: Int64 , b :: Int64, lst ; first :: Int64 = 1 )
#0. Init
idxA, idxB = 0, 0
n = length(lst)
nMax = n - 1 + first
is_swapped = nothing
#1. check if index is above max
if a > nMax || b > nMax
#2.1.1 Call Procedure
idxA, idxB = getindices(a , b, lst)
#2.1.2 Swap Indices
a,b = idxA, idxB
end
#2. Otherwise, if interval(a,b): is valid
if a < nMax || b < nMax
#2.1 if collection is ordered
if lst[a] < lst[b]
is_swapped = false # no swap
print(" Content is not swappable\n")
#2.2 If collection is not ordered
elseif lst[a] > lst[b]
lst[a], lst[b] = lst[b], lst[a] #swap
is_swapped = true # swap
print(" Content Swapped\n")
end
end
#3 Return
print("a = ", a , " b ", b , " lst[a] = ", lst[a], " lst[b] = ", lst[b])
a, b, is_swapped
end
#=
function compareTriad( a :: Int64, m1 :: Int64 , b :: Int64, arr ; exceptionParameter = UnexpectedError)
try
# Scan #1
##Swap #1
a, m1, _isSwapped = swap(a, m1, arr)
##Swap #2
m1, b, _isSwapped = swap(m1, b, arr)
#Scan2
## Swap3
a, m1, _isSwapped = swap(a, m1, arr)
print("a , m1, b", a, " ", m1, " ", b)
a, b, m1
catch exceptionParameter
writeError( msg, exceptionParameter)
end
end
=#
# DEMO
ar1 = [10, 8, 3]
a, b, m1 = compareTriad(1,2, 3, ar1)
print(" a = ", a, " m1 = ", m1, " b = ", b ," ; lst[a] = ", ar1[a],
" ar1[a] = ", ar1[a], " ar1[m1] = ", ar1[m1]," ar1[b] = ", ar1[b]) # to dislay at the end
println("\nar1 = ", ar1)
""" get a subView from any view, provided a `lowerBound` & an `upperBound` """
function subView(lowerBound :: Int64 ,upperBound :: Int64 ,_view)
collect(lowerBound:upperBound) |> _view -> view(_view, firstindex(_view):lastindex(_view))
end
function compareQuartet(a :: Int64, m1 :: Int64 , m2 :: Int64, b ::Int64 , _view)
try
## artest = [10, 8, 6, 3]
## a, m1, m2 , b
twinMiddles = nothing
mins=[]
maxes=[]
#= # apply view(arr, a:b)
compareQuartet(a, m1, m2, b, arr)
=#
# m1, m2, _isSwapped = doCompare(m1, m2, view(_view, m1:m2)) #compare twinMiddles' content
## a, b, _isSwapped = doCompare(a, b, view(_view, a:b)) #compare bounds' content
# a, m1, _isSwapped = doCompare(a, m1, view(_view, a:m1))
a, m1, _isSwapped1 = doCompare(a, m1, _view) #swapContent(a, m1, _view) # a is local min # 8 10 true
println("After swap : a, ",a, " m1 = ", m1," _view = ",_view )
## mins.append(a)
## maxes.append(m1)
## m2, b = remap(m2, b)
m2, b, _isSwapped2 = doCompare(m2, b, _view) #swapContent( m2, b, _view) # b is local max # 810true # 38true
println("after swap : m2w, ", m2," b =", b, " view = ", _view )
#m2, b, _iswapped = subView(m2, b, _view )
#Phase 2: mins vs maxes
# post local sort: rearrange terms into 2 new groups: mins & max(es)
#println("a, m2", a, m2)
## mins:
##unCommentMe: a, m2, _isSwapped3 = swap(a, m2, _view) #swapContent(a, m2, _view) # a is local min
println("after swap : a ", a," m2 =", m2," view = ",_view)
#m1, b, _iswapped = subView( m1, b, _view )
# maxes:
## m1, b, _isSwapped4 = swap( m1, b, _view) #swapContent( m1, b, _view) # b is local max
## println("after swap subView: m1, ", m1, " b = ", b, " view = ",_view )
# at this point: a is global min, b is global max
# (still we have yet to check last 2 terms: m1, m2 )
#Phase3:
#m1, m2, _iswapped = subView( m1, m2, _view )
#m1, m2, _isSwapped3 = swapContents( m1, m2, _view) #swapContent( m1, m2, _view) # b is local max
println("end: : m1, ", m1," m2 =", m2," view = ", _view)
#scan2
#=
a, m2, _isSwapped4 = swap(a, m2, _view) #swapContents(a, m2, _view) # a is local min
m1, b, _isSwapped5 = swapContents(m1, b, _view) #swapContent( m2, b, _view) # b is local max # 810true # 38true
# m2, b, _isSwapped5 = swap(m2, b _view) #swapContents(a, m2, _view) # a is local min
=#
m1,m2, _isSwapped6 = swapContents(m1,m2, _view) # [8 6] -> [6 8]
# a,b, _isSwapped6 = swapContents(m1,m2, _view)
a, m1, m2, b , [_isSwapped1, _isSwapped2, _isSwapped6 ] # _isSwapped4, _isSwapped5 # _isSwapped4, _isSwapped5, _isSwapped6 ] # _isSwapped3 ,_isSwapped4, _isSwapped5, _isSwapped6 ] #_isSwapped4, _isSwapped5 ]
catch UnexpectedError
@error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
end
function endAlgorithmSafely()
return # 0
end
## swapContent(_view[lowerBound], _view[upperBound], _view)
#oldSchoolSwap(arr[lowerBound], arr[upperBound], arr) #an inbounds swap #actual array swap
#lowerBound, upperBound = oldSchoolSwap(lowerBound, upperBound, _view)
# contentSwapped = true #arr[lowerBound], arr[upperBound]
# elseif aContent > bContent
#do nothing
# contentSwapped = false
# end
# ===========
# Utils.jl
# ===========
# 5305
## orphaned Code Block compiles inputArguments() # TODO: create lowerBound function, for it
# m2, upperBound relation (m2+1 , upperBound-1 )
#= UncommentMe
if euclidDistDifference(m2 + 1, upperBound - 1) > 2 #? #TODO: fix orphaned code
conquer(collect(m2+1:upperBound-1), kernel) #: return
elseif isUnitDistanceReached(m2 + 1, upperBound - 1) == true # , kernel) == true #1 # euclidDistDifference(m2 + 1, upperBound - 1) == 1
#v = collect(m2:upperBound)
#_view = view(v, firstindex(v):lastindex(v))
if m2 == upperBound - 1 || m2 + 1 == upperBound # (instead of 4 points,) [we have unique 2 points]
# the difference between m2 & m2+ 1 is 1 -> docompare() is the best compare function
_view = collect(m2:m2+1) # |>
_view -> view(_view, firstindex(_view):lastindex(_view))
res = swapContent(m2, m2 + 1, _view)
#check return
if res !== nothing
lowerBound, upperBound, contentSwapped = res
return lowerBound, upperBound, contentSwapped
end
else # if there are 4 points
_view = collect(m2:upperBound) |> _view -> view(_view, firstindex(_view):lastindex(_view))
res = compareQuartet(m2, m2 + 1, upperBound - 1, upperBound, _view) # <------------------ indexed_iterate(I::Nothing, i::Int64)
if res !== nothing
m2, m2 + 1, upperBound - 1, upperBound = res
return m2, m2 + 1, upperBound - 1, upperBound
end
end
# the difference between m2 & m2+ 1 is 1 -> docompare() is the best compare function
# _view = collect(m2:m2+1) |> _view -> view(_view, firstindex(_view):lastindex(_view))
# m2 == upperBound - 1 && m2 + 1 == upperBound ? swapContent(m2, m2 + 1, _view1) : compareQuartet(m2, m2 + 1, upperBound - 1, upperBound, _view)
#infer information, locations are equal : lowerBound = m2+1 , upperBound = upperBound-1
elseif isEndReached(1, 1) #euclidDistDifference(m2 + 1, upperBound - 1) == 0 # scalar
# return true #isEndReached(m2, upperBound)
else
throws(error(msg))
end
#end
=#
# end
artest = [10, 8, 6, 3]
a, m1, m2, b, _swapped = compareQuartet(10, 8,6,3, artest)
println("artest = ", artest)
artest = [6,10, 4,1 ]
a, m1, m2, b, _swapped = compareQuartet(6, 10,4,1 , artest)
println("End: artest = ", artest)