Browse Source

- Queue class comment update
- Change the API of Queue to use #next and #nextPut:

Nicolas Petton 11 years ago
parent
commit
748f39c963

+ 23 - 23
js/Kernel-Collections.deploy.js

@@ -3629,35 +3629,38 @@ smalltalk.Set);
 
 smalltalk.addClass('Queue', smalltalk.Object, ['read', 'readIndex', 'write'], 'Kernel-Collections');
 smalltalk.addMethod(
-"_back_",
+"_initialize",
 smalltalk.method({
-selector: "back:",
-fn: function (anObject){
+selector: "initialize",
+fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self["@write"])._add_(anObject);
-return self}, function($ctx1) {$ctx1.fill(self,"back:",{anObject:anObject},smalltalk.Queue)})},
-messageSends: ["add:"]}),
+return smalltalk.withContext(function($ctx1) { 
smalltalk.Object.fn.prototype._initialize.apply(_st(self), []);
+self["@read"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
+self["@write"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
+self["@readIndex"]=(1);
+return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},smalltalk.Queue)})},
+messageSends: ["initialize", "new"]}),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_front",
+"_next",
 smalltalk.method({
-selector: "front",
+selector: "next",
 fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
var $1;
-$1=_st(self)._frontIfAbsent_((function(){
+$1=_st(self)._nextIfAbsent_((function(){
 return smalltalk.withContext(function($ctx2) {
return _st(self)._error_("Cannot read from empty Queue.");
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
 return $1;
-}, function($ctx1) {$ctx1.fill(self,"front",{},smalltalk.Queue)})},
-messageSends: ["frontIfAbsent:", "error:"]}),
+}, function($ctx1) {$ctx1.fill(self,"next",{},smalltalk.Queue)})},
+messageSends: ["nextIfAbsent:", "error:"]}),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_frontIfAbsent_",
+"_nextIfAbsent_",
 smalltalk.method({
-selector: "frontIfAbsent:",
+selector: "nextIfAbsent:",
 fn: function (aBlock){
 var self=this;
 var result;
@@ -3691,22 +3694,19 @@ $4=result;
 return $4;
 }
 catch(e) {if(e===$early)return e[0]; throw e}
-}, function($ctx1) {$ctx1.fill(self,"frontIfAbsent:",{aBlock:aBlock,result:result},smalltalk.Queue)})},
+}, function($ctx1) {$ctx1.fill(self,"nextIfAbsent:",{aBlock:aBlock,result:result},smalltalk.Queue)})},
 messageSends: ["at:ifAbsent:", "ifTrue:", ">", "value", "isEmpty", "new", "first", "at:put:", "+"]}),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_initialize",
+"_nextPut_",
 smalltalk.method({
-selector: "initialize",
-fn: function (){
+selector: "nextPut:",
+fn: function (anObject){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
smalltalk.Object.fn.prototype._initialize.apply(_st(self), []);
-self["@read"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
-self["@write"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
-self["@readIndex"]=(1);
-return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},smalltalk.Queue)})},
-messageSends: ["initialize", "new"]}),
+return smalltalk.withContext(function($ctx1) { 
_st(self["@write"])._add_(anObject);
+return self}, function($ctx1) {$ctx1.fill(self,"nextPut:",{anObject:anObject},smalltalk.Queue)})},
+messageSends: ["add:"]}),
 smalltalk.Queue);
 
 

+ 34 - 34
js/Kernel-Collections.js

@@ -4919,47 +4919,50 @@ smalltalk.Set);
 
 
 smalltalk.addClass('Queue', smalltalk.Object, ['read', 'readIndex', 'write'], 'Kernel-Collections');
-smalltalk.Queue.comment="A Queue am a one-sided queue.\x0a\x0aA Queue uses two OrderedCollections inside,\x0a`read` is at the front, is not modified and only read using `readIndex`.\x0a`write` is at the back and is appended new items.\x0aWhen `read` is exhausted, `write` is promoted to `read` and new `write` is created.\x0a\x0aAs a consequence, no data moving is done by the Queue; write appending may do data moving\x0awhen growing `write`, but this is left to engine to implement as good as it chooses to."
+smalltalk.Queue.comment="I am a one-sided queue.\x0a\x0a## Usage\x0a\x0aUse `#nextPut:` to add items to the queue.\x0aUse `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.\x0a\x0a## Implementation notes\x0a\x0aA Queue uses two OrderedCollections inside,\x0a`read` is at the front, is not modified and only read using `readIndex`.\x0a`write` is at the back and is appended new items.\x0aWhen `read` is exhausted, `write` is promoted to `read` and new `write` is created.\x0a\x0aAs a consequence, no data moving is done by me, write appending may do data moving\x0awhen growing `write`, but this is left to engine to implement as good as it chooses to."
 smalltalk.addMethod(
-"_back_",
+"_initialize",
 smalltalk.method({
-selector: "back:",
-category: 'accessing',
-fn: function (anObject){
+selector: "initialize",
+category: 'initialization',
+fn: function (){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
_st(self["@write"])._add_(anObject);
-return self}, function($ctx1) {$ctx1.fill(self,"back:",{anObject:anObject},smalltalk.Queue)})},
-args: ["anObject"],
-source: "back: anObject\x0a\x09write add: anObject",
-messageSends: ["add:"],
-referencedClasses: []
+return smalltalk.withContext(function($ctx1) { 
smalltalk.Object.fn.prototype._initialize.apply(_st(self), []);
+self["@read"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
+self["@write"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
+self["@readIndex"]=(1);
+return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},smalltalk.Queue)})},
+args: [],
+source: "initialize\x0a\x09super initialize.\x0a\x09read := OrderedCollection new.\x0a\x09write := OrderedCollection new.\x0a\x09readIndex := 1",
+messageSends: ["initialize", "new"],
+referencedClasses: ["OrderedCollection"]
 }),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_front",
+"_next",
 smalltalk.method({
-selector: "front",
+selector: "next",
 category: 'accessing',
 fn: function (){
 var self=this;
 return smalltalk.withContext(function($ctx1) { 
var $1;
-$1=_st(self)._frontIfAbsent_((function(){
+$1=_st(self)._nextIfAbsent_((function(){
 return smalltalk.withContext(function($ctx2) {
return _st(self)._error_("Cannot read from empty Queue.");
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1)})}));
 return $1;
-}, function($ctx1) {$ctx1.fill(self,"front",{},smalltalk.Queue)})},
+}, function($ctx1) {$ctx1.fill(self,"next",{},smalltalk.Queue)})},
 args: [],
-source: "front\x0a\x09^self frontIfAbsent: [ self error: 'Cannot read from empty Queue.' ]",
-messageSends: ["frontIfAbsent:", "error:"],
+source: "next\x0a\x09^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]",
+messageSends: ["nextIfAbsent:", "error:"],
 referencedClasses: []
 }),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_frontIfAbsent_",
+"_nextIfAbsent_",
 smalltalk.method({
-selector: "frontIfAbsent:",
+selector: "nextIfAbsent:",
 category: 'accessing',
 fn: function (aBlock){
 var self=this;
@@ -4994,30 +4997,27 @@ $4=result;
 return $4;
 }
 catch(e) {if(e===$early)return e[0]; throw e}
-}, function($ctx1) {$ctx1.fill(self,"frontIfAbsent:",{aBlock:aBlock,result:result},smalltalk.Queue)})},
+}, function($ctx1) {$ctx1.fill(self,"nextIfAbsent:",{aBlock:aBlock,result:result},smalltalk.Queue)})},
 args: ["aBlock"],
-source: "frontIfAbsent: aBlock\x0a\x09| result |\x0a\x09result := read at: readIndex ifAbsent: [\x0a\x09\x09write isEmpty ifTrue: [\x0a\x09\x09\x09readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].\x0a\x09\x09\x09^aBlock value ].\x0a\x09\x09read := write.\x0a\x09\x09readIndex := 1.\x0a\x09\x09write := OrderedCollection new.\x0a\x09\x09read first ].\x0a\x09read at: readIndex put: nil.\x0a\x09readIndex := readIndex + 1.\x0a\x09^result",
+source: "nextIfAbsent: aBlock\x0a\x09| result |\x0a\x09result := read at: readIndex ifAbsent: [\x0a\x09\x09write isEmpty ifTrue: [\x0a\x09\x09\x09readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].\x0a\x09\x09\x09^aBlock value ].\x0a\x09\x09read := write.\x0a\x09\x09readIndex := 1.\x0a\x09\x09write := OrderedCollection new.\x0a\x09\x09read first ].\x0a\x09read at: readIndex put: nil.\x0a\x09readIndex := readIndex + 1.\x0a\x09^result",
 messageSends: ["at:ifAbsent:", "ifTrue:", ">", "value", "isEmpty", "new", "first", "at:put:", "+"],
 referencedClasses: ["OrderedCollection"]
 }),
 smalltalk.Queue);
 
 smalltalk.addMethod(
-"_initialize",
+"_nextPut_",
 smalltalk.method({
-selector: "initialize",
-category: 'initialization',
-fn: function (){
+selector: "nextPut:",
+category: 'accessing',
+fn: function (anObject){
 var self=this;
-return smalltalk.withContext(function($ctx1) { 
smalltalk.Object.fn.prototype._initialize.apply(_st(self), []);
-self["@read"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
-self["@write"]=_st((smalltalk.OrderedCollection || OrderedCollection))._new();
-self["@readIndex"]=(1);
-return self}, function($ctx1) {$ctx1.fill(self,"initialize",{},smalltalk.Queue)})},
-args: [],
-source: "initialize\x0a\x09super initialize.\x0a\x09read := OrderedCollection new.\x0a\x09write := OrderedCollection new.\x0a\x09readIndex := 1",
-messageSends: ["initialize", "new"],
-referencedClasses: ["OrderedCollection"]
+return smalltalk.withContext(function($ctx1) { 
_st(self["@write"])._add_(anObject);
+return self}, function($ctx1) {$ctx1.fill(self,"nextPut:",{anObject:anObject},smalltalk.Queue)})},
+args: ["anObject"],
+source: "nextPut: anObject\x0a\x09write add: anObject",
+messageSends: ["add:"],
+referencedClasses: []
 }),
 smalltalk.Queue);
 

+ 4 - 4
js/Kernel-Methods.deploy.js

@@ -528,9 +528,9 @@ $1=_st(self["@poolSize"]).__lt(_st(self)._maxPoolSize());
 if(smalltalk.assert($1)){
 _st(self)._addWorker();
 };
-_st(self["@queue"])._back_(aBlock);
+_st(self["@queue"])._nextPut_(aBlock);
 return self}, function($ctx1) {$ctx1.fill(self,"fork:",{aBlock:aBlock},smalltalk.ForkPool)})},
-messageSends: ["ifTrue:", "addWorker", "<", "maxPoolSize", "back:"]}),
+messageSends: ["ifTrue:", "addWorker", "<", "maxPoolSize", "nextPut:"]}),
 smalltalk.ForkPool);
 
 smalltalk.addMethod(
@@ -560,7 +560,7 @@ $1=(function(){
 var block;
 return smalltalk.withContext(function($ctx2) {
self["@poolSize"]=_st(self["@poolSize"]).__minus((1));
 self["@poolSize"];
-block=_st(self["@queue"])._frontIfAbsent_((function(){
+block=_st(self["@queue"])._nextIfAbsent_((function(){
 return smalltalk.withContext(function($ctx3) {
return sentinel;
 }, function($ctx3) {$ctx3.fillBlock({},$ctx1)})}));
 block;
@@ -575,7 +575,7 @@ return smalltalk.withContext(function($ctx3) {
return _st(self)._addWorker();
 }, function($ctx2) {$ctx2.fillBlock({block:block},$ctx1)})});
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"makeWorker",{sentinel:sentinel},smalltalk.ForkPool)})},
-messageSends: ["new", "-", "frontIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="]}),
+messageSends: ["new", "-", "nextIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="]}),
 smalltalk.ForkPool);
 
 smalltalk.addMethod(

+ 6 - 6
js/Kernel-Methods.js

@@ -732,11 +732,11 @@ $1=_st(self["@poolSize"]).__lt(_st(self)._maxPoolSize());
 if(smalltalk.assert($1)){
 _st(self)._addWorker();
 };
-_st(self["@queue"])._back_(aBlock);
+_st(self["@queue"])._nextPut_(aBlock);
 return self}, function($ctx1) {$ctx1.fill(self,"fork:",{aBlock:aBlock},smalltalk.ForkPool)})},
 args: ["aBlock"],
-source: "fork: aBlock\x0a\x09poolSize < self maxPoolSize ifTrue: [ self addWorker ].\x0a\x09queue back: aBlock",
-messageSends: ["ifTrue:", "addWorker", "<", "maxPoolSize", "back:"],
+source: "fork: aBlock\x0a\x09poolSize < self maxPoolSize ifTrue: [ self addWorker ].\x0a\x09queue nextPut: aBlock",
+messageSends: ["ifTrue:", "addWorker", "<", "maxPoolSize", "nextPut:"],
 referencedClasses: []
 }),
 smalltalk.ForkPool);
@@ -774,7 +774,7 @@ $1=(function(){
 var block;
 return smalltalk.withContext(function($ctx2) {
self["@poolSize"]=_st(self["@poolSize"]).__minus((1));
 self["@poolSize"];
-block=_st(self["@queue"])._frontIfAbsent_((function(){
+block=_st(self["@queue"])._nextIfAbsent_((function(){
 return smalltalk.withContext(function($ctx3) {
return sentinel;
 }, function($ctx3) {$ctx3.fillBlock({},$ctx1)})}));
 block;
@@ -790,8 +790,8 @@ return smalltalk.withContext(function($ctx3) {
return _st(self)._addWorker();
 return $1;
 }, function($ctx1) {$ctx1.fill(self,"makeWorker",{sentinel:sentinel},smalltalk.ForkPool)})},
 args: [],
-source: "makeWorker\x0a\x09| sentinel |\x0a\x09sentinel := Object new.\x0a\x09^[ | block |\x0a\x09\x09poolSize := poolSize - 1.\x0a\x09\x09block := queue frontIfAbsent: [ sentinel ].\x0a\x09\x09block == sentinel ifFalse: [\x0a\x09\x09\x09[ block value ] ensure: [ self addWorker ]]]",
-messageSends: ["new", "-", "frontIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="],
+source: "makeWorker\x0a\x09| sentinel |\x0a\x09sentinel := Object new.\x0a\x09^[ | block |\x0a\x09\x09poolSize := poolSize - 1.\x0a\x09\x09block := queue nextIfAbsent: [ sentinel ].\x0a\x09\x09block == sentinel ifFalse: [\x0a\x09\x09\x09[ block value ] ensure: [ self addWorker ]]]",
+messageSends: ["new", "-", "nextIfAbsent:", "ifFalse:", "ensure:", "addWorker", "value", "=="],
 referencedClasses: ["Object"]
 }),
 smalltalk.ForkPool);

+ 16 - 9
st/Kernel-Collections.st

@@ -1637,27 +1637,30 @@ Object subclass: #Queue
 	instanceVariableNames: 'read readIndex write'
 	package: 'Kernel-Collections'!
 !Queue commentStamp!
-A Queue am a one-sided queue.
+I am a one-sided queue.
+
+## Usage
+
+Use `#nextPut:` to add items to the queue.
+Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
+
+## Implementation notes
 
 A Queue uses two OrderedCollections inside,
 `read` is at the front, is not modified and only read using `readIndex`.
 `write` is at the back and is appended new items.
 When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
 
-As a consequence, no data moving is done by the Queue; write appending may do data moving
+As a consequence, no data moving is done by me, write appending may do data moving
 when growing `write`, but this is left to engine to implement as good as it chooses to.!
 
 !Queue methodsFor: 'accessing'!
 
-back: anObject
-	write add: anObject
-!
-
-front
-	^self frontIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
+next
+	^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
 !
 
-frontIfAbsent: aBlock
+nextIfAbsent: aBlock
 	| result |
 	result := read at: readIndex ifAbsent: [
 		write isEmpty ifTrue: [
@@ -1670,6 +1673,10 @@ frontIfAbsent: aBlock
 	read at: readIndex put: nil.
 	readIndex := readIndex + 1.
 	^result
+!
+
+nextPut: anObject
+	write add: anObject
 ! !
 
 !Queue methodsFor: 'initialization'!

+ 2 - 2
st/Kernel-Methods.st

@@ -272,7 +272,7 @@ maxPoolSize: anInteger
 
 fork: aBlock
 	poolSize < self maxPoolSize ifTrue: [ self addWorker ].
-	queue back: aBlock
+	queue nextPut: aBlock
 ! !
 
 !ForkPool methodsFor: 'defaults'!
@@ -296,7 +296,7 @@ makeWorker
 	sentinel := Object new.
 	^[ | block |
 		poolSize := poolSize - 1.
-		block := queue frontIfAbsent: [ sentinel ].
+		block := queue nextIfAbsent: [ sentinel ].
 		block == sentinel ifFalse: [
 			[ block value ] ensure: [ self addWorker ]]]
 ! !