2635 |
2635 |
2636 addLiteral:anObject |
2636 addLiteral:anObject |
2637 "add a literal to the literalArray - watch for and eliminate |
2637 "add a literal to the literalArray - watch for and eliminate |
2638 duplicates. return the index of the literal in the Array" |
2638 duplicates. return the index of the literal in the Array" |
2639 |
2639 |
2640 |index oldLit class sharableByIdentity sharableByValue| |
2640 |index oldLit class sharable sharableValue| |
2641 |
2641 |
2642 litArray isNil ifTrue:[ |
2642 litArray isNil ifTrue:[ |
2643 litArray := OrderedCollection with:anObject. |
2643 litArray := OrderedCollection with:anObject. |
2644 ^ 1 |
2644 ^ 1 |
2645 ]. |
2645 ]. |
2646 |
2646 |
2647 sharableByIdentity := sharableByValue := false. |
2647 sharable := sharableValue := false. |
2648 class := anObject class. |
2648 class := anObject class. |
2649 class == Symbol |
2649 class == Symbol |
2650 ifTrue:[ sharableByIdentity := true ] |
2650 ifTrue:[ sharable := true ] |
2651 ifFalse:[ |
2651 ifFalse:[ |
2652 anObject isImmutable ifTrue:[ |
2652 anObject isImmutable ifTrue:[ |
2653 sharableByIdentity := true |
2653 sharable := true |
2654 ] ifFalse:[ |
2654 ] ifFalse:[ |
2655 ((class == String) or:[class == Array or:[class == ByteArray]]) ifTrue:[ |
2655 ((class == String) or:[class == Array or:[class == ByteArray]]) ifTrue:[ |
2656 anObject isEmpty ifTrue:[ |
2656 anObject isEmpty ifTrue:[ |
2657 sharableByIdentity := true |
2657 sharable := true |
2658 ] |
2658 ] |
2659 ] ifFalse:[ |
2659 ] ifFalse:[ |
2660 ((class == Float) or:[class == Fraction or:[class == LargeInteger]]) ifTrue:[ |
2660 ((class == Float) or:[class == Fraction or:[class == LargeInteger]]) ifTrue:[ |
2661 sharableByValue := true |
2661 sharableValue := true |
2662 ] |
2662 ] |
2663 ] |
2663 ] |
2664 ]. |
2664 ]. |
2665 ]. |
2665 ]. |
2666 |
2666 |
2667 (sharableByIdentity or:[sharableByValue]) ifFalse:[ |
2667 (sharable not and:[sharableValue not]) ifTrue:[ |
2668 litArray add:anObject. |
2668 litArray add:anObject. |
2669 index := litArray size. |
2669 index := litArray size. |
2670 ^ index. |
2670 ^ index. |
2671 ]. |
2671 ]. |
2672 |
2672 |
2673 "/ searching a dictionary is *much* faster; the code below starts to |
2673 "/ searching a dictionary is *much* faster; the code below starts to |
2674 "/ keep track of literals whenever we have collected more than a threshold |
2674 "/ keep track of literals whenever we have collected more than a threshold |
2675 allLiterals notNil ifTrue:[ |
2675 allLiterals notNil ifTrue:[ |
2676 sharableByIdentity ifTrue:[ |
2676 sharable ifTrue:[ |
2677 index := allLiterals at:anObject ifAbsent:nil. |
2677 index := allLiterals at:anObject ifAbsent:nil. |
2678 index isNil ifTrue:[ |
2678 index isNil ifTrue:[ |
2679 litArray add:anObject. |
2679 litArray add:anObject. |
2680 index := litArray size. |
2680 index := litArray size. |
2681 allLiterals at:anObject put:index. |
2681 allLiterals at:anObject put:index. |
2682 ^ index. |
2682 ^ index. |
2683 ]. |
2683 ]. |
2684 "/ allLiterals is byValue (a dictionary); so check again for the class |
|
2685 "/ (eg. Float value vs. Integer value) |
|
2686 (litArray at:index) class ~~ anObject class ifTrue:[ |
2684 (litArray at:index) class ~~ anObject class ifTrue:[ |
2687 index := nil. |
2685 index := nil. |
2688 ]. |
2686 ]. |
2689 ]. |
2687 ]. |
2690 ]. |
2688 ]. |