Kernel-Collections.st 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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. do: aBlock displayingProgress: aString
  691. ProgressHandler current
  692. do: aBlock on: self displaying: aString
  693. !
  694. with: anotherCollection do: aBlock
  695. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], anotherCollection[i]);}>
  696. !
  697. withIndexDo: aBlock
  698. <for(var i=0;i<self.length;i++){aBlock._value_value_(self[i], i+1);}>
  699. ! !
  700. !SequenceableCollection methodsFor: 'streaming'!
  701. newStream
  702. ^self streamClass on: self
  703. !
  704. readStream
  705. "For Pharo compatibility"
  706. ^self stream
  707. !
  708. stream
  709. ^self newStream
  710. !
  711. streamClass
  712. ^self class streamClass
  713. !
  714. writeStream
  715. "For Pharo compatibility"
  716. ^self stream
  717. ! !
  718. !SequenceableCollection methodsFor: 'testing'!
  719. includes: anObject
  720. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  721. ! !
  722. !SequenceableCollection class methodsFor: 'accessing'!
  723. streamClass
  724. ^Stream
  725. ! !
  726. !SequenceableCollection class methodsFor: 'streaming'!
  727. streamContents: aBlock
  728. | stream |
  729. stream := (self streamClass on: self new).
  730. aBlock value: stream.
  731. ^ stream contents
  732. ! !
  733. SequenceableCollection subclass: #Array
  734. instanceVariableNames: ''
  735. package: 'Kernel-Collections'!
  736. !Array commentStamp!
  737. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  738. I am directly mapped to JavaScript Number.
  739. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  740. !Array methodsFor: 'accessing'!
  741. at: anIndex ifAbsent: aBlock
  742. <
  743. if((anIndex < 1) || (self.length < anIndex)) {return aBlock._value()};
  744. return self[anIndex - 1];
  745. >
  746. !
  747. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  748. <return anIndex < 1 || self.length < anIndex ? anotherBlock._value() : aBlock._value_(self[anIndex - 1]);>
  749. !
  750. at: anIndex put: anObject
  751. <return self[anIndex - 1] = anObject>
  752. !
  753. size
  754. <return self.length>
  755. ! !
  756. !Array methodsFor: 'adding/removing'!
  757. add: anObject
  758. <self.push(anObject); return anObject;>
  759. !
  760. remove: anObject ifAbsent: aBlock
  761. <
  762. for(var i=0;i<self.length;i++) {
  763. if(_st(self[i]).__eq(anObject)) {
  764. self.splice(i,1);
  765. return self;
  766. }
  767. };
  768. aBlock._value();
  769. >
  770. !
  771. removeFrom: aNumber to: anotherNumber
  772. <self.splice(aNumber -1, anotherNumber - aNumber + 1)>
  773. !
  774. removeIndex: anInteger
  775. <self.splice(anInteger - 1, 1)>
  776. ! !
  777. !Array methodsFor: 'converting'!
  778. asJavascript
  779. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  780. !
  781. reversed
  782. <return self._copy().reverse()>
  783. ! !
  784. !Array methodsFor: 'enumerating'!
  785. collect: aBlock
  786. "Optimized version"
  787. <return self.map(function(each) {return aBlock._value_(each)})>
  788. !
  789. join: aString
  790. <return self.join(aString)>
  791. !
  792. select: aBlock
  793. "Optimized version"
  794. <
  795. var result = self.klass._new();
  796. for(var i=0; i<self.length; i++) {
  797. if(aBlock._value_(self[i])) {
  798. result.push(self[i]);
  799. }
  800. }
  801. return result;
  802. >
  803. !
  804. sort
  805. ^self basicPerform: 'sort'
  806. !
  807. sort: aBlock
  808. <
  809. return self.sort(function(a, b) {
  810. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  811. })
  812. >
  813. !
  814. sorted
  815. ^self copy sort
  816. !
  817. sorted: aBlock
  818. ^self copy sort: aBlock
  819. ! !
  820. !Array methodsFor: 'printing'!
  821. printOn: aStream
  822. super printOn: aStream.
  823. aStream nextPutAll: ' ('.
  824. self
  825. do: [ :each | each printOn: aStream ]
  826. separatedBy: [ aStream nextPutAll: ' ' ].
  827. aStream nextPutAll: ')'
  828. ! !
  829. !Array class methodsFor: 'instance creation'!
  830. new: anInteger
  831. <return new Array(anInteger)>
  832. !
  833. with: anObject
  834. ^(self new: 1)
  835. at: 1 put: anObject;
  836. yourself
  837. !
  838. with: anObject with: anObject2
  839. ^(self new: 2)
  840. at: 1 put: anObject;
  841. at: 2 put: anObject2;
  842. yourself
  843. !
  844. with: anObject with: anObject2 with: anObject3
  845. ^(self new: 3)
  846. at: 1 put: anObject;
  847. at: 2 put: anObject2;
  848. at: 3 put: anObject3;
  849. yourself
  850. !
  851. withAll: aCollection
  852. | instance index |
  853. index := 1.
  854. instance := self new: aCollection size.
  855. aCollection do: [:each |
  856. instance at: index put: each.
  857. index := index + 1].
  858. ^instance
  859. ! !
  860. SequenceableCollection subclass: #CharacterArray
  861. instanceVariableNames: ''
  862. package: 'Kernel-Collections'!
  863. !CharacterArray commentStamp!
  864. I am the abstract superclass of string-like collections.!
  865. !CharacterArray methodsFor: 'accessing'!
  866. at: anIndex put: anObject
  867. self errorReadOnly
  868. ! !
  869. !CharacterArray methodsFor: 'adding/removing'!
  870. add: anObject
  871. self errorReadOnly
  872. !
  873. remove: anObject
  874. self errorReadOnly
  875. ! !
  876. !CharacterArray methodsFor: 'converting'!
  877. asLowercase
  878. ^self class fromString: self asString asLowercase
  879. !
  880. asNumber
  881. ^self asString asNumber
  882. !
  883. asString
  884. ^self subclassResponsibility
  885. !
  886. asSymbol
  887. ^self asString
  888. !
  889. asUppercase
  890. ^self class fromString: self asString asUppercase
  891. ! !
  892. !CharacterArray methodsFor: 'copying'!
  893. , aString
  894. ^self asString, aString asString
  895. ! !
  896. !CharacterArray methodsFor: 'error handling'!
  897. errorReadOnly
  898. self error: 'Object is read-only'
  899. ! !
  900. !CharacterArray methodsFor: 'printing'!
  901. printOn: aStream
  902. self asString printOn: aStream
  903. ! !
  904. !CharacterArray methodsFor: 'streaming'!
  905. putOn: aStream
  906. aStream nextPutString: self
  907. ! !
  908. !CharacterArray class methodsFor: 'instance creation'!
  909. fromString: aString
  910. self subclassResponsibility
  911. ! !
  912. CharacterArray subclass: #String
  913. instanceVariableNames: ''
  914. package: 'Kernel-Collections'!
  915. !String commentStamp!
  916. 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.
  917. String inherits many useful methods from its hierarchy, such as
  918. `Collection >> #,`!
  919. !String methodsFor: 'accessing'!
  920. asciiValue
  921. <return self.charCodeAt(0);>
  922. !
  923. at: anIndex ifAbsent: aBlock
  924. <return String(self).charAt(anIndex - 1) || aBlock._value()>
  925. !
  926. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  927. <
  928. var result = String(self).charAt(anIndex - 1);
  929. return result ? aBlock._value_(result) : anotherBlock._value();
  930. >
  931. !
  932. charCodeAt: anInteger
  933. < return self.charCodeAt(anInteger - 1) >
  934. !
  935. escaped
  936. <return escape(self)>
  937. !
  938. size
  939. <return self.length>
  940. !
  941. unescaped
  942. <return unescape(self)>
  943. ! !
  944. !String methodsFor: 'comparing'!
  945. < aString
  946. <return String(self) < aString._asString()>
  947. !
  948. <= aString
  949. <return String(self) <= aString._asString()>
  950. !
  951. = aString
  952. <
  953. if(typeof aString === 'undefined') { return false }
  954. if(!!aString._isString || !! aString._isString()) {
  955. return false;
  956. }
  957. return String(self) === String(aString)
  958. >
  959. !
  960. == aString
  961. ^self = aString
  962. !
  963. > aString
  964. <return String(self) >> aString._asString()>
  965. !
  966. >= aString
  967. <return String(self) >>= aString._asString()>
  968. ! !
  969. !String methodsFor: 'converting'!
  970. asJSON
  971. ^self
  972. !
  973. asJavascript
  974. <
  975. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  976. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  977. else
  978. return "\"" + self + "\"";
  979. >
  980. !
  981. asLowercase
  982. <return self.toLowerCase()>
  983. !
  984. asNumber
  985. <return Number(self)>
  986. !
  987. asRegexp
  988. ^ RegularExpression fromString: self
  989. !
  990. asSelector
  991. <return smalltalk.selector(self)>
  992. !
  993. asString
  994. ^self
  995. !
  996. asSymbol
  997. ^ self
  998. !
  999. asUppercase
  1000. <return self.toUpperCase()>
  1001. !
  1002. crlfSanitized
  1003. ^self lines join: String lf
  1004. !
  1005. reversed
  1006. <return self.split("").reverse().join("")>
  1007. ! !
  1008. !String methodsFor: 'copying'!
  1009. , aString
  1010. <return self + aString>
  1011. !
  1012. copyFrom: anIndex to: anotherIndex
  1013. <return self.substring(anIndex - 1, anotherIndex)>
  1014. !
  1015. deepCopy
  1016. ^self shallowCopy
  1017. !
  1018. shallowCopy
  1019. ^self class fromString: self
  1020. ! !
  1021. !String methodsFor: 'enumerating'!
  1022. do: aBlock
  1023. <for(var i=0;i<self.length;i++){aBlock._value_(self.charAt(i));}>
  1024. !
  1025. withIndexDo: aBlock
  1026. <for(var i=0;i<self.length;i++){aBlock._value_value_(self.charAt(i), i+1);}>
  1027. ! !
  1028. !String methodsFor: 'printing'!
  1029. printNl
  1030. <console.log(self)>
  1031. !
  1032. printOn: aStream
  1033. aStream
  1034. nextPutAll: '''';
  1035. nextPutAll: self;
  1036. nextPutAll: ''''
  1037. ! !
  1038. !String methodsFor: 'regular expressions'!
  1039. match: aRegexp
  1040. <return self.search(aRegexp) !!= -1>
  1041. !
  1042. matchesOf: aRegularExpression
  1043. <return self.match(aRegularExpression)>
  1044. !
  1045. replace: aString with: anotherString
  1046. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1047. !
  1048. replaceRegexp: aRegexp with: aString
  1049. <return self.replace(aRegexp, aString)>
  1050. !
  1051. trimBoth
  1052. ^self trimBoth: '\s'
  1053. !
  1054. trimBoth: separators
  1055. ^(self trimLeft: separators) trimRight: separators
  1056. !
  1057. trimLeft
  1058. ^self trimLeft: '\s'
  1059. !
  1060. trimLeft: separators
  1061. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1062. !
  1063. trimRight
  1064. ^self trimRight: '\s'
  1065. !
  1066. trimRight: separators
  1067. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1068. ! !
  1069. !String methodsFor: 'split join'!
  1070. join: aCollection
  1071. ^ String
  1072. streamContents: [:stream | aCollection
  1073. do: [:each | stream nextPutAll: each asString]
  1074. separatedBy: [stream nextPutAll: self]]
  1075. !
  1076. lineIndicesDo: aBlock
  1077. "execute aBlock with 3 arguments for each line:
  1078. - start index of line
  1079. - end index of line without line delimiter
  1080. - end index of line including line delimiter(s) CR, LF or CRLF"
  1081. | cr lf start sz nextLF nextCR |
  1082. start := 1.
  1083. sz := self size.
  1084. cr := String cr.
  1085. nextCR := self indexOf: cr startingAt: 1.
  1086. lf := String lf.
  1087. nextLF := self indexOf: lf startingAt: 1.
  1088. [ start <= sz ] whileTrue: [
  1089. (nextLF = 0 and: [ nextCR = 0 ])
  1090. ifTrue: [ "No more CR, nor LF, the string is over"
  1091. aBlock value: start value: sz value: sz.
  1092. ^self ].
  1093. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1094. ifTrue: [ "Found a LF"
  1095. aBlock value: start value: nextLF - 1 value: nextLF.
  1096. start := 1 + nextLF.
  1097. nextLF := self indexOf: lf startingAt: start ]
  1098. ifFalse: [ 1 + nextCR = nextLF
  1099. ifTrue: [ "Found a CR-LF pair"
  1100. aBlock value: start value: nextCR - 1 value: nextLF.
  1101. start := 1 + nextLF.
  1102. nextCR := self indexOf: cr startingAt: start.
  1103. nextLF := self indexOf: lf startingAt: start ]
  1104. ifFalse: [ "Found a CR"
  1105. aBlock value: start value: nextCR - 1 value: nextCR.
  1106. start := 1 + nextCR.
  1107. nextCR := self indexOf: cr startingAt: start ]]]
  1108. !
  1109. lineNumber: anIndex
  1110. "Answer a string containing the characters in the given line number."
  1111. | lineCount |
  1112. lineCount := 0.
  1113. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1114. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1115. ^nil
  1116. !
  1117. lines
  1118. "Answer an array of lines composing this receiver without the line ending delimiters."
  1119. | lines |
  1120. lines := Array new.
  1121. self linesDo: [:aLine | lines add: aLine].
  1122. ^lines
  1123. !
  1124. linesDo: aBlock
  1125. "Execute aBlock with each line in this string. The terminating line
  1126. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1127. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1128. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1129. !
  1130. subStrings: aString
  1131. ^ self tokenize: aString
  1132. !
  1133. tokenize: aString
  1134. <return self.split(aString)>
  1135. ! !
  1136. !String methodsFor: 'testing'!
  1137. includesSubString: subString
  1138. < return self.indexOf(subString) !!= -1 >
  1139. !
  1140. isImmutable
  1141. ^ true
  1142. !
  1143. isString
  1144. ^true
  1145. !
  1146. isVowel
  1147. "Answer true if the receiver is a one character string containing a voyel"
  1148. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1149. ! !
  1150. !String class methodsFor: 'accessing'!
  1151. cr
  1152. <return '\r'>
  1153. !
  1154. crlf
  1155. <return '\r\n'>
  1156. !
  1157. esc
  1158. ^ self fromCharCode: 27
  1159. !
  1160. lf
  1161. <return '\n'>
  1162. !
  1163. space
  1164. <return ' '>
  1165. !
  1166. streamClass
  1167. ^StringStream
  1168. !
  1169. tab
  1170. <return '\t'>
  1171. ! !
  1172. !String class methodsFor: 'instance creation'!
  1173. fromCharCode: anInteger
  1174. <return String.fromCharCode(anInteger)>
  1175. !
  1176. fromString: aString
  1177. <return String(aString)>
  1178. !
  1179. value: aUTFCharCode
  1180. <return String.fromCharCode(aUTFCharCode);>
  1181. ! !
  1182. !String class methodsFor: 'random'!
  1183. random
  1184. "Returns random alphanumeric string beginning with letter"
  1185. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1186. !
  1187. randomNotIn: aString
  1188. | result |
  1189. [ result := self random. aString includesSubString: result ] whileTrue.
  1190. ^result
  1191. ! !
  1192. Collection subclass: #Set
  1193. instanceVariableNames: 'elements'
  1194. package: 'Kernel-Collections'!
  1195. !Set commentStamp!
  1196. I represent an unordered set of objects without duplicates.!
  1197. !Set methodsFor: 'accessing'!
  1198. size
  1199. ^elements size
  1200. ! !
  1201. !Set methodsFor: 'adding/removing'!
  1202. add: anObject
  1203. <
  1204. var found;
  1205. for(var i=0; i < self['@elements'].length; i++) {
  1206. if(_st(anObject).__eq(self['@elements'][i])) {
  1207. found = true;
  1208. break;
  1209. }
  1210. }
  1211. if(!!found) {self['@elements'].push(anObject)}
  1212. >
  1213. !
  1214. remove: anObject
  1215. elements remove: anObject
  1216. !
  1217. remove: anObject ifAbsent: aBlock
  1218. elements remove: anObject ifAbsent: aBlock
  1219. ! !
  1220. !Set methodsFor: 'comparing'!
  1221. = aCollection
  1222. self class = aCollection class ifFalse: [ ^ false ].
  1223. self size = aCollection size ifFalse: [ ^ false ].
  1224. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1225. ^ true
  1226. ! !
  1227. !Set methodsFor: 'converting'!
  1228. asArray
  1229. ^elements copy
  1230. ! !
  1231. !Set methodsFor: 'enumerating'!
  1232. collect: aBlock
  1233. ^self class withAll: (elements collect: aBlock)
  1234. !
  1235. detect: aBlock ifNone: anotherBlock
  1236. ^elements detect: aBlock ifNone: anotherBlock
  1237. !
  1238. do: aBlock
  1239. elements do: aBlock
  1240. !
  1241. select: aBlock
  1242. | collection |
  1243. collection := self class new.
  1244. self do: [:each |
  1245. (aBlock value: each) ifTrue: [
  1246. collection add: each]].
  1247. ^collection
  1248. ! !
  1249. !Set methodsFor: 'initialization'!
  1250. initialize
  1251. super initialize.
  1252. elements := #()
  1253. ! !
  1254. !Set methodsFor: 'printing'!
  1255. printOn: aStream
  1256. super printOn: aStream.
  1257. aStream nextPutAll: ' ('.
  1258. self
  1259. do: [ :each | each printOn: aStream ]
  1260. separatedBy: [ aStream nextPutAll: ' ' ].
  1261. aStream nextPutAll: ')'
  1262. ! !
  1263. !Set methodsFor: 'testing'!
  1264. includes: anObject
  1265. ^elements includes: anObject
  1266. ! !
  1267. Object subclass: #Queue
  1268. instanceVariableNames: 'read readIndex write'
  1269. package: 'Kernel-Collections'!
  1270. !Queue commentStamp!
  1271. I am a one-sided queue.
  1272. ## Usage
  1273. Use `#nextPut:` to add items to the queue.
  1274. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1275. ## Implementation notes
  1276. A Queue uses two OrderedCollections inside,
  1277. `read` is at the front, is not modified and only read using `readIndex`.
  1278. `write` is at the back and is appended new items.
  1279. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1280. As a consequence, no data moving is done by me, write appending may do data moving
  1281. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1282. !Queue methodsFor: 'accessing'!
  1283. next
  1284. ^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1285. !
  1286. nextIfAbsent: aBlock
  1287. | result |
  1288. result := read at: readIndex ifAbsent: [
  1289. write isEmpty ifTrue: [
  1290. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1291. ^aBlock value ].
  1292. read := write.
  1293. readIndex := 1.
  1294. write := OrderedCollection new.
  1295. read first ].
  1296. read at: readIndex put: nil.
  1297. readIndex := readIndex + 1.
  1298. ^result
  1299. !
  1300. nextPut: anObject
  1301. write add: anObject
  1302. ! !
  1303. !Queue methodsFor: 'initialization'!
  1304. initialize
  1305. super initialize.
  1306. read := OrderedCollection new.
  1307. write := OrderedCollection new.
  1308. readIndex := 1
  1309. ! !
  1310. Object subclass: #RegularExpression
  1311. instanceVariableNames: ''
  1312. package: 'Kernel-Collections'!
  1313. !RegularExpression commentStamp!
  1314. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1315. !RegularExpression methodsFor: 'evaluating'!
  1316. compile: aString
  1317. <return self.compile(aString)>
  1318. !
  1319. exec: aString
  1320. <return self.exec(aString) || nil>
  1321. !
  1322. test: aString
  1323. <return self.test(aString)>
  1324. ! !
  1325. !RegularExpression class methodsFor: 'instance creation'!
  1326. fromString: aString
  1327. ^self fromString: aString flag: ''
  1328. !
  1329. fromString: aString flag: anotherString
  1330. <return new RegExp(aString, anotherString)>
  1331. ! !
  1332. Object subclass: #Stream
  1333. instanceVariableNames: 'collection position streamSize'
  1334. package: 'Kernel-Collections'!
  1335. !Stream commentStamp!
  1336. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1337. My instances are read/write streams to the contents sequence collection.!
  1338. !Stream methodsFor: 'accessing'!
  1339. collection
  1340. ^collection
  1341. !
  1342. contents
  1343. ^self collection
  1344. copyFrom: 1
  1345. to: self streamSize
  1346. !
  1347. position
  1348. ^position ifNil: [position := 0]
  1349. !
  1350. position: anInteger
  1351. position := anInteger
  1352. !
  1353. setCollection: aCollection
  1354. collection := aCollection
  1355. !
  1356. setStreamSize: anInteger
  1357. streamSize := anInteger
  1358. !
  1359. size
  1360. ^self streamSize
  1361. !
  1362. streamSize
  1363. ^streamSize
  1364. ! !
  1365. !Stream methodsFor: 'actions'!
  1366. close
  1367. !
  1368. flush
  1369. !
  1370. reset
  1371. self position: 0
  1372. !
  1373. resetContents
  1374. self reset.
  1375. self setStreamSize: 0
  1376. ! !
  1377. !Stream methodsFor: 'enumerating'!
  1378. do: aBlock
  1379. [self atEnd] whileFalse: [aBlock value: self next]
  1380. ! !
  1381. !Stream methodsFor: 'positioning'!
  1382. setToEnd
  1383. self position: self size
  1384. !
  1385. skip: anInteger
  1386. self position: ((self position + anInteger) min: self size max: 0)
  1387. ! !
  1388. !Stream methodsFor: 'reading'!
  1389. next
  1390. ^self atEnd
  1391. ifTrue: [nil]
  1392. ifFalse: [
  1393. self position: self position + 1.
  1394. collection at: self position]
  1395. !
  1396. next: anInteger
  1397. | tempCollection |
  1398. tempCollection := self collection class new.
  1399. anInteger timesRepeat: [
  1400. self atEnd ifFalse: [
  1401. tempCollection add: self next]].
  1402. ^tempCollection
  1403. !
  1404. peek
  1405. ^self atEnd ifFalse: [
  1406. self collection at: self position + 1]
  1407. ! !
  1408. !Stream methodsFor: 'testing'!
  1409. atEnd
  1410. ^self position = self size
  1411. !
  1412. atStart
  1413. ^self position = 0
  1414. !
  1415. isEmpty
  1416. ^self size = 0
  1417. ! !
  1418. !Stream methodsFor: 'writing'!
  1419. << anObject
  1420. self write: anObject
  1421. !
  1422. nextPut: anObject
  1423. self position: self position + 1.
  1424. self collection at: self position put: anObject.
  1425. self setStreamSize: (self streamSize max: self position)
  1426. !
  1427. nextPutAll: aCollection
  1428. aCollection do: [:each |
  1429. self nextPut: each]
  1430. !
  1431. nextPutString: aString
  1432. self nextPut: aString
  1433. !
  1434. write: anObject
  1435. anObject putOn: self
  1436. ! !
  1437. !Stream class methodsFor: 'instance creation'!
  1438. on: aCollection
  1439. ^self new
  1440. setCollection: aCollection;
  1441. setStreamSize: aCollection size;
  1442. yourself
  1443. ! !
  1444. Stream subclass: #StringStream
  1445. instanceVariableNames: ''
  1446. package: 'Kernel-Collections'!
  1447. !StringStream commentStamp!
  1448. I am a Stream specific to `String` objects.!
  1449. !StringStream methodsFor: 'reading'!
  1450. next: anInteger
  1451. | tempCollection |
  1452. tempCollection := self collection class new.
  1453. anInteger timesRepeat: [
  1454. self atEnd ifFalse: [
  1455. tempCollection := tempCollection, self next]].
  1456. ^tempCollection
  1457. ! !
  1458. !StringStream methodsFor: 'writing'!
  1459. cr
  1460. ^self nextPutAll: String cr
  1461. !
  1462. crlf
  1463. ^self nextPutAll: String crlf
  1464. !
  1465. lf
  1466. ^self nextPutAll: String lf
  1467. !
  1468. nextPut: aString
  1469. self nextPutAll: aString
  1470. !
  1471. nextPutAll: aString
  1472. | pre post |
  1473. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1474. pre := self collection copyFrom: 1 to: self position.
  1475. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1476. self setCollection: pre, aString, post
  1477. ].
  1478. self position: self position + aString size.
  1479. self setStreamSize: (self streamSize max: self position)
  1480. !
  1481. nextPutString: aString
  1482. self nextPutAll: aString
  1483. !
  1484. space
  1485. self nextPut: ' '
  1486. !
  1487. tab
  1488. ^self nextPutAll: String tab
  1489. ! !