requires: {#Sequence}. provides: {#Tuple. #Vector}. prototypes addSlot: #Tuple valued: Sequence derive. prototypes Tuple addSlot: #actualSize. "If actualSize isNotNil, then contents size are not taken to be actual size and tupling is non-copying. Nilling the size will fix the tuple at that size." prototypes Tuple addSlot: #contents valued: {}. n@(Number traits) , m@(Number traits) "Creates a new 2-D Point." [| newT | newT: Tuple clone. newT contents: {n. m}. newT actualSize: Nil. newT ]. s@(Sequence traits) as: t@(Tuple traits) [| newT | newT: (Tuple newSize: s size immutable: True). newT contents replaceFrom: 0 to: newT actualSize - 1 with: s startingAt: 0. newT ]. t@(Tuple traits) newWith: array "This swallows the array, avoiding unnecessary copying at the caller's discretion." [| newT | newT: t clone. newT contents: array. newT actualSize: Nil. newT ]. t@(Tuple traits) newSize: n "The default setting, so that tupling is non-copying in general." [ t newSize: n immutable: False ]. t@(Tuple traits) newSize: n immutable: b "Set up the contents and actualSize variables for the use according to b." [| newT | newT: t clone. b ifTrue: [newT contents: (Array newSize: n)] ifFalse: [newT contents: (Array newSize: n + 5). newT actualSize: n]. newT ]. t@(Tuple traits) , n@(Root traits) "Uses a copy-avoiding variant of tupling if t actualSize isNotNil." [ t actualSize ifNil: [| newT | newT: (t newSize: t contents size + 1 immutable: True). newT contents replaceFrom: 0 to: t contents size - 1 with: t contents startingAt: 0. newT contents at: t contents size put: n. newT] ifNotNil: [| newContents | t contents size = t actualSize ifTrue: [ newContents: (Array newSize: t actualSize + 4). newContents copyReplaceFrom: 0 to: t actualSize - 1 with: t contents. newContents at: t actualSize put: n. t contents: newContents. t actualSize: t actualSize + 1] ifFalse: [ ]. t] ]. t@(Tuple traits) capacity [ t contents capacity ]. t@(Tuple traits) size "Always returns the intended size of the Tuple, immutable or not." [ t actualSize ifNil: [t capacity] ]. t@(Tuple traits) makeImmutable "Handle the details of managing the transition to a fixed tuple." [ t actualSize ifNil: [^ t]. t actualSize = t capacity ifFalse: [| newContents | newContents: (Array newSize: t actualSize). newContents copyFrom: 0 to: t actualSize - 1 with: t contents. t contents: newContents]. t actualSize: Nil. t ]. t@(Tuple traits) freeze "Make t and all its sub-Tuples immutable." [ t makeImmutable. t contents do: [| :each | (each is: Tuple) ifTrue: [each makeRecursivelyImmutable]]. t ]. t@(Tuple traits) at: n [ t contents at: n ]. t@(Tuple traits) at: n put: obj [ t contents at: n put: obj ]. t@(Tuple traits) collect: block [| newT a | newT: (t newSize: t size). a: newT contents. a doWithIndex: [| :index | a at: index put: (block value: (t contents at: index))]. newT ]. t@(Tuple traits) with: u@(Tuple traits) collect: block [| newT size | size: (t size min: u size). newT: (t newSize: size immutable: True). newT contents: (t contents with: u contents collect: block). newT ]. t1@(Tuple traits) , t2@(Tuple traits) [ t1 size = t2 size ifFalse: [Nil]. {t1. t2} as: Tuple ]. t@(Tuple traits) printOn: s [ s nextPut: $<. t contents do: [| :obj | obj printOn: s] separatedBy: [s ; ', ']. s nextPut: $>. t ]. prototypes addSlot: #Vector valued: Tuple derive. prototypes Vector addDelegate: #Numeric valued: NumericMixin. v@(Vector traits) dot: w@(Vector traits) "Dot-product." [| sum | sum: 0. 0 below: (v size min: w size) do: [| :index | sum: sum + ((v at: index) * (w at: index))]. sum ]. v@(Vector traits) originSized: n [| newV | newV: v clone. newV contents: (Array newSize: n). newV contents doWithIndex: [| :each :index | newV at: index put: 0]. newV ].