-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.monkey
3057 lines (2288 loc) · 70.9 KB
/
vector.monkey
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
Strict
Public
#Rem
TODO:
* Change several uses of "Data.Length()" to use the 'Size' property.
* Remove the limitation of not being able to offset the internal-data of the active vector properly. (Horribly complicated)
COMMAND NAMING CONVENTIONS:
* The names of commands (Methods, functions, etc) usually indicate the return value of said command.
For example, most commands ending with an underscore ("_"), then the class's "dimension tag" ("2D", "3D", etc) will likely not return a vector object.
Whereas a command which does not have an underscore as a seperator will very likely return a vector of some kind (Generated, usually).
See commands like the 'Vector2D' class's 'Add2D', and the 'Vector3D' class's 'Rotate_3D' as examples.
* Most methods (Unless explicitly stated otherwise) will affect the vector they're used from.
For example, the 'Add' command found in the 'AbstractVector' class does not produce a vector of any kind.
This command only affects its own object's ('Self') data, meaning it will mutate based on the input.
However, it will likely not mutate the input itself.
* Any names you find in this module starting with the word "Alternate" are subject to change at any point.
These elements are used by my own projects, and their underlying functionality will not be set in stone.
GENERAL ARGUMENT NOTES:
* When reading the arguments of some of these commands, you'll find some common names.
Here's a few, and what they tend to mean:
* VData_Length: The length of the current vector's data container.
* VData_Offset: The offset used when accessing the current vector.
This does not change 'VData_Length' as the end-point, so you can set this to anything and not have an issue.
* A_Length: The length of the current (Usually external) array.
* A_Offset: The offset used when accessing the current (Usually external) array.
OTHER NOTES:
* When dealing with lengths and offsets in this module, it's best to understand what they actually mean.
Basically, most situations which allow manual input for offsets and/or lengths work with a 'For' loop internally.
This loop (Conceptually) starts at the offset, and ends at the specified length (If there is any) / the internal length of the array at hand.
You SHOULD NOT expect the offset to change what the length is in any way, shape, or form. The length of an array is treated as the end point.
To put it simply, the loop has what I tend to call an operation area. I define an operation area like so: Operation_Area = (Length-Offset).
The loop will (As far as the user is concerned) start at your defined offset, and end at the length specified.
Commands technically have the right to disregard these notes, but they will usually have notation specifying such.
* For the sake of future-proofing, all 'For' loops with in-line local variables which are directly
used to iterate through memory will be done with dynamic type-detection via the ':=' operator.
This operator is also common in this module (And my other modules) when type-information is unimportant.
* THESE RULES DO NOT APPLY TO ALL MODULES I RELEASE, BUT THEY DEFINITELY APPLY HERE.
* If you're interested in better performance with some C++ compilers, you might want to look into SSE and AVX optimization options.
I wrote some basic information about this in my 'hash' module.
That functionality is not available here, but the notes I wrote could be of interest.
Optimizations applied in that module will likely apply to this one.
#End
' Preprocessor related:
#VECTOR_IMPLEMENTED = True
#Rem
VECTOR_TYPEFIXES:
DESCRIPTION:
* If this is enabled, hacks will be used in order to support non-numeric types, such as Monkey's standard "box" classes.
Default behavior for this module is to automatically enabled this functionality if the reflection filter is set to something.
If you intend to use vectors made from the standard "box" classes, make sure to manually set this to 'True'.
(Especially if you're using reflection, though this should handle that already)
NOTES:
* Work has been done specifically to support the standard box types. Don't expect to have the same optimizations and/or fixes
for other types that use "auto boxing". That being said, assuming your conversion isn't ambiguous, this should still work with such types.
* Some names for normally standard commands are different in this module;
this is because of conflicts with the current version of Monkey (As of the time I'm writing this).
Such commands will be backed by the standard implementations automatically.
That being said, some of the fixes and other such hacks done within this module may provide
alternate implementations for the standard "box" classes.
These implementations should still follow the standard behavior defined by Monkey's standards of conduct.
In addition, this module reserves the right to reimplement the standard commands' behaviors at will, even if that likely will never be necessary.
#End
#If REFLECTION_FILTER
#VECTOR_TYPEFIXES = True
#Else
#VECTOR_TYPEFIXES = False
#End
#VECTOR_SUPPORTLAYER_TYPEFIXES = VECTOR_TYPEFIXES
#VECTOR_ZERO_WITH_NIL = True
#VECTOR_GROW_ON_ACCESS = True
#VECTOR_ALLOW_EXACT_GROWTH = True
#VECTOR_SMART_GROW = True
#VECTOR_TOSTRING_USE_GENERIC_UTIL = False ' True
#VECTOR_LEGACY_NAME_STRINGS = False
#Rem
Basically, if you disable this, class-specific
implementations of shared commands may be used.
If you enable this, several implementations of
a normally shared command may be used.
Enabling this is generally faster, but you
should probably leave this variable alone.
This is only used for cases where class-specific
implementations of a command are effectively identical.
The big example here being the 'MakeBetween' command,
and similar commands which generate new vectors.
This can happen pretty often where 'Clone' is used.
This setting does not affect class-specific commands
like the 'Vector2D' class's 'MakeBetween2D' method.
This does not act as a rule, it acts as a guideline;
the value of this preprocessor variable does not
directly dictate this module's final decisions.
#End
#VECTOR_SHARE_OPTIMIZATIONS = True
' Determine which route should be used for class-name storage:
#If LANG = "cpp"
#VECTOR_CONSTANT_NAME_STRINGS = False
#Else
#VECTOR_CONSTANT_NAME_STRINGS = True
#End
#Rem
These variables toggle specific checks which may improve
program stability and sometimes, even performance.
Unless you're seriously looking into debugging
this module, don't bother changing these.
#End
#If CONFIG = "debug"
#VECTOR_SAFETY = True
#VECTOR_NUMBER_SAFETY = True
#Else
#VECTOR_SAFETY = True ' False
#VECTOR_NUMBER_SAFETY = False
#End
#Rem
Alternate division can sometimes improve performance,
the default state of this variable is "undefined".
This means I may change the default state
of this functionality at some point in the future.
#End
#If Not VECTOR_NUMBER_SAFETY And CONFIG = "release"
#VECTOR_ALTERNATE_DIVISION = True
#Else
#VECTOR_ALTERNATE_DIVISION = False
#End
' This is just a backup check:
#If Not VECTOR_NUMBER_SAFETY
#Rem
This functionality can potentially improve performance on some systems.
Basically, if this is enabled, this module will negate numbers using multiplication.
I do not currently recommend this strategy, as it's
really just something the compiler should be handling.
#End
#VECTOR_ALTERNATE_NEGATE = False ' True
#End
' Imports (Public):
Import regal.boxutil
Import regal.util
Import regal.ioelement
Import regal.sizeof
' Imports (Private):
Private
' BRL:
Import brl.stream
Public
' The following operations must be done in order to configure the module(s) imported afterword:
#If VECTOR_SUPPORTLAYER_TYPEFIXES
' Enabling this could cause some performance and/or garbage-generation problems.
#VECTOR_DELEGATE_SUPPORTLAYER = False
#Rem
DESCRIPTION:
* The support layer's "memory over-optimization" functionality basically
allows heavy re-use of objects without constraint.
As an example, if a command takes an 'IntObject', then this will re-use the initial object.
For this reason, when the support layer is delegated, the default behavior is to not apply such optimizations.
This is done due to the intrusive nature of this functionality.
A user could easily experience unintended side-effects with this feature enabled.
This module (On the other hand) is designed to support this feature.
#End
#VECTOR_SUPPORTLAYER_OVEROPTIMIZE_MEMORY = (BOXUTIL_IMPLEMENTED And Not VECTOR_DELEGATE_SUPPORTLAYER)
#End
' Imports (Other):
#If Not VECTOR_DELEGATE_SUPPORTLAYER
Private
#End
Import supportlayer
#If Not VECTOR_DELEGATE_SUPPORTLAYER
Public
#End
' Check which modules were implemented, then perform various operations accordingly:
' These checks are mainly for optional functionality:
#If BRL_STREAM_IMPLEMENTED
' If this is enabled, the standard I/O methods will be implemented.
#VECTOR_ALLOW_SERIALIZATION = True
#End
' Check if serialization was enabled, and if we were able to find the 'ioelement' module:
#If VECTOR_ALLOW_SERIALIZATION And IOELEMENT_IMPLEMENTED
' If enabled, this will allow the standard vector implementation
' to be compliant with the 'SerializableElement' interface.
#VECTOR_SUPPORT_IOELEMENT = True
#End
' Check if we should support the 'sizeof' module:
#If SIZEOF_IMPLEMENTED
#VECTOR_SUPPORT_SIZEOF = True
#End
' Global variable(s) (Public):
' Nothing so far.
' Global variable(s) (Private):
Private
' Class name-string instances:
#If Not VECTOR_CONSTANT_NAME_STRINGS
Global AbstractVector_Name_Str:String = "Vector (Abstract)"
Global Vector2D_Name_Str:String = "Vector(2D)"
Global Vector3D_Name_Str:String = "Vector(3D)"
Global Vector4D_Name_Str:String = "Vector(4D)"
Global ManualVector_Name_Str:String = "Vector"
#End
Public
' Constant variable(s) (Public):
Const VECTOR_AUTO:= UTIL_AUTO
' Property data-positions:
Const VECTOR_XPOSITION:= 0
Const VECTOR_YPOSITION:= 1
Const VECTOR_ZPOSITION:= 2
' Constant variable(s) (Private):
Private
#If VECTOR_CONSTANT_NAME_STRINGS
Const AbstractVector_Name_Str:String = "Vector (Abstract)"
Const Vector2D_Name_Str:String = "Vector(2D)"
Const Vector3D_Name_Str:String = "Vector(3D)"
Const Vector4D_Name_Str:String = "Vector(4D)"
Const ManualVector_Name_Str:String = "Vector"
#End
Public
' Interfaces:
#Rem
There are a number of requirements for implementing a vector class.
This interface shows exactly what those are.
Any class following this interface can work with the standard implementation.
This is also used by the standard vector classes
as a nonspecific interface between each other.
Such functionality may or may not be required by this interface.
If you decide to implement this, please use this interface for commands
in your own vector class, which do not need instances of that specific class.
#End
Interface Vector<T>
' Constant variable(s):
Const XPOS:= VECTOR_XPOSITION
Const YPOS:= VECTOR_YPOSITION
Const ZPOS:= VECTOR_ZPOSITION
Const AUTO:= VECTOR_AUTO
' Methods (Public):
' Conversion commands:
Method ToString:String()
Method ToString:String(GiveName:Bool, FixErrors:Bool=True)
' General purpose commands:
Method Clone:Vector<T>()
Method Copy:Void(V:Vector<T>, FitVector:Bool)
Method Copy:Void(V:Vector<T>, Size:Int=AUTO, Offset:Int=XPOS, FitVector:Bool=False)
Method Copy:Void(Info:T[], FitInfo:Bool)
Method Copy:Void(Info:T[], Size:Int=AUTO, Offset:Int=XPOS, FitInfo:Bool=False)
Method GetData:T(Index:Int)
Method GetData:T[]()
Method SetData:Void(Index:Int, Info:T)
' Mathematical commands:
Method Zero:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Absolute:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Negate:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ForceNegative:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ApplyMin:Void(Value:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ApplyMax:Void(Value:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ApplyClamp:Void(MinValue:T, MaxValue:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Add:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Add:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Add:Void(F:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method DeltaAdd:Void(V:Vector<T>, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method DeltaAdd:Void(A:T[], Scalar:T, A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method DeltaAdd:Void(F:T, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method DeltaSubtract:Void(V:Vector<T>, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method DeltaSubtract:Void(A:T[], Scalar:T, A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method DeltaSubtract:Void(F:T, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Decelerate:Void(Deceleration:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Accelerate:Void(V:Vector<T>, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Subtract:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Subtract:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Subtract:Void(F:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Divide:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Divide:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Divide:Void(F:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Multiply:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Multiply:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Multiply:Void(F:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method LinearInterpolation:Void(V:Vector<T>, t:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method LinearInterpolation:Void(A:T[], t:T, A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Normalize:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Normalize:Void(Length:T, VData_Length:Int, VData_Offset:Int)
Method IsNormalTo:Bool(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method IsNormalTo:Bool(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method IsEqualTo:Bool(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method IsEqualTo:Bool(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method Distance:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Distance:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
' This is effectively an alias for 'Add'.
' The return value is the amount possible. (Usually the same as the input)
Method Offset:T(Amount:T)
Method DotProduct:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method DotProduct:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
Method DotProductNormalized:T(V:Vector<T>)
Method SubtractTowardsZero:Void(Time:T=AbstractVector<T>.ONE, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ProjectionScalar:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method ProjectionScalar:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Project:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Project:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' This is basically the same as 'Multiply'.
Method Project:Void(Scalar:T)
Method MakeBetween:Vector<T>(V:Vector<T>)
Method AngleBetween:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method AngleBetween:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method AngleBetweenCos:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method AngleBetweenCos:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method AngleBetweenSin:T(V:Vector2D<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method AngleBetweenSin:T(A:T[], TempVector:Vector<T>)
Method AngleBetweenSin:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS, TempVector:Vector<T>=Null)
Method LengthScalar:T(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method LengthScalar:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Method Normalized:Vector<T>(VData_Length:Int=AUTO, VData_Offset:Int=XPOS, OutputVector:Vector<T>=Null)
Method Normalized:Vector<T>(Length:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS, OutputVector:Vector<T>=Null)
' This does not have to be legitimately implemented.
' Even if you were to implement it, it's not going to be efficient.
Method CrossProduct:Vector<T>(V:Vector<T>, VOUT:Vector<T>=Null)
' Methods (Private):
Private
Method Resize:Bool()
Method Resize:Bool(Size:Int)
Method Grow:Bool(GrowthIndex:Int=AUTO)
Method ControlledGrow:Bool(Index:Int=AUTO)
Public
' Properties (Public):
Method Size:Int() Property
' This isn't based on default arguments due to issues with the type of 'T'.
Method Length:T() Property
Method Length:T(VData_Length:Int, VData_Offset:Int) Property
Method Length:T(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS) Property
Method Length:Void(Value:T) Property
Method X:T() Property
Method X:Void(Info:T) Property
' Properties (Private):
Private
Method Data:T[]() Property
Public
End
' Classes:
#If VECTOR_SUPPORT_IOELEMENT
Class AbstractVector<T> Implements Vector<T>, SerializableElement
#Else
Class AbstractVector<T> Implements Vector<T>
#End
' Constant variable(s):
' The position of the 'X' property.
Const XPOS:= Vector<T>.XPOS
' The size of this vector-type.
Const VECTORSIZE:Int = 1
' General:
Const AUTO:= VECTOR_AUTO
' Error template(s):
Const VECTOR_GENERIC_ERROR_TEMPLATE:String = "{VECTOR} {ERROR}: "
' Defaults:
Const Default_ExactGrowthThreshold:Int = 16
' Boleans / Flags:
Const Default_ToString_FixErrors:Bool = True
' Global variable(s):
' The default value of the type specified.
Global NIL:T
#If VECTOR_LEGACY_NAME_STRINGS
' The name of this class.
Global Name_Str:= AbstractVector_Name_Str
#End
#If VECTOR_ZERO_WITH_NIL
Global ZERO:= NIL
#Else
Global ZERO:= T(0)
#End
Global ONE:= T(1)
Global TWO:= T(2)
Global FULL_ROTATION_IN_DEGREES:T = T(360)
Global HALF_FULL_ROTATION_IN_DEGREES:T = T(180.0)
' Defaults:
' The default multiplier used for container growth.
Global Default_GrowthMultiplier:Float = 1.5 ' 2.0
' Booleans / Flags:
' This represents the default auto-grow flag for all vector classes for the current type ('T').
Global Default_AutoGrow:Bool = False
' Functions:
Function Name:String()
#If VECTOR_LEGACY_NAME_STRINGS
Return Name_Str
#Else
Return AbstractVector_Name_Str
#End
End
Function DotProductNormalized:T(V1:Vector<T>, V2:Vector<T>)
Return V1.DotProductNormalized(V2)
End
Function VectorError:Void(Message:String, Template:String=VECTOR_GENERIC_ERROR_TEMPLATE)
DebugError(Template+Message)
Return
End
' Constructor(s) (Public):
Method New(Size:Int)
CreateData(Size)
End
Method New(Info:T[], CopyInfo:Bool)
If (CopyInfo) Then
Assign(Info)
Else
AssignByRef(Info)
Endif
End
Method New(Info:T[], Size:Int=AUTO, Offset:Int=XPOS)
Assign(Info, Size, Offset)
End
Method New(V:Vector<T>, Size:Int=AUTO, Offset:Int=XPOS)
' Local variable(s):
Local FitVector:Bool = False
' Check if the size was specified:
If (Size = AUTO) Then
Size = V.Size
FitVector = True
Endif
' Create and manage the internal container:
CreateData(Size)
' Copy the internal data of 'V'.
Assign(V, Size, Offset, FitVector)
End
' Constructor(s) (Private):
Private
Method CreateData:Void(Size:Int)
Data = New T[Size]
Return
End
Public
' Methods:
Method Clone:Vector<T>()
Return CloneAsAbstract()
End
Method CloneAsAbstract:AbstractVector<T>()
Return New AbstractVector<T>(Self)
End
Method Clone:Void(V:Vector<T>, FitVector:Bool)
Assign(V, FitVector)
Return
End
Method Clone:Void(V:Vector<T>, Size:Int=AUTO, Offset:Int=XPOS, FitVector:Bool=False)
Assign(V, Size, Offset, FitVector)
Return
End
Method Copy:Void(V:Vector<T>, FitVector:Bool)
Clone(V, FitVector)
Return
End
Method Copy:Void(V:Vector<T>, Size:Int=AUTO, Offset:Int=XPOS, FitVector:Bool=False)
Clone(V, Size, Offset, FitVector)
Return
End
Method Copy:Void(Info:T[], FitInfo:Bool)
Copy(Info, AUTO, XPOS, FitInfo)
Return
End
Method Copy:Void(Info:T[], Size:Int=AUTO, Offset:Int=XPOS, FitInfo:Bool=False)
Assign(Info, Size, Offset, FitInfo)
Return
End
Method Assign:Void(V:Vector<T>, FitVector:Bool)
Assign(V, AUTO, XPOS, FitVector)
Return
End
Method Assign:Void(V:Vector<T>, Size:Int=AUTO, Offset:Int=XPOS, FitVector:Bool=False)
' Check for errors:
#If VECTOR_SAFETY
If (V = Null) Then Return
#End
Assign(V.Data, Size, Offset, FitVector)
Return
End
Method Assign:Void(Info:T[], FitInfo:Bool)
Assign(Info, AUTO, XPOS, FitInfo)
Return
End
Method Assign:Void(Info:T[], Size:Int=AUTO, Offset:Int=XPOS, FitInfo:Bool=True)
If (Size = AUTO) Then
Size = Info.Length()
Endif
'Self.Data = GenericUtilities<T>.CopyArray(Info, Self.Data, FitInfo)
Self.Data = GenericUtilities<T>.CopyArray(Info, Self.Data, Offset, XPOS, Size, AUTO, FitInfo)
'AssignByRef(Info.Resize(Size))
Return
End
Method AssignByRef:Void(Info:T[])
Self.Data = Info
Return
End
Method GetData:T(Index:Int)
#If VECTOR_SAFETY
If (Index >= Size) Then
#If VECTOR_GROW_ON_ACCESS
If (AutoGrow) Then
ControlledGrow(Index)
Else
#End
#If CONFIG = "debug"
VectorError("Attempted to access an invalid or non-existent element.")
#End
Return NIL
#If VECTOR_GROW_ON_ACCESS
Endif
#End
Endif
If (Index < XPOS) Then
#If CONFIG = "debug"
VectorError("Attempted to access protected memory.")
#End
Return NIL
Endif
#End
Return Self.Data[Index]
End
Method GetData:T[]()
Return Data
End
Method SetData:Void(Index:Int, Info:T)
If (AutoGrow) Then
ControlledGrow(Index)
Endif
#If VECTOR_SAFETY
#If CONFIG = "debug"
If (Index >= Size) Then
DebugStop()
Endif
#End
#End
Self.Data[Index] = Info
Return
End
Method Resize:Bool(Size:Int)
Self.Data = Data.Resize(Size)
' Return the default response.
Return True
End
Method Resize:Bool()
Self.Data = Self.Data.Resize(Size * GrowthMultiplier)
' Return the default response.
Return True
End
' This command accepts an index, not a size.
Method Grow:Bool(GrowthIndex:Int=AUTO)
If (GrowthIndex = AUTO) Then
Return Resize()
Endif
#If VECTOR_SAFETY
If (GrowthIndex < Size) Then
Return False
Endif
#End
Local Growth:Int = (GrowthIndex-(LastIndex))
' If we're allowed to do so, resize exactly:
#If VECTOR_ALLOW_EXACT_GROWTH
If (Growth >= ExactGrowthThreshold) Then
Return Resize(GrowthIndex+1)
Endif
#End
' If we get to this point, simply use standard resizing.
Return Resize()
End
Method ControlledGrow:Bool(Index:Int=AUTO)
' Local variable(s):
Local Size:= Self.Size
' Check for errors:
#If VECTOR_SAFETY
If (Index <> AUTO And Index < Size) Then
Return False
Endif
#End
#If VECTOR_SMART_GROW
' If we were using a different memory model, this would work well:
#Rem
Local Response:Bool = False
' Continue growing until the internal container is big enough:
Repeat
Response = Grow()
' Make sure we were able to grow:
If (Not Response) Then
' We weren't able to grow, return a negative response.
Return False
Endif
Until (Size > Index)
#End
' Grow to the desired size.
Return Grow(Index)
#Else
If (Index = AUTO) Then
Return Resize(Size+1)
Endif
Return Resize(Index+1)
#End
End
Method Zero:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Data[I] = NIL ' ZERO
Next
Return
End
Method Absolute:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Data[I] = AbsoluteNumber(Data[I])
Next
Return
End
Method Negate:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Data[I] = NegateNumber(Data[I])
Next
Return
End
Method ForceNegative:Void(VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Absolute(VData_Length, VData_Offset)
Negate(VData_Length, VData_Offset)
Return
End
Method ApplyMin:Void(Value:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Self.Data[I] = MinimumNumber(Self.Data[I], Value)
Next
Return
End
Method ApplyMax:Void(Value:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Self.Data[I] = MaximumNumber(Self.Data[I], Value)
Next
Return
End
Method ApplyClamp:Void(MinValue:T, MaxValue:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local I:= VData_Offset Until Min(VData_Length, VData_RawLength)
Self.Data[I] = ClampNumber(Self.Data[I], MinValue, MaxValue)
Next
Return
End
' Add the input-vector to this vector:
Method Add:Void(V:Vector<T>, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
Add(V.Data, VData_Length, VData_Offset)
Return
End
Method Add:Void(A:T[], A_Length:Int=AUTO, A_Offset:Int=XPOS)
' Local variable(s):
Local A_RawLength:= A.Length()
If (A_Length = AUTO) Then
A_Length = A_RawLength
Endif
For Local Index:= A_Offset Until Min(Data.Length(), Min(A_RawLength, A_Length))
#If VECTOR_SUPPORTLAYER_TYPEFIXES
Data[Index] = AddNumbers(Data[Index], A[Index])
#Else
Data[Index] += A[Index]
#End
Next
Return
End
' This overload adds by scalar, instead of by vector.
Method Add:Void(F:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local Index:= VData_Offset Until Min(VData_Length, VData_RawLength)
#If VECTOR_SUPPORTLAYER_TYPEFIXES
Data[Index] = AddNumbers(Data[Index], F)
#Else
Data[Index] += F
#End
Next
Return
End
Method DeltaAdd:Void(V:Vector<T>, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
DeltaAdd(V.Data, Scalar, VData_Length, VData_Offset)
Return
End
Method DeltaAdd:Void(A:T[], Scalar:T, A_Length:Int=AUTO, A_Offset:Int=XPOS)
' Local variable(s):
Local A_RawLength:= A.Length()
If (A_Length = AUTO) Then
A_Length = A_RawLength
Endif
For Local Index:= A_Offset Until Min(Data.Length(), Min(A_RawLength, A_Length))
#If VECTOR_SUPPORTLAYER_TYPEFIXES
Data[Index] = DeltaAddNumbers(Data[Index], A[Index], Scalar)
#Else
Data[Index] += (A[Index] * Scalar)
#End
Next
Return
End
Method DeltaAdd:Void(F:T, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
' Local variable(s):
Local VData_RawLength:= Data.Length()
If (VData_Length = AUTO) Then
VData_Length = VData_RawLength
Endif
For Local Index:= VData_Offset Until Min(VData_Length, VData_RawLength)
#If VECTOR_SUPPORTLAYER_TYPEFIXES
Data[Index] = DeltaAddNumbers(Data[Index], F, Scalar)
#Else
Data[Index] += (F * Scalar)
#End
Next
Return
End
Method DeltaSubtract:Void(V:Vector<T>, Scalar:T, VData_Length:Int=AUTO, VData_Offset:Int=XPOS)
DeltaSubtract(V.Data, Scalar, VData_Length, VData_Offset)
Return
End
Method DeltaSubtract:Void(A:T[], Scalar:T, A_Length:Int=AUTO, A_Offset:Int=XPOS)
' Local variable(s):
Local A_RawLength:= A.Length()
If (A_Length = AUTO) Then
A_Length = A_RawLength
Endif
For Local Index:= A_Offset Until Min(Data.Length(), Min(A_RawLength, A_Length))
#If VECTOR_SUPPORTLAYER_TYPEFIXES