1
0

Kernel-Collections.st 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. Smalltalk current createPackage: 'Kernel-Collections'!
  2. Object subclass: #Association
  3. instanceVariableNames: 'key value'
  4. package: 'Kernel-Collections'!
  5. !Association commentStamp!
  6. I represent a pair of associated objects, a key and a value. My instances can serve as entries in a dictionary.
  7. Instances can be created with the class-side method `#key:value:`!
  8. !Association methodsFor: 'accessing'!
  9. key
  10. ^key
  11. !
  12. key: aKey
  13. key := aKey
  14. !
  15. value
  16. ^value
  17. !
  18. value: aValue
  19. value := aValue
  20. ! !
  21. !Association methodsFor: 'comparing'!
  22. = anAssociation
  23. ^self class = anAssociation class and: [
  24. self key = anAssociation key and: [
  25. self value = anAssociation value]]
  26. ! !
  27. !Association methodsFor: 'printing'!
  28. printOn: aStream
  29. self key printOn: aStream.
  30. aStream nextPutAll: ' -> '.
  31. self value printOn: aStream
  32. ! !
  33. !Association class methodsFor: 'instance creation'!
  34. key: aKey value: aValue
  35. ^self new
  36. key: aKey;
  37. value: aValue;
  38. yourself
  39. ! !
  40. Object subclass: #Collection
  41. instanceVariableNames: ''
  42. package: 'Kernel-Collections'!
  43. !Collection commentStamp!
  44. I am the abstract superclass of all classes that represent a group of elements.
  45. I provide a set of useful methods to the Collection hierarchy such as enumerating and converting methods.!
  46. !Collection methodsFor: 'accessing'!
  47. occurrencesOf: anObject
  48. "Answer how many of the receiver's elements are equal to anObject."
  49. | tally |
  50. tally := 0.
  51. self do: [:each | anObject = each ifTrue: [tally := tally + 1]].
  52. ^tally
  53. !
  54. size
  55. self subclassResponsibility
  56. ! !
  57. !Collection methodsFor: 'adding/removing'!
  58. add: anObject
  59. self subclassResponsibility
  60. !
  61. addAll: aCollection
  62. aCollection do: [:each |
  63. self add: each].
  64. ^aCollection
  65. !
  66. remove: anObject
  67. ^self remove: anObject ifAbsent: [self errorNotFound]
  68. !
  69. remove: anObject ifAbsent: aBlock
  70. self subclassResponsibility
  71. ! !
  72. !Collection methodsFor: 'converting'!
  73. asArray
  74. ^Array withAll: self
  75. !
  76. asJSON
  77. ^self asArray collect: [:each | each asJSON]
  78. !
  79. asOrderedCollection
  80. ^self asArray
  81. !
  82. asSet
  83. ^Set withAll: self
  84. ! !
  85. !Collection methodsFor: 'copying'!
  86. , aCollection
  87. ^self copy
  88. addAll: aCollection;
  89. yourself
  90. !
  91. copyWith: anObject
  92. ^self copy add: anObject; yourself
  93. !
  94. copyWithAll: aCollection
  95. ^self copy addAll: aCollection; yourself
  96. !
  97. copyWithoutAll: aCollection
  98. "Answer a copy of the receiver that does not contain any elements
  99. equal to those in aCollection."
  100. ^ self reject: [:each | aCollection includes: each]
  101. ! !
  102. !Collection methodsFor: 'enumerating'!
  103. collect: aBlock
  104. | stream |
  105. stream := self class new writeStream.
  106. self do: [ :each |
  107. stream nextPut: (aBlock value: each) ].
  108. ^stream contents
  109. !
  110. detect: aBlock
  111. ^self detect: aBlock ifNone: [self errorNotFound]
  112. !
  113. detect: aBlock ifNone: anotherBlock
  114. self subclassResponsibility
  115. !
  116. do: aBlock
  117. self subclassResponsibility
  118. !
  119. do: aBlock separatedBy: anotherBlock
  120. | actionBeforeElement |
  121. actionBeforeElement := [actionBeforeElement := anotherBlock].
  122. self do: [:each |
  123. actionBeforeElement value.
  124. aBlock value: each]
  125. !
  126. inject: anObject into: aBlock
  127. | result |
  128. result := anObject.
  129. self do: [:each |
  130. result := aBlock value: result value: each].
  131. ^result
  132. !
  133. intersection: aCollection
  134. "Answer the set theoretic intersection of two collections."
  135. | set outputSet |
  136. set := self asSet.
  137. outputSet := Set new.
  138. aCollection do: [ :each |
  139. ((set includes: each) and: [(outputSet includes: each) not])
  140. ifTrue: [
  141. outputSet add: each]].
  142. ^ self class withAll: outputSet asArray
  143. !
  144. reject: aBlock
  145. ^ self select: [ :each | (aBlock value: each) = false ]
  146. !
  147. select: aBlock
  148. | stream |
  149. stream := self class new writeStream.
  150. self do: [:each |
  151. (aBlock value: each) ifTrue: [
  152. stream nextPut: each]].
  153. ^stream contents
  154. !
  155. select: selectBlock thenCollect: collectBlock
  156. | stream |
  157. stream := self class new writeStream.
  158. self do: [:each |
  159. (selectBlock value: each) ifTrue: [
  160. stream nextPut: (collectBlock value: each)]].
  161. ^stream contents
  162. ! !
  163. !Collection methodsFor: 'error handling'!
  164. errorNotFound
  165. self error: 'Object is not in the collection'
  166. ! !
  167. !Collection methodsFor: 'streaming'!
  168. putOn: aStream
  169. self do: [ :each | each putOn: aStream ]
  170. ! !
  171. !Collection methodsFor: 'testing'!
  172. contains: aBlock
  173. "Evaluate aBlock with the elements of the receiver.
  174. If aBlock returns true for any element return true.
  175. Otherwise return false."
  176. self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].
  177. ^ false
  178. !
  179. ifEmpty: aBlock
  180. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver. Note that the fact that this method returns its argument in case the receiver is not empty allows one to write expressions like the following ones: self classifyMethodAs:
  181. (myProtocol ifEmpty: ['As yet unclassified'])"
  182. ^ self isEmpty
  183. ifTrue: [ aBlock value ]
  184. ifFalse: [ self ]
  185. !
  186. ifNotEmpty: aBlock
  187. self notEmpty ifTrue: aBlock.
  188. !
  189. includes: anObject
  190. | sentinel |
  191. sentinel := Object new.
  192. ^(self detect: [ :each | each = anObject] ifNone: [ sentinel ]) ~= sentinel
  193. !
  194. isEmpty
  195. ^self size = 0
  196. !
  197. notEmpty
  198. ^self isEmpty not
  199. ! !
  200. !Collection class methodsFor: 'helios'!
  201. heliosClass
  202. ^ 'collection'
  203. ! !
  204. !Collection class methodsFor: 'instance creation'!
  205. new: anInteger
  206. ^self new
  207. !
  208. with: anObject
  209. ^self new
  210. add: anObject;
  211. yourself
  212. !
  213. with: anObject with: anotherObject
  214. ^self new
  215. add: anObject;
  216. add: anotherObject;
  217. yourself
  218. !
  219. with: firstObject with: secondObject with: thirdObject
  220. ^self new
  221. add: firstObject;
  222. add: secondObject;
  223. add: thirdObject;
  224. yourself
  225. !
  226. withAll: aCollection
  227. ^self new
  228. addAll: aCollection;
  229. yourself
  230. ! !
  231. Collection subclass: #IndexableCollection
  232. instanceVariableNames: ''
  233. package: 'Kernel-Collections'!
  234. !IndexableCollection commentStamp!
  235. I am a key-value store collection, that is,
  236. I store values under indexes.
  237. As a rule of thumb, if a collection has `#at:` and `#at:put:`,
  238. it is an IndexableCollection.!
  239. !IndexableCollection methodsFor: 'accessing'!
  240. at: anIndex
  241. "Lookup the given index in the receiver.
  242. If it is present, answer the value stored at anIndex.
  243. Otherwise, raise an error."
  244. ^self at: anIndex ifAbsent: [ self errorNotFound ]
  245. !
  246. at: anIndex ifAbsent: aBlock
  247. "Lookup the given index in the receiver.
  248. If it is present, answer the value stored at anIndex.
  249. Otherwise, answer the value of aBlock."
  250. self subclassReponsibility
  251. !
  252. at: anIndex ifPresent: aBlock
  253. "Lookup the given index in the receiver.
  254. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  255. Otherwise, answer nil."
  256. ^self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  257. !
  258. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  259. "Lookup the given index in the receiver.
  260. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  261. Otherwise, answer the value of anotherBlock."
  262. self subclassResponsibility
  263. !
  264. at: anIndex put: anObject
  265. "Store anObject under the given index in the receiver."
  266. self subclassReponsibility
  267. !
  268. indexOf: anObject
  269. "Lookup index at which anObject is stored in the receiver.
  270. If not present, raise an error."
  271. ^self indexOf: anObject ifAbsent: [ self errorNotFound ]
  272. !
  273. indexOf: anObject ifAbsent: aBlock
  274. "Lookup index at which anObject is stored in the receiver.
  275. If not present, return value of executing aBlock."
  276. self subclassResponsibility
  277. ! !
  278. !IndexableCollection methodsFor: 'enumarating'!
  279. with: anotherCollection do: aBlock
  280. "Calls aBlock with every value from self
  281. and with indetically-indexed value from anotherCollection"
  282. self withIndexDo: [ :each :index |
  283. aBlock value: each value: (anotherCollection at: index) ]
  284. !
  285. withIndexDo: aBlock
  286. "Calls aBlock with every value from self
  287. and with its index as the second argument"
  288. self subclassReponsibility
  289. ! !
  290. IndexableCollection subclass: #HashedCollection
  291. instanceVariableNames: ''
  292. package: 'Kernel-Collections'!
  293. !HashedCollection commentStamp!
  294. I am a traditional JavaScript object, or a Smalltalk `Dictionary`.
  295. Unlike a `Dictionary`, I can only have strings as keys.!
  296. !HashedCollection methodsFor: 'accessing'!
  297. associations
  298. | associations |
  299. associations := #().
  300. self associationsDo: [:each | associations add: each].
  301. ^associations
  302. !
  303. at: aKey ifAbsent: aBlock
  304. ^(self includesKey: aKey)
  305. ifTrue: [self basicAt: aKey]
  306. ifFalse: aBlock
  307. !
  308. at: aKey ifAbsentPut: aBlock
  309. ^self at: aKey ifAbsent: [
  310. self at: aKey put: aBlock value]
  311. !
  312. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  313. "Lookup the given key in the receiver.
  314. If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
  315. otherwise answer the value of absentBlock."
  316. ^(self includesKey: aKey)
  317. ifTrue: [ aBlock value: (self at: aKey) ]
  318. ifFalse: anotherBlock
  319. !
  320. at: aKey put: aValue
  321. ^self basicAt: aKey put: aValue
  322. !
  323. indexOf: anObject ifAbsent: aBlock
  324. ^ self keys detect: [ :each | (self at: each) = anObject ] ifNone: aBlock
  325. !
  326. keyAtValue: anObject
  327. ^ self keyAtValue: anObject ifAbsent: [ self errorNotFound ]
  328. !
  329. keyAtValue: anObject ifAbsent: aBlock
  330. ^ self indexOf: anObject ifAbsent: aBlock
  331. !
  332. keys
  333. <
  334. if ('function'===typeof Object.keys) return Object.keys(self);
  335. var keys = [];
  336. for(var i in self) {
  337. if(self.hasOwnProperty(i)) {
  338. keys.push(i);
  339. }
  340. };
  341. return keys;
  342. >
  343. !
  344. size
  345. ^self keys size
  346. !
  347. values
  348. <
  349. return self._keys().map(function(key){
  350. return self._at_(key);
  351. });
  352. >
  353. ! !
  354. !HashedCollection methodsFor: 'adding/removing'!
  355. add: anAssociation
  356. self at: anAssociation key put: anAssociation value
  357. !
  358. addAll: aHashedCollection
  359. super addAll: aHashedCollection associations.
  360. ^aHashedCollection
  361. !
  362. remove: aKey ifAbsent: aBlock
  363. ^self removeKey: aKey ifAbsent: aBlock
  364. !
  365. removeKey: aKey
  366. ^self remove: aKey
  367. !
  368. removeKey: aKey ifAbsent: aBlock
  369. ^(self includesKey: aKey)
  370. ifFalse: [aBlock value]
  371. ifTrue: [self basicDelete: aKey]
  372. ! !
  373. !HashedCollection methodsFor: 'comparing'!
  374. = aHashedCollection
  375. self class = aHashedCollection class ifFalse: [^false].
  376. self size = aHashedCollection size ifFalse: [^false].
  377. ^self associations = aHashedCollection associations
  378. ! !
  379. !HashedCollection methodsFor: 'converting'!
  380. asDictionary
  381. ^Dictionary from: self associations
  382. !
  383. asJSON
  384. | c |
  385. c := self class new.
  386. self keysAndValuesDo: [:key :value |
  387. c at: key put: value asJSON].
  388. ^c
  389. ! !
  390. !HashedCollection methodsFor: 'copying'!
  391. , aCollection
  392. self shouldNotImplement
  393. !
  394. deepCopy
  395. | copy |
  396. copy := self class new.
  397. self keysAndValuesDo: [:key :value |
  398. copy at: key put: value deepCopy].
  399. ^copy
  400. !
  401. shallowCopy
  402. | copy |
  403. copy := self class new.
  404. self keysAndValuesDo: [:key :value |
  405. copy at: key put: value].
  406. ^copy
  407. ! !
  408. !HashedCollection methodsFor: 'enumerating'!
  409. associationsDo: aBlock
  410. self keysAndValuesDo: [:key :value |
  411. aBlock value: (Association key: key value: value)]
  412. !
  413. collect: aBlock
  414. | newDict |
  415. newDict := self class new.
  416. self keysAndValuesDo: [:key :value |
  417. newDict at: key put: (aBlock value: value)].
  418. ^newDict
  419. !
  420. detect: aBlock ifNone: anotherBlock
  421. ^self values detect: aBlock ifNone: anotherBlock
  422. !
  423. do: aBlock
  424. self valuesDo: aBlock
  425. !
  426. includes: anObject
  427. ^self values includes: anObject
  428. !
  429. keysAndValuesDo: aBlock
  430. self keysDo: [:each |
  431. aBlock value: each value: (self at: each)]
  432. !
  433. keysDo: aBlock
  434. self keys do: aBlock
  435. !
  436. select: aBlock
  437. | newDict |
  438. newDict := self class new.
  439. self keysAndValuesDo: [:key :value |
  440. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  441. ^newDict
  442. !
  443. valuesDo: aBlock
  444. self values do: [ :value | aBlock value: value ]
  445. !
  446. withIndexDo: aBlock
  447. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  448. ! !
  449. !HashedCollection methodsFor: 'printing'!
  450. printOn: aStream
  451. super printOn: aStream.
  452. aStream nextPutAll: ' ('.
  453. self associations
  454. do: [:each | each printOn: aStream ]
  455. separatedBy: [ aStream nextPutAll: ' , ' ].
  456. aStream nextPutAll: ')'
  457. ! !
  458. !HashedCollection methodsFor: 'testing'!
  459. includesKey: aKey
  460. <return self.hasOwnProperty(aKey)>
  461. ! !
  462. !HashedCollection class methodsFor: 'instance creation'!
  463. from: aCollection
  464. | newCollection |
  465. newCollection := self new.
  466. aCollection do: [ :each | newCollection add: each ].
  467. ^ newCollection
  468. !
  469. fromPairs: aCollection
  470. "This message is poorly named and has been replaced by #from:"
  471. ^ self from: aCollection
  472. !
  473. newFromPairs: aCollection
  474. "Accept an array of elements where every two elements form an
  475. association - the odd element being the key, and the even element the value."
  476. | newCollection |
  477. aCollection size even ifFalse: [
  478. self error: '#newFromPairs only accepts arrays of an even length' ].
  479. newCollection := self new.
  480. ( 1 to: aCollection size by: 2 ) do: [ :each |
  481. newCollection at: (aCollection at: each) put: (aCollection at: each + 1) ].
  482. ^ newCollection
  483. ! !
  484. HashedCollection subclass: #Dictionary
  485. instanceVariableNames: 'keys values'
  486. package: 'Kernel-Collections'!
  487. !Dictionary commentStamp!
  488. I represent a set of elements that can be viewed from one of two perspectives: a set of associations,
  489. or a container of values that are externally named where the name can be any object that responds to `=`.
  490. The external name is referred to as the key.!
  491. !Dictionary methodsFor: 'accessing'!
  492. at: aKey ifAbsent: aBlock
  493. <
  494. var index = self._positionOfKey_(aKey);
  495. return index >>=0 ? self['@values'][index] : aBlock._value();
  496. >
  497. !
  498. at: aKey put: aValue
  499. <
  500. var index = self._positionOfKey_(aKey);
  501. if(index === -1) {
  502. var keys = self['@keys'];
  503. index = keys.length;
  504. keys.push(aKey);
  505. }
  506. return self['@values'][index] = aValue;
  507. >
  508. !
  509. indexOf: anObject ifAbsent: aBlock
  510. | index |
  511. index := values
  512. indexOf: anObject
  513. ifAbsent: [ 0 ].
  514. ^ index = 0
  515. ifTrue: [ aBlock value ]
  516. ifFalse: [ keys at: index ]
  517. !
  518. keys
  519. ^ keys copy
  520. !
  521. values
  522. ^ values
  523. ! !
  524. !Dictionary methodsFor: 'adding/removing'!
  525. removeKey: aKey ifAbsent: aBlock
  526. <
  527. var index = self._positionOfKey_(aKey);
  528. if(index === -1) {
  529. return aBlock._value()
  530. } else {
  531. var keys = self['@keys'], values = self['@values'];
  532. var value = values[index], l = keys.length;
  533. keys[index] = keys[l-1];
  534. keys.pop();
  535. values[index] = values[l-1];
  536. values.pop();
  537. return value;
  538. }
  539. >
  540. ! !
  541. !Dictionary methodsFor: 'converting'!
  542. asHashedCollection
  543. ^ HashedCollection from: self associations
  544. !
  545. asJSON
  546. ^ self asHashedCollection asJSON
  547. ! !
  548. !Dictionary methodsFor: 'enumerating'!
  549. keysAndValuesDo: aBlock
  550. ^ keys with: values do: aBlock
  551. !
  552. keysDo: aBlock
  553. ^ keys do: aBlock
  554. !
  555. valuesDo: aBlock
  556. ^ values do: aBlock
  557. ! !
  558. !Dictionary methodsFor: 'initialization'!
  559. initialize
  560. super initialize.
  561. keys := #().
  562. values := #()
  563. ! !
  564. !Dictionary methodsFor: 'private'!
  565. positionOfKey: anObject
  566. <
  567. var keys = self['@keys'];
  568. for(var i=0;i<keys.length;i++){
  569. if(keys[i].__eq(anObject)) { return i;}
  570. }
  571. return -1;
  572. >
  573. ! !
  574. !Dictionary methodsFor: 'testing'!
  575. includesKey: aKey
  576. < return self._positionOfKey_(aKey) >>= 0; >
  577. ! !
  578. IndexableCollection subclass: #SequenceableCollection
  579. instanceVariableNames: ''
  580. package: 'Kernel-Collections'!
  581. !SequenceableCollection commentStamp!
  582. I am an IndexableCollection
  583. with numeric indexes starting with 1.!
  584. !SequenceableCollection methodsFor: 'accessing'!
  585. allButFirst
  586. ^self copyFrom: 2 to: self size
  587. !
  588. allButLast
  589. ^self copyFrom: 1 to: self size - 1
  590. !
  591. atRandom
  592. ^ self at: self size atRandom
  593. !
  594. first
  595. ^self at: 1
  596. !
  597. first: n
  598. "Answer the first n elements of the receiver.
  599. Raise an error if there are not enough elements."
  600. ^ self copyFrom: 1 to: n
  601. !
  602. fourth
  603. ^self at: 4
  604. !
  605. indexOf: anObject ifAbsent: aBlock
  606. <
  607. for(var i=0;i<self.length;i++) {
  608. if(self[i].__eq(anObject)) {return i+1}
  609. };
  610. return aBlock._value();
  611. >
  612. !
  613. indexOf: anObject startingAt: start
  614. "Answer the index of the first occurence of anElement after start
  615. within the receiver. If the receiver does not contain anElement,
  616. answer 0."
  617. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  618. !
  619. indexOf: anObject startingAt: start ifAbsent: aBlock
  620. <
  621. for(var i=start-1;i<self.length;i++){
  622. if(self[i].__eq(anObject)) {return i+1}
  623. }
  624. return aBlock._value();
  625. >
  626. !
  627. last
  628. ^self at: self size
  629. !
  630. second
  631. ^self at: 2
  632. !
  633. third
  634. ^self at: 3
  635. ! !
  636. !SequenceableCollection methodsFor: 'adding/removing'!
  637. addLast: anObject
  638. self add: anObject
  639. !
  640. removeLast
  641. self remove: self last
  642. ! !
  643. !SequenceableCollection methodsFor: 'comparing'!
  644. = aCollection
  645. (self class = aCollection class and: [
  646. self size = aCollection size]) ifFalse: [^false].
  647. self withIndexDo: [:each :i |
  648. (aCollection at: i) = each ifFalse: [^false]].
  649. ^true
  650. ! !
  651. !SequenceableCollection methodsFor: 'converting'!
  652. reversed
  653. self subclassResponsibility
  654. ! !
  655. !SequenceableCollection methodsFor: 'copying'!
  656. copyFrom: anIndex to: anotherIndex
  657. | range newCollection |
  658. range := anIndex to: anotherIndex.
  659. newCollection := self class new: range size.
  660. range withIndexDo: [:each :i |
  661. newCollection at: i put: (self at: each)].
  662. ^newCollection
  663. !
  664. deepCopy
  665. | newCollection |
  666. newCollection := self class new: self size.
  667. self withIndexDo: [:each :index |
  668. newCollection at: index put: each deepCopy].
  669. ^newCollection
  670. !
  671. shallowCopy
  672. | newCollection |
  673. newCollection := self class new: self size.
  674. self withIndexDo: [ :each :index |
  675. newCollection at: index put: each].
  676. ^newCollection
  677. ! !
  678. !SequenceableCollection methodsFor: 'enumerating'!
  679. detect: aBlock ifNone: anotherBlock
  680. <
  681. for(var i = 0; i < self.length; i++)
  682. if(aBlock._value_(self[i]))
  683. return self[i];
  684. return anotherBlock._value();
  685. >
  686. !
  687. do: aBlock
  688. <for(var i=0;i<self.length;i++){aBlock._value_(self[i]);}>
  689. !
  690. with: anotherCollection do: aBlock
  691. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], anotherCollection[i]);}>
  692. !
  693. withIndexDo: aBlock
  694. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], i+1);}>
  695. ! !
  696. !SequenceableCollection methodsFor: 'streaming'!
  697. newStream
  698. ^self streamClass on: self
  699. !
  700. readStream
  701. "For Pharo compatibility"
  702. ^self stream
  703. !
  704. stream
  705. ^self newStream
  706. !
  707. streamClass
  708. ^self class streamClass
  709. !
  710. writeStream
  711. "For Pharo compatibility"
  712. ^self stream
  713. ! !
  714. !SequenceableCollection methodsFor: 'testing'!
  715. includes: anObject
  716. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  717. ! !
  718. !SequenceableCollection class methodsFor: 'accessing'!
  719. streamClass
  720. ^Stream
  721. ! !
  722. !SequenceableCollection class methodsFor: 'streaming'!
  723. streamContents: aBlock
  724. | stream |
  725. stream := (self streamClass on: self new).
  726. aBlock value: stream.
  727. ^ stream contents
  728. ! !
  729. SequenceableCollection subclass: #Array
  730. instanceVariableNames: ''
  731. package: 'Kernel-Collections'!
  732. !Array commentStamp!
  733. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  734. I am directly mapped to JavaScript Number.
  735. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  736. !Array methodsFor: 'accessing'!
  737. at: anIndex ifAbsent: aBlock
  738. <
  739. if((anIndex < 1) || (self.length < anIndex)) {return aBlock._value()};
  740. return self[anIndex - 1];
  741. >
  742. !
  743. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  744. <return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>
  745. !
  746. at: anIndex put: anObject
  747. <return self[anIndex - 1] = anObject>
  748. !
  749. size
  750. <return self.length>
  751. ! !
  752. !Array methodsFor: 'adding/removing'!
  753. add: anObject
  754. <self.push(anObject); return anObject;>
  755. !
  756. remove: anObject ifAbsent: aBlock
  757. <
  758. for(var i=0;i<self.length;i++) {
  759. if(_st(self[i]).__eq(anObject)) {
  760. self.splice(i,1);
  761. return self;
  762. }
  763. };
  764. aBlock._value();
  765. >
  766. !
  767. removeFrom: aNumber to: anotherNumber
  768. <self.splice(aNumber -1, anotherNumber - aNumber + 1)>
  769. !
  770. removeIndex: anInteger
  771. <self.splice(anInteger - 1, 1)>
  772. ! !
  773. !Array methodsFor: 'converting'!
  774. asJavascript
  775. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  776. !
  777. reversed
  778. <return self._copy().reverse()>
  779. ! !
  780. !Array methodsFor: 'enumerating'!
  781. collect: aBlock
  782. "Optimized version"
  783. <return self.map(function(each) {return aBlock._value_(each)})>
  784. !
  785. join: aString
  786. <return self.join(aString)>
  787. !
  788. select: aBlock
  789. "Optimized version"
  790. <
  791. var result = self.klass._new();
  792. for(var i=0; i<self.length; i++) {
  793. if(aBlock._value_(self[i])) {
  794. result.push(self[i]);
  795. }
  796. }
  797. return result;
  798. >
  799. !
  800. sort
  801. ^self basicPerform: 'sort'
  802. !
  803. sort: aBlock
  804. <
  805. return self.sort(function(a, b) {
  806. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  807. })
  808. >
  809. !
  810. sorted
  811. ^self copy sort
  812. !
  813. sorted: aBlock
  814. ^self copy sort: aBlock
  815. ! !
  816. !Array methodsFor: 'printing'!
  817. printOn: aStream
  818. super printOn: aStream.
  819. aStream nextPutAll: ' ('.
  820. self
  821. do: [ :each | each printOn: aStream ]
  822. separatedBy: [ aStream nextPutAll: ' ' ].
  823. aStream nextPutAll: ')'
  824. ! !
  825. !Array class methodsFor: 'instance creation'!
  826. new: anInteger
  827. <return new Array(anInteger)>
  828. !
  829. with: anObject
  830. ^(self new: 1)
  831. at: 1 put: anObject;
  832. yourself
  833. !
  834. with: anObject with: anObject2
  835. ^(self new: 2)
  836. at: 1 put: anObject;
  837. at: 2 put: anObject2;
  838. yourself
  839. !
  840. with: anObject with: anObject2 with: anObject3
  841. ^(self new: 3)
  842. at: 1 put: anObject;
  843. at: 2 put: anObject2;
  844. at: 3 put: anObject3;
  845. yourself
  846. !
  847. withAll: aCollection
  848. | instance index |
  849. index := 1.
  850. instance := self new: aCollection size.
  851. aCollection do: [:each |
  852. instance at: index put: each.
  853. index := index + 1].
  854. ^instance
  855. ! !
  856. SequenceableCollection subclass: #CharacterArray
  857. instanceVariableNames: ''
  858. package: 'Kernel-Collections'!
  859. !CharacterArray commentStamp!
  860. I am the abstract superclass of string-like collections.!
  861. !CharacterArray methodsFor: 'accessing'!
  862. at: anIndex put: anObject
  863. self errorReadOnly
  864. ! !
  865. !CharacterArray methodsFor: 'adding/removing'!
  866. add: anObject
  867. self errorReadOnly
  868. !
  869. remove: anObject
  870. self errorReadOnly
  871. ! !
  872. !CharacterArray methodsFor: 'converting'!
  873. asLowercase
  874. ^self class fromString: self asString asLowercase
  875. !
  876. asNumber
  877. ^self asString asNumber
  878. !
  879. asString
  880. ^self subclassResponsibility
  881. !
  882. asSymbol
  883. ^self asString
  884. !
  885. asUppercase
  886. ^self class fromString: self asString asUppercase
  887. ! !
  888. !CharacterArray methodsFor: 'copying'!
  889. , aString
  890. ^self asString, aString asString
  891. ! !
  892. !CharacterArray methodsFor: 'error handling'!
  893. errorReadOnly
  894. self error: 'Object is read-only'
  895. ! !
  896. !CharacterArray methodsFor: 'printing'!
  897. printOn: aStream
  898. self asString printOn: aStream
  899. ! !
  900. !CharacterArray methodsFor: 'streaming'!
  901. putOn: aStream
  902. aStream nextPutString: self
  903. ! !
  904. !CharacterArray class methodsFor: 'instance creation'!
  905. fromString: aString
  906. self subclassResponsibility
  907. ! !
  908. CharacterArray subclass: #String
  909. instanceVariableNames: ''
  910. package: 'Kernel-Collections'!
  911. !String commentStamp!
  912. I am an indexed collection of Characters. Unlike most Smalltalk dialects, Amber doesn't provide the Character class. Instead, elements of a String are single character strings.
  913. String inherits many useful methods from its hierarchy, such as
  914. `Collection >> #,`!
  915. !String methodsFor: 'accessing'!
  916. asciiValue
  917. <return self.charCodeAt(0);>
  918. !
  919. at: anIndex ifAbsent: aBlock
  920. <return String(self).charAt(anIndex - 1) || aBlock._value()>
  921. !
  922. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  923. <
  924. var result = String(self).charAt(anIndex - 1);
  925. return result ? aBlock._value_(result) : anotherBlock._value();
  926. >
  927. !
  928. charCodeAt: anInteger
  929. < return self.charCodeAt(anInteger - 1) >
  930. !
  931. escaped
  932. <return escape(self)>
  933. !
  934. size
  935. <return self.length>
  936. !
  937. unescaped
  938. <return unescape(self)>
  939. ! !
  940. !String methodsFor: 'comparing'!
  941. < aString
  942. <return String(self) < aString._asString()>
  943. !
  944. <= aString
  945. <return String(self) <= aString._asString()>
  946. !
  947. = aString
  948. <
  949. if(typeof aString === 'undefined') { return false }
  950. if(!!aString._isString || !! aString._isString()) {
  951. return false;
  952. }
  953. return String(self) === String(aString)
  954. >
  955. !
  956. == aString
  957. ^self = aString
  958. !
  959. > aString
  960. <return String(self) >> aString._asString()>
  961. !
  962. >= aString
  963. <return String(self) >>= aString._asString()>
  964. ! !
  965. !String methodsFor: 'converting'!
  966. asJSON
  967. ^self
  968. !
  969. asJavascript
  970. <
  971. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  972. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  973. else
  974. return "\"" + self + "\"";
  975. >
  976. !
  977. asLowercase
  978. <return self.toLowerCase()>
  979. !
  980. asMutator
  981. "Answer a setter selector. For example,
  982. #name asMutator returns #name:"
  983. self last = ':' ifFalse: [ ^ self, ':' ].
  984. ^ self
  985. !
  986. asNumber
  987. <return Number(self)>
  988. !
  989. asRegexp
  990. ^ RegularExpression fromString: self
  991. !
  992. asSelector
  993. <return smalltalk.selector(self)>
  994. !
  995. asString
  996. ^self
  997. !
  998. asSymbol
  999. ^ self
  1000. !
  1001. asUppercase
  1002. <return self.toUpperCase()>
  1003. !
  1004. crlfSanitized
  1005. ^self lines join: String lf
  1006. !
  1007. reversed
  1008. <return self.split("").reverse().join("")>
  1009. ! !
  1010. !String methodsFor: 'copying'!
  1011. , aString
  1012. <return self + aString>
  1013. !
  1014. copyFrom: anIndex to: anotherIndex
  1015. <return self.substring(anIndex - 1, anotherIndex)>
  1016. !
  1017. deepCopy
  1018. ^self shallowCopy
  1019. !
  1020. shallowCopy
  1021. ^self class fromString: self
  1022. ! !
  1023. !String methodsFor: 'enumerating'!
  1024. do: aBlock
  1025. <for(var i=0;i<self.length;i++){aBlock._value_(self.charAt(i));}>
  1026. !
  1027. withIndexDo: aBlock
  1028. <for(var i=0;i<self.length;i++){aBlock._value_value_(self.charAt(i), i+1);}>
  1029. ! !
  1030. !String methodsFor: 'printing'!
  1031. printNl
  1032. <console.log(self)>
  1033. !
  1034. printOn: aStream
  1035. aStream
  1036. nextPutAll: '''';
  1037. nextPutAll: self;
  1038. nextPutAll: ''''
  1039. ! !
  1040. !String methodsFor: 'regular expressions'!
  1041. match: aRegexp
  1042. <return self.search(aRegexp) !!= -1>
  1043. !
  1044. matchesOf: aRegularExpression
  1045. <return self.match(aRegularExpression)>
  1046. !
  1047. replace: aString with: anotherString
  1048. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1049. !
  1050. replaceRegexp: aRegexp with: aString
  1051. <return self.replace(aRegexp, aString)>
  1052. !
  1053. trimBoth
  1054. ^self trimBoth: '\s'
  1055. !
  1056. trimBoth: separators
  1057. ^(self trimLeft: separators) trimRight: separators
  1058. !
  1059. trimLeft
  1060. ^self trimLeft: '\s'
  1061. !
  1062. trimLeft: separators
  1063. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1064. !
  1065. trimRight
  1066. ^self trimRight: '\s'
  1067. !
  1068. trimRight: separators
  1069. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1070. ! !
  1071. !String methodsFor: 'split join'!
  1072. join: aCollection
  1073. ^ String
  1074. streamContents: [:stream | aCollection
  1075. do: [:each | stream nextPutAll: each asString]
  1076. separatedBy: [stream nextPutAll: self]]
  1077. !
  1078. lineIndicesDo: aBlock
  1079. "execute aBlock with 3 arguments for each line:
  1080. - start index of line
  1081. - end index of line without line delimiter
  1082. - end index of line including line delimiter(s) CR, LF or CRLF"
  1083. | cr lf start sz nextLF nextCR |
  1084. start := 1.
  1085. sz := self size.
  1086. cr := String cr.
  1087. nextCR := self indexOf: cr startingAt: 1.
  1088. lf := String lf.
  1089. nextLF := self indexOf: lf startingAt: 1.
  1090. [ start <= sz ] whileTrue: [
  1091. (nextLF = 0 and: [ nextCR = 0 ])
  1092. ifTrue: [ "No more CR, nor LF, the string is over"
  1093. aBlock value: start value: sz value: sz.
  1094. ^self ].
  1095. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1096. ifTrue: [ "Found a LF"
  1097. aBlock value: start value: nextLF - 1 value: nextLF.
  1098. start := 1 + nextLF.
  1099. nextLF := self indexOf: lf startingAt: start ]
  1100. ifFalse: [ 1 + nextCR = nextLF
  1101. ifTrue: [ "Found a CR-LF pair"
  1102. aBlock value: start value: nextCR - 1 value: nextLF.
  1103. start := 1 + nextLF.
  1104. nextCR := self indexOf: cr startingAt: start.
  1105. nextLF := self indexOf: lf startingAt: start ]
  1106. ifFalse: [ "Found a CR"
  1107. aBlock value: start value: nextCR - 1 value: nextCR.
  1108. start := 1 + nextCR.
  1109. nextCR := self indexOf: cr startingAt: start ]]]
  1110. !
  1111. lineNumber: anIndex
  1112. "Answer a string containing the characters in the given line number."
  1113. | lineCount |
  1114. lineCount := 0.
  1115. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1116. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1117. ^nil
  1118. !
  1119. lines
  1120. "Answer an array of lines composing this receiver without the line ending delimiters."
  1121. | lines |
  1122. lines := Array new.
  1123. self linesDo: [:aLine | lines add: aLine].
  1124. ^lines
  1125. !
  1126. linesDo: aBlock
  1127. "Execute aBlock with each line in this string. The terminating line
  1128. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1129. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1130. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1131. !
  1132. subStrings: aString
  1133. ^ self tokenize: aString
  1134. !
  1135. tokenize: aString
  1136. <return self.split(aString)>
  1137. ! !
  1138. !String methodsFor: 'testing'!
  1139. includesSubString: subString
  1140. < return self.indexOf(subString) !!= -1 >
  1141. !
  1142. isImmutable
  1143. ^ true
  1144. !
  1145. isString
  1146. ^true
  1147. !
  1148. isVowel
  1149. "Answer true if the receiver is a one character string containing a voyel"
  1150. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1151. ! !
  1152. !String class methodsFor: 'accessing'!
  1153. cr
  1154. <return '\r'>
  1155. !
  1156. crlf
  1157. <return '\r\n'>
  1158. !
  1159. esc
  1160. ^ self fromCharCode: 27
  1161. !
  1162. lf
  1163. <return '\n'>
  1164. !
  1165. space
  1166. <return ' '>
  1167. !
  1168. streamClass
  1169. ^StringStream
  1170. !
  1171. tab
  1172. <return '\t'>
  1173. ! !
  1174. !String class methodsFor: 'instance creation'!
  1175. fromCharCode: anInteger
  1176. <return String.fromCharCode(anInteger)>
  1177. !
  1178. fromString: aString
  1179. <return String(aString)>
  1180. !
  1181. value: aUTFCharCode
  1182. <return String.fromCharCode(aUTFCharCode);>
  1183. ! !
  1184. !String class methodsFor: 'random'!
  1185. random
  1186. "Returns random alphanumeric string beginning with letter"
  1187. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1188. !
  1189. randomNotIn: aString
  1190. | result |
  1191. [ result := self random. aString includesSubString: result ] whileTrue.
  1192. ^result
  1193. ! !
  1194. Collection subclass: #Set
  1195. instanceVariableNames: 'elements'
  1196. package: 'Kernel-Collections'!
  1197. !Set commentStamp!
  1198. I represent an unordered set of objects without duplicates.!
  1199. !Set methodsFor: 'accessing'!
  1200. size
  1201. ^elements size
  1202. ! !
  1203. !Set methodsFor: 'adding/removing'!
  1204. add: anObject
  1205. <
  1206. var found;
  1207. for(var i=0; i < self['@elements'].length; i++) {
  1208. if(_st(anObject).__eq(self['@elements'][i])) {
  1209. found = true;
  1210. break;
  1211. }
  1212. }
  1213. if(!!found) {self['@elements'].push(anObject)}
  1214. >
  1215. !
  1216. remove: anObject
  1217. elements remove: anObject
  1218. !
  1219. remove: anObject ifAbsent: aBlock
  1220. elements remove: anObject ifAbsent: aBlock
  1221. ! !
  1222. !Set methodsFor: 'comparing'!
  1223. = aCollection
  1224. self class = aCollection class ifFalse: [ ^ false ].
  1225. self size = aCollection size ifFalse: [ ^ false ].
  1226. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1227. ^ true
  1228. ! !
  1229. !Set methodsFor: 'converting'!
  1230. asArray
  1231. ^elements copy
  1232. ! !
  1233. !Set methodsFor: 'enumerating'!
  1234. collect: aBlock
  1235. ^self class withAll: (elements collect: aBlock)
  1236. !
  1237. detect: aBlock ifNone: anotherBlock
  1238. ^elements detect: aBlock ifNone: anotherBlock
  1239. !
  1240. do: aBlock
  1241. elements do: aBlock
  1242. !
  1243. select: aBlock
  1244. | collection |
  1245. collection := self class new.
  1246. self do: [:each |
  1247. (aBlock value: each) ifTrue: [
  1248. collection add: each]].
  1249. ^collection
  1250. ! !
  1251. !Set methodsFor: 'initialization'!
  1252. initialize
  1253. super initialize.
  1254. elements := #()
  1255. ! !
  1256. !Set methodsFor: 'printing'!
  1257. printOn: aStream
  1258. super printOn: aStream.
  1259. aStream nextPutAll: ' ('.
  1260. self
  1261. do: [ :each | each printOn: aStream ]
  1262. separatedBy: [ aStream nextPutAll: ' ' ].
  1263. aStream nextPutAll: ')'
  1264. ! !
  1265. !Set methodsFor: 'testing'!
  1266. includes: anObject
  1267. ^elements includes: anObject
  1268. ! !
  1269. Object subclass: #Queue
  1270. instanceVariableNames: 'read readIndex write'
  1271. package: 'Kernel-Collections'!
  1272. !Queue commentStamp!
  1273. I am a one-sided queue.
  1274. ## Usage
  1275. Use `#nextPut:` to add items to the queue.
  1276. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1277. ## Implementation notes
  1278. A Queue uses two OrderedCollections inside,
  1279. `read` is at the front, is not modified and only read using `readIndex`.
  1280. `write` is at the back and is appended new items.
  1281. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1282. As a consequence, no data moving is done by me, write appending may do data moving
  1283. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1284. !Queue methodsFor: 'accessing'!
  1285. next
  1286. ^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1287. !
  1288. nextIfAbsent: aBlock
  1289. | result |
  1290. result := read at: readIndex ifAbsent: [
  1291. write isEmpty ifTrue: [
  1292. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1293. ^aBlock value ].
  1294. read := write.
  1295. readIndex := 1.
  1296. write := OrderedCollection new.
  1297. read first ].
  1298. read at: readIndex put: nil.
  1299. readIndex := readIndex + 1.
  1300. ^result
  1301. !
  1302. nextPut: anObject
  1303. write add: anObject
  1304. ! !
  1305. !Queue methodsFor: 'initialization'!
  1306. initialize
  1307. super initialize.
  1308. read := OrderedCollection new.
  1309. write := OrderedCollection new.
  1310. readIndex := 1
  1311. ! !
  1312. Object subclass: #RegularExpression
  1313. instanceVariableNames: ''
  1314. package: 'Kernel-Collections'!
  1315. !RegularExpression commentStamp!
  1316. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1317. !RegularExpression methodsFor: 'evaluating'!
  1318. compile: aString
  1319. <return self.compile(aString)>
  1320. !
  1321. exec: aString
  1322. <return self.exec(aString) || nil>
  1323. !
  1324. test: aString
  1325. <return self.test(aString)>
  1326. ! !
  1327. !RegularExpression class methodsFor: 'instance creation'!
  1328. fromString: aString
  1329. ^self fromString: aString flag: ''
  1330. !
  1331. fromString: aString flag: anotherString
  1332. <return new RegExp(aString, anotherString)>
  1333. ! !
  1334. Object subclass: #Stream
  1335. instanceVariableNames: 'collection position streamSize'
  1336. package: 'Kernel-Collections'!
  1337. !Stream commentStamp!
  1338. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1339. My instances are read/write streams to the contents sequence collection.!
  1340. !Stream methodsFor: 'accessing'!
  1341. collection
  1342. ^collection
  1343. !
  1344. contents
  1345. ^self collection
  1346. copyFrom: 1
  1347. to: self streamSize
  1348. !
  1349. position
  1350. ^position ifNil: [position := 0]
  1351. !
  1352. position: anInteger
  1353. position := anInteger
  1354. !
  1355. setCollection: aCollection
  1356. collection := aCollection
  1357. !
  1358. setStreamSize: anInteger
  1359. streamSize := anInteger
  1360. !
  1361. size
  1362. ^self streamSize
  1363. !
  1364. streamSize
  1365. ^streamSize
  1366. ! !
  1367. !Stream methodsFor: 'actions'!
  1368. close
  1369. !
  1370. flush
  1371. !
  1372. reset
  1373. self position: 0
  1374. !
  1375. resetContents
  1376. self reset.
  1377. self setStreamSize: 0
  1378. ! !
  1379. !Stream methodsFor: 'enumerating'!
  1380. do: aBlock
  1381. [self atEnd] whileFalse: [aBlock value: self next]
  1382. ! !
  1383. !Stream methodsFor: 'positioning'!
  1384. setToEnd
  1385. self position: self size
  1386. !
  1387. skip: anInteger
  1388. self position: ((self position + anInteger) min: self size max: 0)
  1389. ! !
  1390. !Stream methodsFor: 'reading'!
  1391. next
  1392. ^self atEnd
  1393. ifTrue: [nil]
  1394. ifFalse: [
  1395. self position: self position + 1.
  1396. collection at: self position]
  1397. !
  1398. next: anInteger
  1399. | tempCollection |
  1400. tempCollection := self collection class new.
  1401. anInteger timesRepeat: [
  1402. self atEnd ifFalse: [
  1403. tempCollection add: self next]].
  1404. ^tempCollection
  1405. !
  1406. peek
  1407. ^self atEnd ifFalse: [
  1408. self collection at: self position + 1]
  1409. ! !
  1410. !Stream methodsFor: 'testing'!
  1411. atEnd
  1412. ^self position = self size
  1413. !
  1414. atStart
  1415. ^self position = 0
  1416. !
  1417. isEmpty
  1418. ^self size = 0
  1419. ! !
  1420. !Stream methodsFor: 'writing'!
  1421. << anObject
  1422. self write: anObject
  1423. !
  1424. nextPut: anObject
  1425. self position: self position + 1.
  1426. self collection at: self position put: anObject.
  1427. self setStreamSize: (self streamSize max: self position)
  1428. !
  1429. nextPutAll: aCollection
  1430. aCollection do: [:each |
  1431. self nextPut: each]
  1432. !
  1433. nextPutString: aString
  1434. self nextPut: aString
  1435. !
  1436. write: anObject
  1437. anObject putOn: self
  1438. ! !
  1439. !Stream class methodsFor: 'instance creation'!
  1440. on: aCollection
  1441. ^self new
  1442. setCollection: aCollection;
  1443. setStreamSize: aCollection size;
  1444. yourself
  1445. ! !
  1446. Stream subclass: #StringStream
  1447. instanceVariableNames: ''
  1448. package: 'Kernel-Collections'!
  1449. !StringStream commentStamp!
  1450. I am a Stream specific to `String` objects.!
  1451. !StringStream methodsFor: 'reading'!
  1452. next: anInteger
  1453. | tempCollection |
  1454. tempCollection := self collection class new.
  1455. anInteger timesRepeat: [
  1456. self atEnd ifFalse: [
  1457. tempCollection := tempCollection, self next]].
  1458. ^tempCollection
  1459. ! !
  1460. !StringStream methodsFor: 'writing'!
  1461. cr
  1462. ^self nextPutAll: String cr
  1463. !
  1464. crlf
  1465. ^self nextPutAll: String crlf
  1466. !
  1467. lf
  1468. ^self nextPutAll: String lf
  1469. !
  1470. nextPut: aString
  1471. self nextPutAll: aString
  1472. !
  1473. nextPutAll: aString
  1474. | pre post |
  1475. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1476. pre := self collection copyFrom: 1 to: self position.
  1477. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1478. self setCollection: pre, aString, post
  1479. ].
  1480. self position: self position + aString size.
  1481. self setStreamSize: (self streamSize max: self position)
  1482. !
  1483. nextPutString: aString
  1484. self nextPutAll: aString
  1485. !
  1486. space
  1487. self nextPut: ' '
  1488. !
  1489. tab
  1490. ^self nextPutAll: String tab
  1491. ! !