Browse Source

IndexableCollection >> at: family

Herbert Vojčík 12 years ago
parent
commit
8b5e650122
3 changed files with 191 additions and 197 deletions
  1. 63 73
      js/Kernel-Collections.deploy.js
  2. 88 98
      js/Kernel-Collections.js
  3. 40 26
      st/Kernel-Collections.st

+ 63 - 73
js/Kernel-Collections.deploy.js

@@ -795,6 +795,69 @@ smalltalk.Collection.klass);
 
 
 
 
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
+smalltalk.addMethod(
+"_at_",
+smalltalk.method({
+selector: "at:",
+fn: function (anIndex){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifAbsent_(anIndex,(function(){
+return smalltalk.withContext(function($ctx2) {
return _st(self)._errorNotFound();
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"at:",{anIndex:anIndex}, smalltalk.IndexableCollection)})},
+messageSends: ["at:ifAbsent:", "errorNotFound"]}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifAbsent_",
+smalltalk.method({
+selector: "at:ifAbsent:",
+fn: function (anIndex,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+messageSends: ["subclassReponsibility"]}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifPresent_",
+smalltalk.method({
+selector: "at:ifPresent:",
+fn: function (anIndex,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifPresent_ifAbsent_(anIndex,aBlock,(function(){
+return smalltalk.withContext(function($ctx2) {
return nil;
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+messageSends: ["at:ifPresent:ifAbsent:"]}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifPresent_ifAbsent_",
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock}, smalltalk.IndexableCollection)})},
+messageSends: ["subclassReponsibility"]}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_put_",
+smalltalk.method({
+selector: "at:put:",
+fn: function (anIndex,anObject){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:put:",{anIndex:anIndex,anObject:anObject}, smalltalk.IndexableCollection)})},
+messageSends: ["subclassReponsibility"]}),
+smalltalk.IndexableCollection);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_with_do_",
 "_with_do_",
 smalltalk.method({
 smalltalk.method({
@@ -952,23 +1015,6 @@ return self}, function($ctx1) {$ctx1.fill(self,"associationsDo:",{aBlock:aBlock}
 messageSends: ["keysAndValuesDo:", "value:", "key:value:"]}),
 messageSends: ["keysAndValuesDo:", "value:", "key:value:"]}),
 smalltalk.HashedCollection);
 smalltalk.HashedCollection);
 
 
-smalltalk.addMethod(
-"_at_",
-smalltalk.method({
-selector: "at:",
-fn: function (aKey){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=_st(self)._at_ifAbsent_(aKey,(function(){
-return smalltalk.withContext(function($ctx2) {
-return _st(self)._errorNotFound();
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:",{aKey:aKey}, smalltalk.HashedCollection)})},
-messageSends: ["at:ifAbsent:", "errorNotFound"]}),
-smalltalk.HashedCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_at_ifAbsent_",
 "_at_ifAbsent_",
 smalltalk.method({
 smalltalk.method({
@@ -1004,25 +1050,6 @@ return $1;
 messageSends: ["at:ifAbsent:", "at:put:", "value"]}),
 messageSends: ["at:ifAbsent:", "at:put:", "value"]}),
 smalltalk.HashedCollection);
 smalltalk.HashedCollection);
 
 
-smalltalk.addMethod(
-"_at_ifPresent_",
-smalltalk.method({
-selector: "at:ifPresent:",
-fn: function (aKey,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $2,$1;
-$2=_st(self)._includesKey_(aKey);
-if(smalltalk.assert($2)){
-$1=_st(aBlock)._value_(_st(self)._at_(aKey));
-} else {
-$1=nil;
-};
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{aKey:aKey,aBlock:aBlock}, smalltalk.HashedCollection)})},
-messageSends: ["ifTrue:ifFalse:", "value:", "at:", "includesKey:"]}),
-smalltalk.HashedCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_at_ifPresent_ifAbsent_",
 "_at_ifPresent_ifAbsent_",
 smalltalk.method({
 smalltalk.method({
@@ -1698,43 +1725,6 @@ return $1;
 messageSends: ["copyFrom:to:", "-", "size"]}),
 messageSends: ["copyFrom:to:", "-", "size"]}),
 smalltalk.SequenceableCollection);
 smalltalk.SequenceableCollection);
 
 
-smalltalk.addMethod(
-"_at_",
-smalltalk.method({
-selector: "at:",
-fn: function (anIndex){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
var $1;
-$1=_st(self)._at_ifAbsent_(anIndex,(function(){
-return smalltalk.withContext(function($ctx2) {
return _st(self)._errorNotFound();
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:",{anIndex:anIndex}, smalltalk.SequenceableCollection)})},
-messageSends: ["at:ifAbsent:", "errorNotFound"]}),
-smalltalk.SequenceableCollection);
-
-smalltalk.addMethod(
-"_at_ifAbsent_",
-smalltalk.method({
-selector: "at:ifAbsent:",
-fn: function (anIndex,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassResponsibility();
-return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.SequenceableCollection)})},
-messageSends: ["subclassResponsibility"]}),
-smalltalk.SequenceableCollection);
-
-smalltalk.addMethod(
-"_at_put_",
-smalltalk.method({
-selector: "at:put:",
-fn: function (anIndex,anObject){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassResponsibility();
-return self}, function($ctx1) {$ctx1.fill(self,"at:put:",{anIndex:anIndex,anObject:anObject}, smalltalk.SequenceableCollection)})},
-messageSends: ["subclassResponsibility"]}),
-smalltalk.SequenceableCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_atRandom",
 "_atRandom",
 smalltalk.method({
 smalltalk.method({

+ 88 - 98
js/Kernel-Collections.js

@@ -1036,6 +1036,94 @@ smalltalk.Collection.klass);
 
 
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
 smalltalk.addClass('IndexableCollection', smalltalk.Collection, [], 'Kernel-Collections');
 smalltalk.IndexableCollection.comment="An IndexableCollection is a key-value store, that is,\x0ait stores values under indexes.\x0a\x0aAs a rule of thumb, if a collection has at: and at:put:,\x0ait is an IndexableCollection."
 smalltalk.IndexableCollection.comment="An IndexableCollection is a key-value store, that is,\x0ait stores values under indexes.\x0a\x0aAs a rule of thumb, if a collection has at: and at:put:,\x0ait is an IndexableCollection."
+smalltalk.addMethod(
+"_at_",
+smalltalk.method({
+selector: "at:",
+category: 'accessing',
+fn: function (anIndex){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifAbsent_(anIndex,(function(){
+return smalltalk.withContext(function($ctx2) {
return _st(self)._errorNotFound();
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"at:",{anIndex:anIndex}, smalltalk.IndexableCollection)})},
+args: ["anIndex"],
+source: "at: anIndex\x0a\x09\x22Lookup the given index in the receiver. \x0a\x09If it is present, answer the value stored at anIndex. \x0a\x09Otherwise, raise an error.\x22\x0a\x0a\x09^self at: anIndex ifAbsent: [ self errorNotFound ]",
+messageSends: ["at:ifAbsent:", "errorNotFound"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifAbsent_",
+smalltalk.method({
+selector: "at:ifAbsent:",
+category: 'accessing',
+fn: function (anIndex,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+args: ["anIndex", "aBlock"],
+source: "at: anIndex ifAbsent: aBlock\x0a\x09\x22Lookup the given index in the receiver. \x0a\x09If it is present, answer the value stored at anIndex. \x0a\x09Otherwise, answer the value of aBlock.\x22\x0a\x0a\x09self subclassReponsibility",
+messageSends: ["subclassReponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifPresent_",
+smalltalk.method({
+selector: "at:ifPresent:",
+category: 'accessing',
+fn: function (anIndex,aBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
var $1;
+$1=_st(self)._at_ifPresent_ifAbsent_(anIndex,aBlock,(function(){
+return smalltalk.withContext(function($ctx2) {
return nil;
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.IndexableCollection)})},
+args: ["anIndex", "aBlock"],
+source: "at: anIndex ifPresent: aBlock\x0a\x09\x22Lookup the given index in the receiver. \x0a\x09If it is present, answer the value of evaluating aBlock with the value stored at anIndex. \x0a\x09Otherwise, answer nil.\x22\x0a\x0a\x09^self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]",
+messageSends: ["at:ifPresent:ifAbsent:"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_ifPresent_ifAbsent_",
+smalltalk.method({
+selector: "at:ifPresent:ifAbsent:",
+category: 'accessing',
+fn: function (anIndex,aBlock,anotherBlock){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:ifAbsent:",{anIndex:anIndex,aBlock:aBlock,anotherBlock:anotherBlock}, smalltalk.IndexableCollection)})},
+args: ["anIndex", "aBlock", "anotherBlock"],
+source: "at: anIndex ifPresent: aBlock ifAbsent: anotherBlock\x0a\x09\x22Lookup the given index in the receiver. \x0a\x09If it is present, answer the value of evaluating aBlock with the value stored at anIndex.\x0a\x09Otherwise, answer the value of anotherBlock.\x22\x0a\x0a\x09self subclassReponsibility",
+messageSends: ["subclassReponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
+smalltalk.addMethod(
+"_at_put_",
+smalltalk.method({
+selector: "at:put:",
+category: 'accessing',
+fn: function (anIndex,anObject){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassReponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"at:put:",{anIndex:anIndex,anObject:anObject}, smalltalk.IndexableCollection)})},
+args: ["anIndex", "anObject"],
+source: "at: anIndex put: anObject\x0a\x09\x22Store anObject under the given index in the receiver.\x22\x0a\x0a\x09self subclassReponsibility",
+messageSends: ["subclassReponsibility"],
+referencedClasses: []
+}),
+smalltalk.IndexableCollection);
+
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_with_do_",
 "_with_do_",
 smalltalk.method({
 smalltalk.method({
@@ -1244,28 +1332,6 @@ referencedClasses: ["Association"]
 }),
 }),
 smalltalk.HashedCollection);
 smalltalk.HashedCollection);
 
 
-smalltalk.addMethod(
-"_at_",
-smalltalk.method({
-selector: "at:",
-category: 'accessing',
-fn: function (aKey){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $1;
-$1=_st(self)._at_ifAbsent_(aKey,(function(){
-return smalltalk.withContext(function($ctx2) {
-return _st(self)._errorNotFound();
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:",{aKey:aKey}, smalltalk.HashedCollection)})},
-args: ["aKey"],
-source: "at: aKey\x0a\x09^self at: aKey ifAbsent: [self errorNotFound]",
-messageSends: ["at:ifAbsent:", "errorNotFound"],
-referencedClasses: []
-}),
-smalltalk.HashedCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_at_ifAbsent_",
 "_at_ifAbsent_",
 smalltalk.method({
 smalltalk.method({
@@ -1311,30 +1377,6 @@ referencedClasses: []
 }),
 }),
 smalltalk.HashedCollection);
 smalltalk.HashedCollection);
 
 
-smalltalk.addMethod(
-"_at_ifPresent_",
-smalltalk.method({
-selector: "at:ifPresent:",
-category: 'accessing',
-fn: function (aKey,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
-var $2,$1;
-$2=_st(self)._includesKey_(aKey);
-if(smalltalk.assert($2)){
-$1=_st(aBlock)._value_(_st(self)._at_(aKey));
-} else {
-$1=nil;
-};
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:ifPresent:",{aKey:aKey,aBlock:aBlock}, smalltalk.HashedCollection)})},
-args: ["aKey", "aBlock"],
-source: "at: aKey ifPresent: aBlock\x0a\x09\x22Lookup the given key in the receiver. \x0a\x09If it is present, answer the value of evaluating the given block with the value associated with the key. \x0a\x09Otherwise, answer nil.\x22\x0a\x09^(self includesKey: aKey)\x0a\x09\x09ifTrue: [ aBlock value: (self at: aKey) ]\x0a\x09\x09ifFalse: [ nil ]",
-messageSends: ["ifTrue:ifFalse:", "value:", "at:", "includesKey:"],
-referencedClasses: []
-}),
-smalltalk.HashedCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_at_ifPresent_ifAbsent_",
 "_at_ifPresent_ifAbsent_",
 smalltalk.method({
 smalltalk.method({
@@ -2216,58 +2258,6 @@ referencedClasses: []
 }),
 }),
 smalltalk.SequenceableCollection);
 smalltalk.SequenceableCollection);
 
 
-smalltalk.addMethod(
-"_at_",
-smalltalk.method({
-selector: "at:",
-category: 'accessing',
-fn: function (anIndex){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
var $1;
-$1=_st(self)._at_ifAbsent_(anIndex,(function(){
-return smalltalk.withContext(function($ctx2) {
return _st(self)._errorNotFound();
-}, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
-return $1;
-}, function($ctx1) {$ctx1.fill(self,"at:",{anIndex:anIndex}, smalltalk.SequenceableCollection)})},
-args: ["anIndex"],
-source: "at: anIndex\x0a\x09^self at: anIndex ifAbsent: [\x0a\x09    self errorNotFound]",
-messageSends: ["at:ifAbsent:", "errorNotFound"],
-referencedClasses: []
-}),
-smalltalk.SequenceableCollection);
-
-smalltalk.addMethod(
-"_at_ifAbsent_",
-smalltalk.method({
-selector: "at:ifAbsent:",
-category: 'accessing',
-fn: function (anIndex,aBlock){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassResponsibility();
-return self}, function($ctx1) {$ctx1.fill(self,"at:ifAbsent:",{anIndex:anIndex,aBlock:aBlock}, smalltalk.SequenceableCollection)})},
-args: ["anIndex", "aBlock"],
-source: "at: anIndex ifAbsent: aBlock\x0a\x09self subclassResponsibility",
-messageSends: ["subclassResponsibility"],
-referencedClasses: []
-}),
-smalltalk.SequenceableCollection);
-
-smalltalk.addMethod(
-"_at_put_",
-smalltalk.method({
-selector: "at:put:",
-category: 'accessing',
-fn: function (anIndex,anObject){
-var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self)._subclassResponsibility();
-return self}, function($ctx1) {$ctx1.fill(self,"at:put:",{anIndex:anIndex,anObject:anObject}, smalltalk.SequenceableCollection)})},
-args: ["anIndex", "anObject"],
-source: "at: anIndex put: anObject\x0a\x09self subclassResponsibility",
-messageSends: ["subclassResponsibility"],
-referencedClasses: []
-}),
-smalltalk.SequenceableCollection);
-
 smalltalk.addMethod(
 smalltalk.addMethod(
 "_atRandom",
 "_atRandom",
 smalltalk.method({
 smalltalk.method({

+ 40 - 26
st/Kernel-Collections.st

@@ -312,6 +312,46 @@ it stores values under indexes.
 As a rule of thumb, if a collection has at: and at:put:,
 As a rule of thumb, if a collection has at: and at:put:,
 it is an IndexableCollection.!
 it is an IndexableCollection.!
 
 
+!IndexableCollection methodsFor: 'accessing'!
+
+at: anIndex
+	"Lookup the given index in the receiver. 
+	If it is present, answer the value stored at anIndex. 
+	Otherwise, raise an error."
+
+	^self at: anIndex ifAbsent: [ self errorNotFound ]
+!
+
+at: anIndex ifAbsent: aBlock
+	"Lookup the given index in the receiver. 
+	If it is present, answer the value stored at anIndex. 
+	Otherwise, answer the value of aBlock."
+
+	self subclassReponsibility
+!
+
+at: anIndex ifPresent: aBlock
+	"Lookup the given index in the receiver. 
+	If it is present, answer the value of evaluating aBlock with the value stored at anIndex. 
+	Otherwise, answer nil."
+
+	^self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
+!
+
+at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
+	"Lookup the given index in the receiver. 
+	If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
+	Otherwise, answer the value of anotherBlock."
+
+	self subclassReponsibility
+!
+
+at: anIndex put: anObject
+	"Store anObject under the given index in the receiver."
+
+	self subclassReponsibility
+! !
+
 !IndexableCollection methodsFor: 'enumeration'!
 !IndexableCollection methodsFor: 'enumeration'!
 
 
 with: anotherCollection do: aBlock
 with: anotherCollection do: aBlock
@@ -346,10 +386,6 @@ associations
 	^associations
 	^associations
 !
 !
 
 
-at: aKey
-	^self at: aKey ifAbsent: [self errorNotFound]
-!
-
 at: aKey ifAbsent: aBlock
 at: aKey ifAbsent: aBlock
 	^(self includesKey: aKey)
 	^(self includesKey: aKey)
 		ifTrue: [self basicAt: aKey]
 		ifTrue: [self basicAt: aKey]
@@ -361,15 +397,6 @@ at: aKey ifAbsentPut: aBlock
 	    self at: aKey put: aBlock value]
 	    self at: aKey put: aBlock value]
 !
 !
 
 
-at: aKey ifPresent: aBlock
-	"Lookup the given key in the receiver. 
-	If it is present, answer the value of evaluating the given block with the value associated with the key. 
-	Otherwise, answer nil."
-	^(self includesKey: aKey)
-		ifTrue: [ aBlock value: (self at: aKey) ]
-		ifFalse: [ nil ]
-!
-
 at: aKey ifPresent: aBlock ifAbsent: anotherBlock
 at: aKey ifPresent: aBlock ifAbsent: anotherBlock
 	"Lookup the given key in the receiver. 
 	"Lookup the given key in the receiver. 
 	If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key, 
 	If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key, 
@@ -687,19 +714,6 @@ allButLast
 	^self copyFrom: 1 to: self size - 1
 	^self copyFrom: 1 to: self size - 1
 !
 !
 
 
-at: anIndex
-	^self at: anIndex ifAbsent: [
-	    self errorNotFound]
-!
-
-at: anIndex ifAbsent: aBlock
-	self subclassResponsibility
-!
-
-at: anIndex put: anObject
-	self subclassResponsibility
-!
-
 atRandom
 atRandom
 	^ self at: self size atRandom
 	^ self at: self size atRandom
 !
 !