-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcauseEffect.jl
2445 lines (1913 loc) · 77.5 KB
/
causeEffect.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=
Lessons learned
"All your actions go right between the Idealized & the Materialized"
---
1. view(collect(1:5),1:5) #correct syntax
view(collect(a:b),a:b) #correct syntax
in general:
`view(arr,1:5)`` or `view(arr,a:b)``
2. When an error raises a `MethodError`: no method matching iterate(::Nothing) iterate('nothing')
it's like saying, WARNING: there is a `Hidden issue` that you have Overseen
Like a `function call`, with a `Missing Function` (user forgot to get it compiled)
3. `MethodError` : no method matching !(::Int64)
!(a >= b) no such operator composition (not less than (or greater than))
just revert the `bounds` manually
(a <= b)
4. `StackOverflowError` : severe Error, with an unclear reasoning: missing 1 (or more) conditions(s) without a returning argument
5. `BoundsError` : attempt to access 2-element view(::Vector{Int64}, 1:2) with eltype Int64 at index [5:9]
possible 2 composed views (subarray of another subarray) whereas answer expects only 1 view (subarray)
6. `BoundsError` : attempt to access 2-element view(::Vector{Int64}, 1:2) with eltype Int64 at index [1:5]
the problem is with the provided `bounds`
1.Update:
1.1. maybe they aren't updated: needs continual updates
1.2. maybe they are wrongly updated: needs to redo each bound, of each view
1.3. they currently passed a view, instead of intended subview
7. #`BoundsError`: attempt to access 4-element Vector{Int64} at index [1:5]:
Problem Definition : the Given bounds exceed the actual array size
Note: 5 & 6 are have the same problem domain, but with different Scenario Implementation
& with the same solution (i.e. with the same key, to opening this AliBaba cave)
solution: check input arguments(bounds) are less than (or equal to) input vector array
#`BoundsError`: attempt to access 4-element Vector{Int64} at index [1, 4]
#remapping problem #subtle-issue why?: because of an update on (1 of the ) bounds
Solution:
b = euclidDist(a,b) + 1
a = 1
# Create a new view with a,b
7. ERROR: Bounds Error: attempt to access 9-element Vector{Int64} at index [1, 5]
Go with the hunch on this one:
Reason : observed b is set to 9 (outside the scope of this function)
Problem's region is outside the function boundaries
passed
8. ERROR: `BoundsError`: attempt to access 3-element view(::Vector{Int64}, 1:3) with eltype Int64 at index [1, 2]
first: it has reached its terminus, as euclidDist(1,2) = 1
instead of terminating...
it throws a `BoundError`
9. `BoundsError`: attempt to access 1-element view(::Vector{Int64}, 1:1) with eltype Int64 at index [1:2]
Answer: it's a scalar: scalar handling when a-b = 0 <=> a=b
10. `BoundsError`: attempt to access 1-element `view(::Vector{Int64}, 1:1)` with eltype `Int64` at index [1:2]
first index [1:2] has a=1 , b=2 , dist = b-a = 1 # check euclidDist(a,b) =
Solution:
#try#1: correct miss-pecified function arguments: check function adds type for arr::Array{Int64,1}
REVEALED A GIGANTIC ISSUE If left Uncorrected
For functions: `arr` , `view`
(before: compiler has been thinking they are the same (1) function(view & arr are same) , after correction, they are 2 expected functions [as Expected] (view, arr are different ))
`BoundsError`: attempt to access 1-element view(::Vector{Int64}, 1:1) with eltype Int64 at index [1:2]
`twinMiddles` [m1, m2]= [1, 1]
`local sorting` finished successfully of sorting _view:
[1]
....
b =1
a =1 b =2
Seeing enough evidence: the error must be: after merging `m1 m2` (as one Unity)
as it's forgotten to `autoadjust`, on the interval `m2:b`:
m2 -= 1 ; b -= 1
=#
#module CauseEffect # unCommentMe : If files complie
using Test
# include("Utils.jl"): lineLengthAcceptable #TODO: add, at the end
import Base: @propagate_inbounds
Middles = []
offset = 1
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#=Recursive code #uncommentMe at end
nNodes = length(arr) - 2 # 2: for a & b count - explorable nodes see length(arr) -2 -1
if nNodes == 0 #offest - 1
return #0
elseif nNodes == 1 #nVertices() == 1 (couple, pair)
doCompare(a, b)
return #0
elseif nNodes > 1
stoppingCondition = copy(isStop(a,b,view(_view,a,b))) # isStop(a,b,view(_view,a:b))
#isStop = copy(isStop(1, 4, [2, 1, 3, 5]))
# if stoppingCondition == false
# if checkCond # checkCond(a,m1, m2, b, isWhole, arr)
effect(a, b, view(arr, a, b - offset)) # nNodes = length(arr)-2 -1 (offset)
#else #on its own does nothing
end
#end
=#
""" if m is bi-valued, unpack it """
function unpackM(m)
_m1 = 0
_m2 = 0
try
if m !== 0
_m1 = m[1]
_m2 = m[2]
elseif m == 0
_m1 = 0
_m2 = 0
else
throw(error("Unexpected error occured"))
end
return _m1, _m2
catch #UnexpectedError
writeError()
#@error "Unexpected Error occured" exception(UnexpectedError, catch_backtrace())
end
end
#= #unCommentMe at the end
"""From the content, get its index, compareContents & swap them accordingly"""
@inline function swapContent( aContent :: Int64, bContent :: Int64, _view) #TODO: check?
contentSwapped = true
a = findall(x -> x == aContent, _view)
# a = a[offset] #ok, but it's offset dependent
a = firstindex(a) #fetch the first index of a # a[firstindex(a)] #always yields the right choice
b = findall(x -> x == bContent, _view) # find all content matching b
# b = copy(b[length(b)])
b = lastindex(b) #fetch last index of b #b[lastindex(b)]
if aContent > bContent
_view[a], _view[b] = _view[b], _view[a] #swap
#contentSwapped = true
println(_view[a], " ", _view[b], " ", contentSwapped)
elseif aContent < bContent
contentSwapped = false
println(_view[a], " ", _view[b], " ", contentSwapped)
elseif aContent == bContent
#personal preference solution , the first one close to lower bound is at first
contentSwapped = false
println(_view[a], " ", _view[b], " ", contentSwapped)
end
return a, b, contentSwapped #returns index
end
=#
#=
acontent = ar[1]
bcontent = ar[2]
if bcontent> acontent
swapContent(acontent, bcontent,ar)
end
=#
#index, value space [vital]
@inline function doCompare(a, b, _view;exceptionParameter = UnexpectedError) #TODO: check?
#try
#try_block
#end
contentSwapped = nothing
aContent = _view[a] #view(_view, a) #arr[a]
bContent = _view[b] #view(_view, b) #arr[b]
triplet = 0 , 0, nothing # the magical stop line
_length = copy(length(_view)) #ok
#a <= _length && b <= _length && a >= 0 && b >= 0
_linelength = lineLengthAcceptable(a,b,_length)
if _linelength == false
return triplet
elseif _linelength == true
if aContent > bContent # arr[a] > arr[b]
a,b,contentSwapped = swapContent(_view[a], _view[b], _view) #oldSchoolSwap(arr[a], arr[b], arr) #an inbounds swap #actual array swap
elseif aContent < bContent #> bContent
#do nothing
contentSwapped = false
end
return a, b, contentSwapped
else raise(exception)
end
triplet
#catch
end
#DEMO:
arr
arr[length(arr)]
#index, value space [vital]
@inline function doCompare(a:: Int64 , b :: Int64 , arr::Array{Int64,1}) #works
#[1...8] length = 8+1 -1 = 8
_length = copy(length(arr))
if lineLengthAcceptable(a,b,_length) == true #a <= _length && b <= _length # && b >= m2
aContent = arr[a] #<-------
bContent = arr[b]
contentSwapped = nothing
# try
# Base.@propagate_inbounds
if aContent > bContent # arr[a] > arr[b]
#Base.@propagate_inbounds
a,b,contentSwapped = swapContent(arr[a], arr[b], arr) #oldSchoolSwap(arr[a], arr[b], arr) #an inbounds swap #actual array swap
#contentSwapped = true #arr[a], arr[b]
#Base.@inbounds
elseif aContent < bContent # arr[a] < arr[b] #review#1 #<-----
#don't swap # return values
# return
contentSwapped = false #arr[a], arr[b]
#@inbounds a[st], a[ed] = a[st] , a[ed] #an inbounds swap
elseif aContent == bContent # arr[a] == arr[b] #contents the same Can increase the count (in a dictionary?)
#a<b Always
# if a < b
contentSwapped = false #arr[a], arr[b]
#do nothing
#else?
end
#catch UnexpError #<--- exception: Caught: check for euclidDist above the scope of this function
# @error "ERROR:UnexpError " exception = (UnexpError, catch_backtrace())
#end
return a, b, contentSwapped #arr[a], arr[b]
end
end
# compareQuartet
""" compareQuartet: Algorithm
G1 = { a, b}
G2 = { m1,m2}
## Phase1:
G1 = {a, b}
G2 = {m1, m2}
#1
docompare(a, b, arr )
#2
docompare(m1, m2, arr)
Regroup (free)
## Phase2:
mins [local min group]= { m1 , m2} out of the mins, find the min (& the max)
maxes [local max group]= {a , b }out of the maxes, find the max (& the min)
input vector array , applies a view on each Interval, remaps the last one
# input vector array , applys view on each Interval, remap last one
num Ops = 4 (as in Quartet)
"""
function compareQuartet(a, m1, m2, b, arr; exceptionParameter = UnexpectedError) # debugged
try
#0. Init
isSwapped = []
#G1: Group1 = {a, b}
#G2: Group2 = {m1, m2}
# Phase1:
println("phase1 : G1 (10, 3) at indicies & G2 at indicies ")
#conversion to index a
a = firstindex(arr)
#conversion to index b
b = lastindex(arr)
# get m1 (in relation to a)
m1 = a + 1
# find m2 (in relation to b)
m2 = b - 1
idx1 = 0 ; idx2 = 0
# Phase 1:
println("Scan #1")
#1
println("#1")
# docompare(a,b, arr)
# At 1,4 , arr , values (8, 6) -> (6, 8) , isSwapped =true
a, b, isSwapped1 = docompare(a,b, arr)
println("arr = ", arr)
# 2
println("#2")
# m1 m2
# At 2,3 , arr , values (10, 3) -> (3, 10 ) , isSwapped =true
m1, m2 , isSwapped2 = docompare( m1, m2, arr )
println("arr = ", arr)
# arr = [ 6, 10, 3, 8 ]
println("Scan #2")
# Compare group 1 values (arr[a], arr[b]) at indicies: a , b
# Phase2 :
#find the local min , out of { arr[a] , arr[m1] }
# compare values whose indicies G1 are a with m1 indicies: compare their values , now `a` should have the `local min`
# 6 , 3 -> 3, 6 , isSwapped = true
#3 local min
#Group1
#find the local min , out of { arr[a] , arr[m1] }
# compare values whose indicies G1 are a with m1 indicies: compare their values , now `a` should have the `local min`
println("#3")
a, m1, isSwapped3 = docompare(a,m1, arr)
println("arr = ", arr)
# arr = [ 6, 10, 3, 8 ]
#4 local max
# Group2: find the local max (from the maxes arr[m2], arr[b] at indicies m2, b )
println("#4")
# 10 , 8 -> 10 , 8 , isSwapped = true
m2, b, isSwapped4 = docompare(m2, b, arr)
println("arr = ", arr)
# 2 misc. group flags
# 2 group flags
swapped = [isSwapped1, isSwapped2, isSwapped3 , isSwapped4] # a, b, m1 , [_isSwapped1, _isSwapped2, _isSwapped3 ]
println("swapped = ", swapped)
#3. Return
a, b, m1 , m2 , swapped
catch exceptionParameter #UnexpectedError
#@error , "Unexpected error" , exception = (UnexpectedError, catch_backtrace())
writeError(msg, exceptionParameter) # @error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
end
#=
function compareQuartet(a, m1, m2, b, _view)
try
twinMiddles = nothing
# 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))
m1, m2, _isSwapped = swapContent(m1, m2, view(_view, m1:m2)) #compare twinMiddles' content
a, b, _isSwapped = swapContent(a, b, view(_view, a:b)) #compare bounds' content
a, m1, _isSwapped = swapContent(a, m1, view(_view, a:m1))
m2, b = remap(m2, b)
println(" m2,b = ", m2, b)
m2, b = swapContent(m2, b, view(_view, m2:b))
twinMiddles = [m1, m2] # vector (Array{Int64, 1})
println("twinMiddles [m1, m2]= ", [m1, m2])
# push!(Middles, twinMiddles)
return a, b, m1, m2 #m1, m2 #should it be a,b, twinMiddles ?
catch UnexpectedError
@error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
end
=#
function endAlgorithmSafely()
return # 0
end
function endAlgorithmSafely(arr::Array{Int64,1})
println("local sorting finished successfully of sorting arr: ") #show a friendly message of end
println(arr)
return #true #0
end
function endAlgorithmSafely(_view::SubArray)
println("local sorting finished successfully of sorting _view: ") #show a friendly message of end
println(_view)
return #true #0
end
euclidDist(a::Int64, b::Int64) = 0 <= a && 0 <= b ? abs(max(a, b) - min(a, b)) : 0 #+ 1 : 0 #-1 #both a,b > 1 positive #review#2: offset is meaningless in this context #&& 0 <= offset
"""sum of indicies""" #scaffold
sumInterval(a::Int64, b::Int64) = a > 0 && b > 0 ? abs(b) + abs(a) : 0
function isEven(a, b) # review#1 corrected: adds offset adjustment #review#2: ; offset = 1 #offset is Independent
try
number = -1
if a > 0 && b > 0
number = b + a # calculates Interval length # offset - 1
number > 0 && number % 2 == 0 ? true : false #always exists (if conditions apply)
else
throw(error("Unexpected value error"))
end
catch UnexpectedError
@error "Unexpected Error occured" exception = (UnexpectedError, catch_backtrace) #passing function pointer to catch_backtrace
end
end
function isEven(number::Int64) # = #review#2: offset & number are independent
isItEven = nothing
try
if number > 0 #&& number % 2 == 0 #isEven
# number += offset - 1 # number = number + offset -1 #review#2
if number % 2 == 0
isItEven = true
else
isItEven = false
end
else
throw(error("not positive error"))
end
catch nonPositiveError
@error "ERROR: input not Positive" exception = (nonPositiveError, catch_backtrace())
end
return isItEven #number
end
function getIsWhole(a::Int64, b::Int64)
isWhole = isEven(a, b) # sumInterval(a,b) % 2 == 0
return isWhole
end
function getIsWhole(arr::Array{Int64,1})
isWhole = firstindex(arr) + lastindex(arr) % 2 == 0
return isWhole
end
function getIsWhole(_view::SubArray)
isWhole = firstindex(_view) + lastindex(_view) % 2 == 0
return isWhole
end
#----
#TODO: try middle
@propagate_inbounds function middle(a::Int64, b::Int64) # b + a -1 # Acceptable #review#2 ; offset = 1 #rule-found: offset only used in an array (at its start)
try
_sum = sumInterval(a, b) # b + a - 1 # distance between them <---- Error
println("a,b =", a, " ", b)
println("sum = ", _sum)
isItEven = isEven(_sum)# #even is a proxy for divisibility # TODO: surround by a copy() #homeMade Heuristic <------
println("iseven = ", isItEven)
mid = _sum / 2 # -1 # precalculate mid (_sum /2 ) #Float32(64)
println("mid( sum / 2) = ", mid)
# isWhole = getIsWhole(arr) # uncommentMe if everything else not working
if isItEven == true
#compareTriad
# 1 middle calculate
println("Even = ", isItEven)
mid = Int(mid) #Int
println("Rational mid(index)= ", mid)
isWhole = true
println("is whole = ", isWhole)
return mid, mid + 1, isWhole
elseif isItEven == false
#compareQuartet
# calculate fractionalMid
println("Even = ", isItEven)
println("fractionalMiddle, twinMiddles ")
lower = -1
upper = -1
lower = Int(floor(mid))
upper = Int(ceil(mid))
isWhole = false
println("lower = ", lower)
println("upper = ", upper)
println("isWhole = ", isWhole)
return lower, upper, isWhole # the differenece is still 1, only way to discriminate is by using isWhole
else
throw(error("Unexpected error occured")) #<-------
end
catch UnexpectedError #errors out
@error "Unexpected error occured" exception = (UnexpectedError, catch_backtrace()) #<-----
end
end
"""
Vital helper functions
1. middle Index : fetches middle (m1), m1+1
2. doCompare: bounds of a vector, if conditions are met, we'd swap, fetches a, m1 , & isSwapped
locally compare & sort values
for 4 verticies : a, m1, m2, b -thus we need 4 comparisons
```input
a: lower bound
b: upper bound
arr: vector array
```
```output
Ms: vector for middes m1, m2
contentSwapped: Bool vector for whether (note: the first one is always isWhole)
```
"""
@propagate_inbounds function callMiddle(a :: Int64, b :: Int64) #, arr) #vital function (middle , docompare...)
try
# Reviewr#2: removed distance() should be here ( distance is only in isStop )
#distance = euclidDist(a,b) # response = isStop(a, b, arr)
if a != b && a > 0 && b > 0 # only condition we require
#contentSwapped = [] #nothing
# _isSwapped = nothing
m1, m2, isWhole = middle(a, b) #gets middle of a length (in rational positive integer ) <---ERROR: no iterate(Nothing)
#end
#push!(rangeIndicies, i)
# push!(contentSwapped, isWhole) #store iswhole at first index #review#2: delete this line
#check if isWhole # now: to store it as current middle & in Middles[] (for the future tree)
#TODO: another thing: assumes that each index is unique & different, (so if not unique, how to handle it?)
#isunque #if different, then index1, index2 form a valid Interval #done
#TODO: #2 NOT Passing a vector: worst case (the Worst)defaults to arr value [nightmare]
#if a != b
# a, b, _isSwapped = doCompare(a, b, arr) #removed
#push!(contentSwapped, _isSwapped)
#end
#if m1 != m2 # they always are different # m2 - m1 = 1 always
# m1, m2, _isSwapped = doCompare(m1, m2, arr)
# push!(contentSwapped, _isSwapped) #2nd comparison
#else # register value only once
#push!(Middles, m1) #store middle
# storeMiddle(m1)
#there is a total mutual exclusion, in the value space (a,m1)
#still, we're left with a, m1 (which inferred to be both local mins) their content's yet uncompared
#if a != m1
# a, m1, _isSwapped = doCompare(a, m1, arr) #compares values at lower bound & middle
# push!(contentSwapped, _isSwapped) #3rd comparison
#end #else return a scalar leaf node a == m1 (binaryTree)
#what's left is: m2, b (local max) - let's compare their values, respectively
#if m2 != b
# m2, b, _isSwapped = doCompare(m2, b, arr)
# push!(contentSwapped, _isSwapped) #4th comparison
#end #else return a scalar leaf node m2 == b (binaryTree)
# explore!(a, m1, arr) #recursively explore what's left on left
# explore!(a + 1, m1 - 1, arr) #recursively explore what's in the next m
# explore!(m2 + 1, b - 1, arr) # explore!(m2+1, b-1, arr)
#code
#optimization: Q.do we use euclidDist between verticies: a, m1, m2, b ? A. Possible; at this point: further processing is not necessaily needed, as of result
#review2: comment: euclidDist won't come alone, but with isNotDivide: better yet is, after finished handling Ms, contentSwapped, call explore (that has all desired functions)
# return [a, b],
#return m1, m2
#return [m1,m2]
# return [m1, m2]
return m1, m2, isWhole # lastindex(contentSwapped): min= 0, max=4 #edited!
#return a, m1, m2, b #indices (practical) # Q. shouldn't we return their content (no, unless we're working with some `DataStructure` i.e. trees, where content is necessary to be read )
elseif a == b # 1, 1 , arr #singleton
#register as a leaf (binTree)
return 0, 0, nothing # 0, 0, 0 # depends on Expected return return
else
raise(exceptionParameter) #throw(error("UnexpectedError occured"))
end
#end
catch exceptionParameter # UnexpectedError
@error "UnexpectedError occured" exception = (UnexpectedError, catch_backtrace())
end
end
a, b, m1, m2 = compareQuartet(a, m1, m2, b, arr) # <--- Erroneous: TODO: relpace with your fixed solution
a = 1
b = 9
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
m1, m2, isWhole = callMiddle(a, b) #, arr) #TODO: callmiddle with arr
a, b, m = compareTriad(a, m1, lastindex(arr), arr) # (1, 9, 5) #ok # BoundsError: attempt to access 4-element Vector{Int64} at index [9] # MethodError: no method matching iterate(::Nothing) #compiles #(1, 4, 2)
#Int(firstindex(arr))
#Int(lastindex(arr))
#------
#vital functions:
"""right side function: to evalualte a,b, arr, & their subsequent intervals -used at first run """
function goright!(a::Int64, b::Int64, arr::Array{Int64,1}) #to evalualte a,b, view1 (& their subsequent intervals )
#to go right, fix b, increase a
cause(a, b, arr) #call on the whole array # <------ problem starts from here (after finishing the function (onEnd()))
#<---- here:
#update new value bounds:
checkRightCondition(arr)
end #<-------
function goright!(_view) #EDIT: Try this use-case #::SubArray) #to evalualte a,b, view1 (& their subsequent intervals )
#to go right, fix b, increase a
a = firstindex(_view)
b = lastindex(_view)
cause(a, b, _view) #call on the whole array # <------ problem starts from here [down goes into the rabbit Hole]
#newView here?
# checkRightCondition(_view) #uncommentMe
end
# TODO: Define ERROR: LoadError: UndefVarError: stoppingCondition not defined
#done remap
#compareTriad
#maybe remap including b+1 is exclusive to special case, & not all
"""right side function: to evalualte a,b, view1, & their subsequent intervals """
function goright!(a::Int64, b::Int64, _view::SubArray) #ok #<------
#to go right, fix b, increase a
#isStop(a,b,view)
#cause(a, b, originalView) #subView here #BoundsError: attempt to access 4-element Vector{Int64} at index [1, 4]
#rule: don't have to subview original view
cause(a, b, _view) #<--------
# checkRightCondition(_view) #uncommentMe
#<---ends
end
function checkRightCondition(arr::Array{Int64,1})
a = firstindex(arr)
b = lastindex(arr)
msg = "UnexpectedError"
#update new value bounds:
try
a = a + 1 #update a #[remapping is required]]
if a != b #1 2
#remapping problem #subtle-issue (Solved)
a, b = remap(a, b) #doing remap
b - a > 1 ? goright!(view(arr, a, b)) : endAlgorithmSafely(view(arr, a, b))
elseif a == b
endAlgorithmSafely()
else
throw(error(msg))
end
catch UnexpectedError
@error msg * " Occured" exception = (UnexpectedError, catch_backtrace())
end
end
#------
function checkRightCondition(_view::SubArray) #correct #<----- this is called after algorithm finishes
a = firstindex(_view)
b = lastindex(_view)
msg = "UnexpectedError"
#update new value bounds:
try
a = a + 1 #update a #[remapping is required]]
if a != b
#remapping problem #subtle-issue
a, b = remap(a, b) #doing remap
#check input arguments(bounds) are less than (or equal to) input vector array
if b - a > 1 # distance is more than 1
# newView = view(_view, a:b)
goright!(view(_view, a:b))
elseif b - a == 1 #distance equals 1
endAlgorithmSafely(view(_view, a:b))
end
# b - a > 1 ? goright!(view(_view, a, b)) : endAlgorithmSafely(view(_view, a, b))
elseif a == b # b-a == 0
endAlgorithmSafely()
else
throw(error(msg))
end
catch UnexpectedError
@error msg * " Occured" exception = (UnexpectedError, catch_backtrace())
end
end
#DEMO:
#checkLeftCondition(arr) for view only
arr
#checkRightCondition(arr)
function goleft!(a :: Int64, b :: Int64, _view :: SubArray ) #compiles correctly #todo: Update a,b
#To go left: fix a, decrease b
# call cause on original view once
cause(a, b, _view)
# cause(a, b, view1) #: endAlgorithmSafely(view1) # return
# checkLeftCondition(_view) #<------ uncommentMe
#b-a >0 #b-a>1
end
function goleft!(_view :: SubArray)
a = firstindex(_view)
b = lastindex(_view)
cause(a, b, _view)
# checkLeftCondition(_view) #<--------- #uncommentMe
end
function goleft!(a::Int64, b::Int64, arr::Array{Int64,1}) #compiles correctly #todo: update a,b
#to go left: fix a, decrease b
# effect(a, b, arr)
#a!=b ?
#eval cause once on original vector array
#a = firstindex(arr)
# b = lastindex(arr)
cause(a, b, arr) #we can keep it as it
# cause(a, b, view(arr, a, b))
#build a view
checkLeftCondition(arr)
#=
if a != b #<-----------
#a != b ? cause(a, b, arr) : endAlgorithmSafely(arr) # return
#a is the same
#q. can i get away without remapping here? NO
b = b - 1 #update b #ok
a, b = remap(a, b) #after remap
#b = euclidDist(a, b) + 1
#a = 1 #always start at this
#newView = view(arr, a, b) #update view #was b-1 #ok got remapped
#isStop() here
b - a > 1 ? goleft!(firstindex(view(arr, a, b)), lastindex(view(arr, a, b)), view(arr, a, b)) : endAlgorithmSafely(view(arr, a, b))
# b - a > 1 ? goleft!(a, b, view(arr, a, b)) : endAlgorithmSafely(view(arr, a, b)) # return #was b - 1 #it's remapped #Q. why play with intervals manually?
else #a ==b i.e. scalar value
#endAlgorithmSafely(arr)
endAlgorithmSafely() #action: return only
end
=#
end
global msg = "UnexpectedError"
function checkLeftCondition(_view::SubArray)
try
a = firstindex(_view)
b = lastindex(_view)
println("b = ", b) # b was 9
#nextCond #interval oriented:
b = b - 1 #update b # now b is 8
if a != b
a, b = remap(a, b)
println("a = ", a, " b = ", b) # b is still 8 #a =1 b =8
if b - a > 1
println("b - a = ", b - a) #b-a = 7
newView = view(_view, a:b) #<-----------
goleft!(newView) #<--------------
elseif b - a == 1
endAlgorithmSafely()#view(_view, a, b)) #TODO: undo this line #uncomment Me
end
# b - a > 1 ? goleft!(view(_view, a, b)) : endAlgorithmSafely(view(_view, a, b)) #<-----
elseif a == b
endAlgorithmSafely()
else
throw(error(msg))
end
catch UnexpectedError
@error msg * " Occured" exception = (UnexpectedError, catch_backtrace())
end
end
#checkLeftCondition finishes correctly
#----
#cause
#check both funcs below are identically equal
#Rule: cause must always get the updated a & b (of the current view )
function cause(a::Int64, b::Int64, _view::SubArray) #in: _view #uses view #error #no isStop(a,b,view) ==false
##if isStop(a, b, view) == false # continue processing #callMiddle #checkCond #sub-interval #UncommentMe
#isStop(a, b, view) #Error here
#isStop(a, b, view(collect(a:b), a:b))
condition = stoppingCondition(a, b, _view) #isstop(a, b, _view) #depreciate this #TODO Questionale change it
if condition == true # the actual utility of function #out: view : arr , a:b
return #0 #last return #correctChoice
elseif condition == false
#elseif condition == false
# return callMiddle(a, b) # m1,m2,isWhole
m1, m2, isWhole = callMiddle(a, b) # arr) #new range new middle
#--------
# a,b remapping
#no need in this context
#why: the bounds a,b haven't been updated
#--------
checkCond(a, m1, m2, b, isWhole, _view) #includes: compare values #<----
return m1, m2, isWhole #TODO: continue logic
end
#m1,m2,isWhole = callMiddle() #checkCond(a,m1,m2,b,view) #is it acceptable to pass it a view?
end
# let currentValue be equal to 1
currentValue = 1
# init
#before first cause, call init
function cause(a::Int64, b::Int64, arr::Array{Int64,1})#, currentValue) #working #uses arr only *Warning*
condition = stoppingCondition(a, b, currentValue) #isStop(a, b, arr) # view #TODO: change to a practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = callMiddle(a, b) # arr)
checkCond(a, m1, m2, b, isWhole, arr) #includes: compare values #<----
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(a, b, view(arr, a:b)) #working #correct use of view (on arr, from a, to b) #view(collect(a:b), a:b)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#strategy pass-in an arr, for now #
function cause(a::Int64, b::Int64, arr::Array{Int64,1},currentValue) #working #uses arr only *Warning*
condition = isStoppingCondition(arr, currentValue) #isStop(a, b, arr) # view #TODO: change to a practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = callMiddle(a, b) # arr)
checkCond2(a, m1, m2, b, arr) #includes: compare values #<----
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(a, b, view(arr, a:b)) #working #correct use of view (on arr, from a, to b) #view(collect(a:b), a:b)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#effect
""" from arr , get the view(arr, a:b)"""
function effect(a::Int64, b::Int64, _view::SubArray) # = view(_view, a, b))
#to subarray the view
#=
condition = isStop(a, b, _view)
if condition == true
return
elseif condition == false
=#
#a != b ?
#cause(offset, length(_view), _view) #view(_view, a:b)) #: return # cause(a, b, view(_view, a:b)) #<-------
cause(a, b, _view)
#end
end
#checkCond
#----
#requires compareQuartet, compareTriad
function checkCond(a::Int64, m1::Int64, m2::Int64, b::Int64, isWhole::Bool, _view::SubArray) #is there a way to convert view to arr
if isWhole == true
#m region
#compare content (of 3-Fractal: a, m, b )
compareTriad(a, m1, b, _view) #ok #issue: arr [should be view ] #hillarious : was b1 instead of b #compareTriad(..,_view)
#Do:
#view1 = view(arr, a:m1) #correct result
#view2 = view(arr, m1:b) #unneeded
#effect(a, m1, _view) # effect(a, m, view(,a,b)) isStop(1, 2, view([1, 2], 1:2))
#effect(m1, b, view2) #<---------- # 3:5
#to go left: fix a, decrease b #but we start with a:m1 - interval is verified
newView = view(_view, a:m1)
println("a = ", a, " m1= ", m1, " newView = ", newView)
goleft!(a, m1, newView) #using subview #was a,m1 #<----- then here (first left I see a:m1 the same i.e )
#remap required
m1, b = remap(m1, b)
print("m1, b = ", m1, b)
# view2 = view(_view, m1:b) #issue: building proper view - subarray of an array #was m1,b
goright!(view(_view, m1:b))
elseif isWhole == false
#check bounds compare content: (of 4-Fractal: a,m1,m2,b )
#2 middles become one a ... m1 nc, m1 m2 =1 (n/A) m2 b1
#should be m1 nc, d(m1 m2)=0 m1-m2=0 (m2 loses its previous position to become 1 with m1 )
# 5 6 7 8 9
# (5 6) 7 8 9 # [-1 ]
#5 nc 6 7 8
#2. length -= 1
# remapForm1m2
compareQuartet(a, m1, m2, b, _view) #compare arr values at 4 index points: a,m1,m2,b #ok
#view1 = view(arr, a:m1) #correct result
#Index changes: need for Remapping: m2, b
#b- m2 #difference
#the fix for the following view:
m2, b = remap(m2, b) #done
#adjust index for merging m1m2 step:
#m2 -= 1
#b -= 1
# b = euclidDist(m2, b) #+1
# m2 = 1
view2 = view(_view, m2:b) #issue: building proper view - subarray of an array #<----------------error #was m2,b
#TODO: handle interval m1,m2 as well (for completeness: we got to compare all vertex intervals )
#effect(a, m1, view1) # effect(a, m, view(,a,b)) isStop(1, 2, view([1, 2], 1:2))
#effect(m2, b, view2) #<---------- # 3:5
goleft!(view(_view, a:m1)) #go left iteratively #
goright!(view2) # goright!(m2, b, view(view2, m2:b)) #go right iteratively #<---------
end
end
function checkCond2(a::Int64, m1::Int64, m2::Int64, b::Int64, isWhole::Bool, _view::SubArray) #is there a way to convert view to arr
if isWhole == true
#m region
#compare content (of 3-Fractal: a, m, b )