-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunUsed2.jl
1602 lines (1271 loc) · 56.4 KB
/
unUsed2.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
#-----testing # File compiles
module unUsed2
#TODO: : replace `SwapContent` with doCompare (as it's been rechecked, debuged, tested )
#TODO: check: minimax :requires compareTriad
#completed :SwapContent undefined
# Debugs
export writeError
include("helper.jl")
import Test: @test
using Base: @propagate_inbounds , @error
# include("Utils.jl")
#msg = "ERROR: Unexpected Error Occured"
currentValue = nothing
arr = [1, 4, 8, 10]
m1 = 4 ; m2 = 8
""" Catch an error on the `backtrace` with specified error, and display elaboration message, by default """
function writeError(msg :: String; _error= UnexpectedError :: String, elaboration :: String = ": please check then try again ")
@error msg + elaboration + exception = (UnexpectedError, catch_backtrace())
#@error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
# --- Q. Is It True, or Nothing?
# isTrue
""" Checks if `_value` is equal to `true` or not; otherwise, raises an `exception` with a custom `msg` """
function isTrue( _value ; msg :: String , exceptionParameter :: String = UnexpectedError) #TODO:debug
#0. Set Bool `_flag` to true
_flag :: Bool = true
try
#1. Check if `_value` equals `false`
if _value == false
_flag = false
#2. Check if `_value` equals `true`
elseif _value == true
#3. Otherwise: raise an exception
else
raise(exceptionParameter)
end
#4. Return `_flag`
_flag
#5. Catch an `exceptionParameter` (in optional parameters)
catch exceptionParameter
#5.1 Write an error `msg`, with `exceptionParameter` [default]
writeError(msg, exceptionParameter)
end
end
""" Checks `_value` if equals nothing: If `true`, return `nothing`, otherwise, throw an exception """
function isNothing( _value , msg :: String , exceptionParameter :: String = UnexpectedError ; exceptionParameter = UnexpectedError ) #TODO:debug
try
#0. Set `response` of type `None` to `nothing`
response :: bool = true # nothing
#1. Check `_value` is not of class `Nothing`
if _value !== Nothing
response = false
#2. If `_value` equals `Nothing`, do nothing
elseif _value === Nothing
#3. Otherwise: raise an exception (from optional parameters) [default]
else
raise(exceptionParameter)
end
#4. Return `response`
response
#5. Catch `exceptionParameter` (Optional)
catch exceptionParameter
#5.1 Write an error: from given `msg` and `exceptionParameter` (Optional)
writeError( msg, exceptionParameter)
end
end
""" drop either 2 (not even), if even (i.e. with 1 middle), or 2 if not Whole ( i.e. twinMiddles)"""
function getSubtractedValue( isWhole :: Bool ; msg :: String = msg, exceptionParameter :: String = UnexpectedError )
try
#0. Initialize
subtract :: Int64 = 1
#1.checks if `isWhole` is `true` (1 Middle)
if isWhole == true
# subtract = 1
#2. Check if `isWhole` is `false` (2 Middles)
elseif isWhole == false
subtract = 2
#3. raise an `exceptionParameter` [default]
else
raise(exceptionParameter)
end
#4. Return `subtract` value
subtract
catch UnexpectedError
writeError( msg, exceptionParameter)
end
end
"""Another variation of intervalLength (): this
intervalLength is vaid , under context of sum """
function intervalLength2(lowerBound, upperBound)
lowerBound + upperBound
end
""" A custom Counter: an event-driven, self-decrementing function
called upon progess with any `Function Criteria` )
initialized automatically, since first run, hence it goes at least once
checks
""" # scaffold #light #Best
function calcVerteciesLeft!(arr ::Array{Int64,1}, currentValue :: Int64) # ,formula)
isWhole = isEven(length(arr))
try
if currentValue === Nothing # or ===
currentValue = calcTotalMiddles(arr) # #A()= length(arr) - 2 #correct move #
elseif currentValue !== Nothing # subtract from currentValue (1 or 2 ) , subtract B()
currentValue -= getSubtractedValue(isWhole)
println("currentVaule = ", currentValue) # = [1, 3] #<--------- the issue not one value (same as interval)
# println("typeof(currrentValue) = ", typeof(currentValue))
println("getSubtractedValue(isWhole) = ", getSubtractedValue(isWhole))
# println("typeof (getSubtractedValue(isWhole)) = ", typeof(getSubtractedValue(isWhole)))
if currentValue <= 0 # the last step
currentValue = 0 #-100 #0
end
else
raise(UnexpectedError) #(error(msg))
end
return currentValue
catch UnexpectedError
writeError(msg, UnexpectedError)
end
end
function calcTotalMiddles(arr=[1,2,3,4,5])
return length(arr) #5
end
isWhole=false; arr = [1,2,3,4,5];
#= #UncommentMe
if currentValue === Nothing # or ===
currentValue = calcTotalMiddles(arr) # #A()= length(arr) - 2 #<------
elseif currentValue !== Nothing # subtract from currentValue (1 or 2 ) , subtract B()
currentValue -= getSubtractedValue(isWhole)
println("currentVaule = ", currentValue) # = [1, 3] #<--------- the issue not one value (same as interval)
println("typeof(currrentValue) = ", typeof(currentValue))
println("getSubtractedValue(isWhole) = ", getSubtractedValue(isWhole))
println("typeof (getSubtractedValue(isWhole)) = ", typeof(getSubtractedValue(isWhole)))
if currentValue <= 0
currentValue = 0
end
end=#
function handleZeroNegativeValue(currentValue :: Int64)
if currentValue <= 0 # the last step
currentValue = 0 #-100 #0 125 currentValue = 0 #-100 #0
end
currentValue
end
#----------------
lowerBound = 0 ; upperBound = 9
println("lowerBound",lowerBound)
println("upperBound", upperBound)
println("currentValue",currentValue)
# Cause
#cause(lowerBound, upperBound, view(collect(lowerBound:upperBound), lowerBound:upperBound), middle) #TODO:uncomment Me
# init
#before first cause, call init
function cause(lowerBound ::Int64, upperBound ::Int64, arr ::Array{Int64,1})#, currentValue) #working #uses arr only *Warning*
condition = isStoppingCondition(arr, currentValue) #(lowerBound, upperBound, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = callMiddle!(lowerBound, upperBound, arr) # arr)
checkCond(lowerBound, m1, m2, upperBound, isWhole, arr) #includes: compare values #<----
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#Strategy pass-in an arr, for now
#------------# infinite loop (no StackOverflowError)
#=
#experimental passing consumed function as input argument parameter # calling kernel in the body
function cause(lowerBound::Int64, upperBound::Int64, arr::Array{Int64,1}, kernel, currentValue) #working #uses arr only *Warning*
currentValue = isStoppingCondition(arr, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
condition = handleC - urrentValue(currentValue)
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = kernel(lowerBound, upperBound, arr, consumedFunction) #middle)
#callMiddle!(lowerBound, upperBound, arr) # arr) #TODO: replace with middle + isEven
checkCond(lowerBound, m1, m2, upperBound, arr) #includes: compare values #<---- #checkCond2
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
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 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
#New : use of kernel as input #TODO:
function cause(pts, _view ::SubArray, kernel, currentValue) #working #uses arr only *Warning*
try
condition = isStoppingCondition(_view, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
lowerBound = pts[1]
upperBound = pts[2]
m1, m2, isWhole = kernel(lowerBound, upperBound, _view) # arr) #correct
# checkCond(lowerBound, m1, m2, upperBound, _view) #includes: compare values #<---- #checkCond2
# compareBounds(lowerBound,upperBound,_view)
#Pts...sth #TODO: recheckc
#TODO: traverse(pts)
if isEven(lowerBound, upperBound) == true #1 middle m1
## compareBounds(lowerBound, m1, upperBound, _view) #todo: unCommentMe
println("Even")
# compareBounds(lowerBound, m1)
elseif isEven(lowerBound, upperBound) == false
println("Odd")
else
#return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
raise(UnexpectedError)
end
end
return
catch UnexpectedError
writeError(msg,UnexpectedError)
end
end
#effect #depreciate
""" from arr , get the view(arr, lowerBound:upperBound)"""
function effect(lowerBound ::Int64, upperBound ::Int64, _view ::SubArray) # = view(_view, lowerBound, upperBound))
#to subarray the view
#=
condition = isStop(lowerBound, upperBound, _view)
if condition == true
return
elseif condition == false
=#
#lowerBound != upperBound ?
#cause(offset, length(_view), _view) #view(_view, lowerBound:upperBound)) #: return # cause(lowerBound, upperBound, view(_view, lowerBound:upperBound)) #<-------
cause(lowerBound, upperBound, _view)
#end
end
#cause
#check both funcs below are identically equal
#Rule: cause must always get the updated lowerBound & upperBound (of the current view )
function cause(lowerBound ::Int64, upperBound ::Int64, _view ::SubArray) #in: _view #uses view #error #no isStop(lowerBound,upperBound,view) ==false
##if isStop(lowerBound, upperBound, view) == false # continue processing #callMiddle #checkCond #sub-interval #UncommentMe
#isStop(lowerBound, upperBound, view) #Error here
#isStop(lowerBound, upperBound, view(collect(lowerBound:upperBound), lowerBound:upperBound))
condition = stoppingCondition(lowerBound, upperBound, _view) #isstop(lowerBound, upperBound, _view) #depreciate this #TODO Questionale change it
if condition == true # the actual utility of function #out: view : arr , lowerBound:upperBound
return #0 #last return #correctChoice
elseif condition == false
#elseif condition == false
# return callMiddle(lowerBound, upperBound) # m1,m2,isWhole
m1, m2, isWhole = callMiddle(lowerBound, upperBound) # arr) #new range new middle
#--------
# lowerBound,upperBound remapping
#no need in this context
#why: the bounds lowerBound,upperBound haven't been updated
#--------
checkCond(lowerBound, m1, m2, upperBound, isWhole, _view) #includes: compare values #<----
return m1, m2, isWhole #TODO: continue logic
end
#m1,m2,isWhole = callMiddle() #checkCond(lowerBound,m1,m2,upperBound,view) #is it acceptable to pass it lowerBound view?
end
# experimental
function effect!(lowerBound, upperBound, _stack, kernel=middle)
if _stack > 0
m1, m2, isWhole = kernel(lowerBound, upperBound)
#haandling # traverse
end
# cause!(lowerBound, upperBound, _stack, kernel)
end
currentValue
# cause
# init
#before first cause, call init
function cause(lowerBound ::Int64, upperBound ::Int64, arr ::Array{Int64,1})#, currentValue) #working #uses arr only *Warning*
condition = stoppingCondition(lowerBound, upperBound, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = callMiddle(lowerBound, upperBound) # arr)
checkCond(lowerBound, m1, m2, upperBound, isWhole, arr) #includes: compare values #<----
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#strategy pass-in an arr, for now
function cause(lowerBound ::Int64, upperBound ::Int64, arr ::Array{Int64,1}, currentValue) #working #uses arr only *Warning*
condition = isStoppingCondition(arr, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = callMiddle(lowerBound, upperBound) # arr)
checkCond2(lowerBound, m1, m2, upperBound, arr) #includes: compare values #<----
return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#effect
""" from arr , get the view(arr, lowerBound:upperBound)"""
function effect(lowerBound ::Int64, upperBound ::Int64, _view ::SubArray) # = view(_view, lowerBound, upperBound))
#to subarray the view
#=
condition = isStop(lowerBound, upperBound, _view)
if condition == true
return
elseif condition == false
=#
#lowerBound != upperBound ?
#cause(offset, length(_view), _view) #view(_view, lowerBound:upperBound)) #: return # cause(lowerBound, upperBound, view(_view, lowerBound:upperBound)) #<-------
cause(lowerBound, upperBound, _view)
#end
end
#------
# cause
"""Event-Driven function """
function cause(lowerBound ::Int64, upperBound ::Int64, _view, kernel, currentValue, i) #working #uses arr only *Warning*
condition = isStoppingCondition(_view, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = kernel(lowerBound, upperBound, _view, i) # arr) #correct
# checkCond(lowerBound, m1, m2, upperBound, _view) #includes: compare values #<---- #checkCond2
#check new bounds m1 m2
checkCondition(lowerBound, m1, m2, upperBound)
end
end
"""Event-Driven function """
function cause(lowerBound :: Int64, upperBound :: Int64, arr, kernel, currentValue, i) #working #uses arr only *Warning*
try
condition = isStoppingCondition(arr, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = kernel(lowerBound, upperBound, arr, i) # arr) #correct
# checkCond(lowerBound, m1, m2, upperBound, _view) #includes: compare values #<---- #checkCond2
#check new bounds m1 m2
checkCondition(lowerBound, m1, m2, upperBound)
else raise(UnexpectedError)
end
return isWhole, m1, m2
catch UnexpectedError
writeError(msg, UnexpectedError)
end
end
function cause(lowerBound :: Int64, upperBound :: Int64, arr ::Array{Int64,1}, kernel, currentValue) #working #uses arr only *Warning*
try
condition = isStoppingCondition(arr, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = kernel(lowerBound, upperBound, arr) # arr) #correct
# checkCond(lowerBound, m1, m2, upperBound, _view) #includes: compare values #<---- #checkCond2
#check new bounds m1 m2
checkCondition(lowerBound, m1, m2, upperBound)
else raise(UnexpectedError)
end
return
catch UnexpectedError
writeError(UnexpectedError)
end
end
#preferred
function cause(lowerBound :: Int64, upperBound :: Int64, _view::SubArray, kernel, currentValue) #working #uses arr only *Warning*
condition = isStoppingCondition(_view, currentValue) #isStop(lowerBound, upperBound, arr) # view #TODO: change to lowerBound practical & tested working function (Erroneous function) #untrustworthy #<--------
if condition == true
return #0
elseif condition == false
m1, m2, isWhole = kernel(lowerBound, upperBound, _view) # arr) #correct
# checkCond(lowerBound, m1, m2, upperBound, _view) #includes: compare values #<---- #checkCond2
#check new bounds m1 m2
cond = copy(isEven(lowerBound, upperBound))
if cond == true #2 middle m1: next check bounds lowerBound,m1 m1, upperBound
v, _view = newView(lowerBound, m1)
## compareBounds(v[1], v[2], _view) #todo: unCommentMe
#v, _view = newView(m1, m2)
#compareBounds(v[1], v[2], _view)
v, _view = newView(m2, upperBound)
## compareBounds(v[1], v[2], _view) #Todo: unCommentMe
elseif cond == false #isEven(lowerBound,upperBound) == #twin middles
# 1,3 4, 7 , 8, 9
if lowerBound < m1 - 1 #in lower part
v, _view = newView(lowerBound, m1 - 1) # lowerBound != m1-1 or lowerBound< m -1
# compareBounds(v[1], v[2], _view) # todo: unCommentMe
end
v, _view = newView(m1, m2) # 4, 7
## compareBounds(v[1], v[2], _view) # todo: unCommentMe
if m2 + 1 < upperBound # in upper side
v, _view = newView(m2 + 1, upperBound) # 8, 9
# compareBounds(v[1], v[2], _view) # todo: unCommentMe
end
end #endIf2
#return m1, m2, isWhole #simulates returning sth vvaluable # idea: isFinishedFlag ::Bool
#isStop(lowerBound, upperBound, view(arr, lowerBound:upperBound)) #working #correct use of view (on arr, from lowerBound, to upperBound) #view(collect(lowerBound:upperBound), lowerBound:upperBound)) #atomic operation only allowed
# isStop(1, 2, view([1, 2], 1:2)) #was
end
end
#--------
#preferred
function effect(lowerBound::Int64, upperBound::Int64, arr::Array{Int64,1}, kernel, currentValue)
cause(lowerBound, upperBound, arr, kernel, currentValue)
end
function effect(lowerBound::Int64, upperBound::Int64, _view::SubArray, kernel, currentValue)
cause(lowerBound, upperBound, _view, kernel, currentValue)
end
function effect(a :: Int64 , b :: Int64 , view = view(arr, a, b))
cause(a, b, view)
end
# ==================================================
#applied space: Materialized
function goleftVector(a :: Int64 , b :: Int64 , arr) #tobeRemoved
#fix a , decrease b
#condition = areInbounds(a, b, arr)
# if areInbounds(a, b, arr) == true
return cause(a, b - 1, arr) #causeVector(a, b - 1, arr) #or is it effect? #<------
end
function gorightVector(a :: Int64 , b :: Int64 , arr)
#fix b, increase a
cause(a+1, b, arr) #causeVector(a + 1, b, arr) #or is it effect?
end
#compareTriad functions : doCompare , push!(Middles, m1)
##""" returned structure would be m1 in the middle, a on the left (min), b on the right (max)"""
#_type = typeof(arr)
# =========================
""" compareTriad : Algorithm
#scan1
doCompare(a,m1 )
doCompare(m1, b)
#now, max is found, which is at b [exclude from next scan ]
#scan2:
doCompare(a,m1) [now scan should be finished]
"""
function compareTriad(a, m1, b, arr::Array{Int64,1}; exceptionParameter = UnexpectedError)
try
#scan1:
#a, b, _isSwapped = doCompare(a, b, arr)#view(arr, a:b)) #compare bounds
a, m1, _isSwapped = doCompare(a, m1, arr) #view(arr, a:m1))
#no need for remap, (context: arr is giiven, & not altered in this one - remap not needed at all )
m1, b, _isSwapped = doCompare(m1, b, arr) #view(arr, m1:b))
# scan2 :
a, m1, _isSwapped = doCompare(a, m1, arr) #view(arr, a:m1))
# push!(Middles, m1)
println("a, m1, b = ", a, " ", m1, " ", b)
return a, b, m1
catch exceptionParameter #UnexpectedError
writeError(msg, exceptionParameter) # @error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
end
function compareTriad(a, m1, b, _view)
try
#instead of these
# compareTriad(a, m1, b, arr)
a, b, _isSwapped = doCompare(a, b, _view)#view(arr, a:b)) #compare bounds #<----
a, m1, _isSwapped = doCompare(a, m1, _view) #view(arr, a:m1))
m1, b, _isSwapped = doCompare(m1, b, _view) #view(arr, m1:b))
#push!(Middles, m1) # TODO: push _isSwapped
println("a, m1, b = ", a, " ", m1, " ", b)
return a, b, m1
catch UnexpectedError
@error "Unexpected error" exception = (UnexpectedError, catch_backtrace())
end
#return a, b, m1
end =#
# =========================
#Demo:
#TODO:
#m1, m2, isWhole = callMiddle(1, 1) #issue: this returns 2 , while main function expects 3 return arguments
#m1, m2, isWhole = callMiddle(firstindex(arr), lastindex(arr)) #was callMiddle(a, b, arr) #old thinking
#----
# 5 6 7 8 9 0
# 1 2 3 4 5 6
#-------
arr #where b is 9
#a=firstindex(arr)#1
#b=lastindex(arr)# 9
a = 1
b = 8
arr = [1, 2, 3, 4, 5, 6, 7, 8]
#m1, m2, isWhole = callMiddle(a, b) #, arr) #TODO: callmiddle with arr
#-------
function boundComparisonCondition(ranges, arr) # promising # :Vector{Vector{Int64}}
#try #unCommentMe
# for i in 1:length(ranges)
_length = length(arr)
i = _length # # i is inferred by the length of arr
if i == 1
return
end
_last = copy(length(ranges) - 1)
lowerBound = max(ranges[i-1][_last])
upperBound = min(ranges[i][1])
contentSwapped = nothing
if i >= 2 && lowerBound < upperBound # max(lastRange) # if i>=1
println("i = ", i)
else
raise(UnexpectedError) #throw(error(msg))
end
lowerBound, upperBound, contentSwapped
#catch UnexpectedError
# @error msg exception = (UnexpectedError, catch_backtrace())
#end
end
## ranges on lowerBound vector array
"""Recursive function : compares lowerBound bounds in ranges of lowerBound Vector arr """
function boundComparisonCondition(ranges, arr) # promising
try
# for i in 1:length(ranges)
_length = length(arr)
i = _length # # i is inferred by the length of arr
if i == 1
return
end
_last = copy(length(ranges) - 1)
lowerBound = max(ranges[i-1][_last])
upperBound = min(ranges[i][1])
contentSwapped = nothing
if i >= 2 && lowerBound < upperBound # max(lastRange) # if i>=1
lowerBound, upperBound, contentSwapped = doCompare(lowerBound, upperBound, arr) # <-----------
# return lowerBound, upperBound, contentSwapped
else
raise(UnexpectedError) #throw(error(msg))
end
_view = view(collect(1:_length-1), 1:_length-1)
return boundComparisonCondition(ranges, _view) #lowerBound, upperBound, contentSwapped
catch UnexpectedError
@error msg exception = (UnexpectedError, catch_backtrace())
end
end
## ranges on lowerBound _view
"""Recursive function : compares lowerBound bounds in ranges of lowerBound Vector _view """
function boundComparisonCondition(ranges ::Vector{Vector{Int64}}, _view) # promising
try
# for i in 1:length(ranges)
_length = length(_view)
i = _length # i is inferred by the length of lowerBound _view
if _length == 1
return
end
_last = copy(length(ranges) - 1)
lowerBound = nothing
upperBound = nothing
contentSwapped = nothing
if i >= 2 && lowerBound < upperBound # max(lastRange) # if i>=1
lowerBound = max(ranges[i-1][_last])
upperBound = min(ranges[i][1])
lowerBound, upperBound, contentSwapped = doCompare(lowerBound, upperBound, _view) # <-----------
# return lowerBound, upperBound, contentSwapped
else
throw(error(msg))
end
_view = view(collect(1:_length-1), 1:_length-1)
return boundComparisonCondition(ranges, _view) #lowerBound, upperBound, contentSwapped
catch UnexpectedError
@error msg exception = (UnexpectedError, catch_backtrace())
end
end
ranges = [[1, 3], [4, 7], [8, 9]]
print("ranges = ",ranges)
# boundComparisonCondition(ranges, view(collect(1:9-1), 1:9-1)) # 9-1 (try 9 as well) #unCommentMe
popfirst!(ranges)
#ranges[i] #3 # [8 9]
#ranges[i-1] #2 [ 4 7]
#ranges[1] # [ 1 3]
#### ran
popfirst!(ranges)
ranges[1][1]
ranges[1][2]
#ERROR: No matching method
#boundComparisonCondition([[1, 3], [4, 7], [8, 9]], [1:9], 2)# start from 2 at least #<------
#highly Experimentalg
"""f: Unknown function """
function f(_sets, arr; ranges ::Vector{Vector{Int64}} = ranges ) # O = length(_sets)
for i in 1:length(_sets) # O = length(_sets)
# ptsToRanges() # requires pts # TODO:
# compare every set's bounds
#finally , compare contents at ranges
boundComparisonCondition(ranges, arr, i)
end
end
# Minimax
"""Minimax function"""
function minimax(_sortedInterval1, _sortedInterval2, _unsortedInterval, arr) #TODO: check
lowerBound = _unsortedInterval[1]
upperBound = _unsortedInterval[2]
#1. sort lowerBound,upperBound , locally , so that lowerBound : local min, upperBound: local max
lowerBound, upperBound, contentSwapped = doCompare(lowerBound, upperBound, _unsortedInterval)
# locally sort _unsortedInterval elemts pairwise with their sorted counterparts
lowerBound, _sortedInterval1[2], _sortedInterval2[2] = compareTriad(lowerBound, _sortedInterval1[2], _sortedInterval2[2], arr)
upperBound, _sortedInterval1[1], _sortedInterval2[1] = compareTriad(upperBound, _sortedInterval1[1], _sortedInterval2[1], arr)
end
"""combines 2 different locally sorted, intervals, with an unsorted Interal """
function minimax(_sortedInterval1, _sortedInterval2, _unsortedInterval, arr) #Warning : arr is unused # depreciate
# unsorted component underquested
#1. locally sort the unsorted Interval
lowerBound = _unsortedInterval[1] #aContent (min of first interval )
upperBound = _unsortedInterval[2]# bContent (max of 2nd interval )
#1. sort lowerBound,upperBound , locally , so that lowerBound : local min, upperBound: local max
lowerBound, upperBound, contentSwapped = doCompare(lowerBound, upperBound, _unsortedInterval)
#2. builds custom _arr dynamically (from the limited infor about sorted Intervals)
_arr = collect(_sortedInterval1:_sortedInterval2) #try collect
if _arr == [] # if for some reason content hasn't been collected (due to non-ordered items)
_arr = collect(_sortedInterval2:_sortedInterval1) # collect,in the other direction
end
_arr # array of indiciees (not yet loaded with values)
#TODO: to continue load all elements _arr with value from _sorter
# locally sort _unsortedInterval elemts pairwise with their sorted counterparts
#first crossroad
#1. either calculate the foloowing 2 (hoping the rest goes well)
lowerBound, _sortedInterval1[2], _sortedInterval2[2] = compareTriad(lowerBound, _sortedInterval1[2], _sortedInterval2[2], _arr)
upperBound, _sortedInterval1[1], _sortedInterval2[1] = compareTriad(upperBound, _sortedInterval1[1], _sortedInterval2[1], _arr)
# P.s: compareTriad: returns an index
# or, as an online learner About Location, we can , logically infer location,correctly
#how: by inferring (invoke: infer location , for any three indices : output returs the sorted )
lower = _sortedInterval1[1] # lowerContent
upper = _sortedInterval2[2]# upperContent
lowerBound = _arr[lowerBound]
lower, upper, lowerBound = inferLocation(lower, upper, lowerBound) # compare contents
upperBound = _arr[upperBound]
lower, upper, upperBound = inferLocation(lower, upper, upperBound)
end
#=
either we compare the 3 contents on their on, once & for all [classic]
or, we can online learn x's location from already mde predicates [dynamic]
=#
# we don't knowif lowerBound , upperBound are the actual bounds
# what we know for sure: lowerBound,upperBound are sorted and in the right order
#=
each item has to be replaced: lowerBound upperBound c
each move transition from oldIndexlocation, to lowerBound new onr
given that we already inferred
new location is either:
lowerBound upperBound x
lowerBound x upperBound
x lowerBound upperBound
=#
#Note: requires: indexOf
# handleMultipleIndicies
## handleMultipleIndicies of lowerBound vector array
""" handles multiple Indicies"""
function handleMultipleIndicies(X, arr)
try
#matchedIndicies >= 2
#if matchedIndicies == 2
firstXIndex = firstindex(findall((x -> x == arr[X]), arr)) # [1]
# newXIndex == findall(( x -> x == arr[X]), arr)[1] #[1] #IndexOf(newlocation[1], x) # given new index the only clue for location
#_XContent = arr[X] #newlocation[newXIndex]
# oldXIndex
lastXIndex = lastindex(findall((x -> x == arr[X]), arr))
# newXIndex = lastindex(newXIndex)
ret_Val = nothing
if firstXIndex == lastXIndex # there is only 1 unique match of arr[d]
# pick one
ret_Val = lastXIndex, nothing, nothing
# if oldXIndex > newXIndex #
# else if oldIndex > newIndex
#else if firstXIndex > lastXIndex ret_Val # Irrational option (index of array is in Ascending Ordered )
elseif firstXIndex < lastXIndex #Rationally Logical
#content check
if arr[firstXIndex] > arr[lastXIndex] # dichotomy (index,content)
firstXIndex, lastXIndex, IsSwapped = doCompare(oldXIndex, newXIndex, arr)
ret_Val = firstXIndex, lastXIndex, IsSwapped
elseif arr[firstXIndex] == arr[lastXIndex] #Equal Content
ret_Val = arr[firstXIndex], arr[lastXIndex], nothing
elseif arr[firstXIndex] < arr[lastXIndex]
ret_Val = nothing, nothing, nothing
end
else
raise(UnexpectedError)
#throw(error("either firstXIndex > lastXIndex or UnexpectedError Occured "))
end
return ret_Val
#end
catch UnexpectedError
#return ret_Val
writeError(msg, UnexpectedError)
#@error "either firstXIndex > lastXIndex or UnexpectedError Occured " exception = (UnexpectedError, catch_backtrace())
end
end
# Update Location
## Update Location of a vector array
"""update location, given an Index X """
function updateLocation(lowerBound :: Int64 , upperBound :: Int64 , X, arr) #sophisticated #TODO: test
#aIndex = indexOf(arr, lowerBound)[1] # arr[indexOf(arr, lowerBound)[1]] # == lowerBound
aIndex = firstindex(findall((x -> x == arr[lowerBound]), arr)) #[1]
#bIndex = indexOf(arr, upperBound)[1] #arr[indexOf(arr, upperBound)[1]]
bIndex = lastindex(findall((x -> x == arr[upperBound]), arr)) # [1]
XIndex = nothing
X < length(arr) ? XIndex = arr[X] : nothing
#Logically: lowerBound <upperBound & arr is sorted (i.e contentA < contentB )
aIndex < bIndex && arr[aIndex] < arr[bIndex] && XIndex <= bIndex == true #always
# x index
# xcontent
#newXIndex
oldXIndex = findall((x -> x == arr[X]), arr)[1]
# newXIndex = = findall(( x -> x == arr[X]), arr)[1] #[1] #IndexOf(newlocation[1], x) # given new index the only clue for location
_XContent = arr[X] #newlocation[newXIndex]
# oldXIndex
newXIndex = findall((x -> x == arr[X]), arr)
newXIndex = lastIndex(newXIndex)
# sometimes
# oldXIndex = indexOf(arr, _XContent) #Warning if you pop; locations wll be misused #TODO: check: old Index == newIndex
#compare oldIndex with newIndex #assume cadlag
if arr[oldXIndex] > arr[newIndex] # && oldXIndex != newXIndex
oldXIndex, newXIndex, contentSwapped = doCompare(oldXIndex, newXIndex, arr)
end
#
if newXIndex == firstindex(arr) #1 # it's min, should be are the start
insert!(arr, aIndex, arr[X])
elseif newXIndex == 2 # in the middle
insert!(arr, aIndex + 1, _XContent)
elseif newXIndex == lastindex(arr) #3 # the max # add at the end
insert!(arr, bIndex, _XContent)
end
#newXContent = newlocation[IndexOf(arr, x)[1]] # can be 1 , 2, or 3
# oldxIndex = arr[indexOf(arr, x)[1]]
#indexOf(arr, lowerBound)
end
"""update location, given an Index X """
function updateLocation(lowerBound :: Int64 , upperBound :: Int64 , _XContent, arr) #sophisticated #TODO: test
#aIndex = indexOf(arr, lowerBound)[1] # arr[indexOf(arr, lowerBound)[1]] # == lowerBound
aIndex = findall((x -> x == arr[lowerBound]), arr)[1]
#bIndex = indexOf(arr, upperBound)[1] #arr[indexOf(arr, upperBound)[1]]
bIndex = findall((x -> x == arr[upperBound]), arr)[1]
#logically: lowerBound <upperBound & arr is sorted (i.e contentA < contentB )
aIndex < bIndex && arr[aIndex] < arr[bIndex] && newlocation <= bIndex == true #always
# x index
# xcontent
#newXIndex
oldXIndex = findall((x -> x == _XContent), arr)[1]
newXIndex = findall((x -> x == _XContent), arr)
newXIndex = lastIndex(newXIndex)
# sometimes
# oldXIndex = indexOf(arr, _XContent) #Warning if you pop; locations, it wll be misused #TODO: check: old Index == newIndex
#compare oldIndex with newIndex #assume cadlag
if arr[oldXIndex] > arr[newIndex] # && oldXIndex != newXIndex
oldXIndex, newXIndex, contentSwapped = doCompare(oldXIndex, newXIndex, arr)
end
if newXIndex == 1 # it's min, should be are the start
insert!(arr, aIndex, _XContent)
elseif newXIndex == 2 # in the middle
insert!(arr, aIndex + 1, _XContent)
elseif newXIndex == 3 # the max # add at the end
insert!(arr, bIndex, _XContent)
end
#newXContent = newlocation[IndexOf(arr, x)[1]] # can be 1 , 2, or 3
# oldxIndex = arr[indexOf(arr, x)[1]]
#indexOf(arr, lowerBound)
end
#arr
#[1 7 ] = [lowerBound upperBound ]
ar = collect(1:7)
x = 10
#updateLocation(1, 7, x, ar) #5, ar) #TODO: complete & check
collect(4:1)# []
collect(1:4)
# === Divide Conquer Strategy ===
#_stack