Browse Source

Collection >> removeAll

Herbert Vojčík 10 years ago
parent
commit
ce483647b2
4 changed files with 136 additions and 0 deletions
  1. 71 0
      js/Kernel-Collections.js
  2. 40 0
      js/Kernel-Tests.js
  3. 17 0
      st/Kernel-Collections.st
  4. 8 0
      st/Kernel-Tests.st

+ 71 - 0
js/Kernel-Collections.js

@@ -764,6 +764,22 @@ referencedClasses: []
 }),
 smalltalk.Collection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "removeAll",
+category: 'adding/removing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self._subclassResponsibility();
+return self}, function($ctx1) {$ctx1.fill(self,"removeAll",{},smalltalk.Collection)})},
+args: [],
+source: "removeAll\x0a\x09self subclassResponsibility",
+messageSends: ["subclassResponsibility"],
+referencedClasses: []
+}),
+smalltalk.Collection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "select:",
@@ -1664,6 +1680,27 @@ referencedClasses: []
 }),
 smalltalk.HashedCollection);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "removeAll",
+category: 'adding/removing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1;
+$1=_st(self._keys())._do_((function(each){
+return smalltalk.withContext(function($ctx2) {
+return self._removeKey_(each);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)})}));
+return $1;
+}, function($ctx1) {$ctx1.fill(self,"removeAll",{},smalltalk.HashedCollection)})},
+args: [],
+source: "removeAll\x0a\x09^ self keys do: [ :each | self removeKey: each ]",
+messageSends: ["do:", "keys", "removeKey:"],
+referencedClasses: []
+}),
+smalltalk.HashedCollection);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "removeKey:",
@@ -2130,6 +2167,24 @@ referencedClasses: []
 }),
 smalltalk.Dictionary);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "removeAll",
+category: 'adding/removing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+_st(self["@keys"])._removeAll();
+$ctx1.sendIdx["removeAll"]=1;
+_st(self["@values"])._removeAll();
+return self}, function($ctx1) {$ctx1.fill(self,"removeAll",{},smalltalk.Dictionary)})},
+args: [],
+source: "removeAll\x0a\x09keys removeAll.\x0a\x09values removeAll",
+messageSends: ["removeAll"],
+referencedClasses: []
+}),
+smalltalk.Dictionary);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "removeKey:ifAbsent:",
@@ -3000,6 +3055,22 @@ referencedClasses: []
 }),
 smalltalk.Array);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "removeAll",
+category: 'adding/removing',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+self.length = 0;
+return self}, function($ctx1) {$ctx1.fill(self,"removeAll",{},smalltalk.Array)})},
+args: [],
+source: "removeAll\x0a\x09<self.length = 0>",
+messageSends: [],
+referencedClasses: []
+}),
+smalltalk.Array);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "removeFrom:to:",

+ 40 - 0
js/Kernel-Tests.js

@@ -1441,6 +1441,26 @@ referencedClasses: []
 }),
 smalltalk.CollectionTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testRemoveAll",
+category: 'tests',
+fn: function (){
+var self=this;
+return smalltalk.withContext(function($ctx1) { 
+var $1,$2;
+$1=self._collection();
+_st($1)._removeAll();
+$2=_st($1)._yourself();
+self._assert_equals_($2,_st(self._collectionClass())._new());
+return self}, function($ctx1) {$ctx1.fill(self,"testRemoveAll",{},smalltalk.CollectionTest)})},
+args: [],
+source: "testRemoveAll\x0a\x09self assert: (self collection removeAll; yourself) equals: self collectionClass new",
+messageSends: ["assert:equals:", "removeAll", "collection", "yourself", "new", "collectionClass"],
+referencedClasses: []
+}),
+smalltalk.CollectionTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testSelect",
@@ -3468,6 +3488,26 @@ referencedClasses: []
 }),
 smalltalk.StringTest);
 
+smalltalk.addMethod(
+smalltalk.method({
+selector: "testRemoveAll",
+category: 'tests',
+fn: function (){
+var self=this;
+function $Error(){return smalltalk.Error||(typeof Error=="undefined"?nil:Error)}
+return smalltalk.withContext(function($ctx1) { 
+self._should_raise_((function(){
+return smalltalk.withContext(function($ctx2) {
+return _st(self._collection())._removeAll();
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)})}),$Error());
+return self}, function($ctx1) {$ctx1.fill(self,"testRemoveAll",{},smalltalk.StringTest)})},
+args: [],
+source: "testRemoveAll\x0a\x09self should: [ self collection removeAll ] raise: Error",
+messageSends: ["should:raise:", "removeAll", "collection"],
+referencedClasses: ["Error"]
+}),
+smalltalk.StringTest);
+
 smalltalk.addMethod(
 smalltalk.method({
 selector: "testReversed",

+ 17 - 0
st/Kernel-Collections.st

@@ -91,6 +91,10 @@ remove: anObject
 
 remove: anObject ifAbsent: aBlock
 	self subclassResponsibility
+!
+
+removeAll
+	self subclassResponsibility
 ! !
 
 !Collection methodsFor: 'converting'!
@@ -473,6 +477,10 @@ remove: aKey ifAbsent: aBlock
 	^ self removeKey: aKey ifAbsent: aBlock
 !
 
+removeAll
+	^ self keys do: [ :each | self removeKey: each ]
+!
+
 removeKey: aKey
 	^ self remove: aKey
 !
@@ -678,6 +686,11 @@ values
 
 !Dictionary methodsFor: 'adding/removing'!
 
+removeAll
+	keys removeAll.
+	values removeAll
+!
+
 removeKey: aKey ifAbsent: aBlock
 	<
 		var index = self._positionOfKey_(aKey);
@@ -994,6 +1007,10 @@ remove: anObject ifAbsent: aBlock
 	>
 !
 
+removeAll
+	<self.length = 0>
+!
+
 removeFrom: aNumber to: anotherNumber
 	<self.splice(aNumber -1, anotherNumber - aNumber + 1)>
 !

+ 8 - 0
st/Kernel-Tests.st

@@ -434,6 +434,10 @@ testIsEmpty
 	self deny: self collection isEmpty
 !
 
+testRemoveAll
+	self assert: (self collection removeAll; yourself) equals: self collectionClass new
+!
+
 testSelect
 	| newCollection |
 	newCollection := #(2 -4).
@@ -1057,6 +1061,10 @@ testJoin
 	self assert: (',' join: #('hello' 'world')) equals: 'hello,world'
 !
 
+testRemoveAll
+	self should: [ self collection removeAll ] raise: Error
+!
+
 testReversed
 	self assert: 'jackiechan' reversed equals: 'nahceikcaj'.
 !