Kernel-Collections.st 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. asMutator
  985. "Answer a setter selector. For example,
  986. #name asMutator returns #name:"
  987. self last = ':' ifFalse: [ ^ self, ':' ].
  988. ^ self
  989. !
  990. asNumber
  991. <return Number(self)>
  992. !
  993. asRegexp
  994. ^ RegularExpression fromString: self
  995. !
  996. asSelector
  997. <return smalltalk.selector(self)>
  998. !
  999. asString
  1000. ^self
  1001. !
  1002. asSymbol
  1003. ^ self
  1004. !
  1005. asUppercase
  1006. <return self.toUpperCase()>
  1007. !
  1008. crlfSanitized
  1009. ^self lines join: String lf
  1010. !
  1011. reversed
  1012. <return self.split("").reverse().join("")>
  1013. ! !
  1014. !String methodsFor: 'copying'!
  1015. , aString
  1016. <return self + aString>
  1017. !
  1018. copyFrom: anIndex to: anotherIndex
  1019. <return self.substring(anIndex - 1, anotherIndex)>
  1020. !
  1021. deepCopy
  1022. ^self shallowCopy
  1023. !
  1024. shallowCopy
  1025. ^self class fromString: self
  1026. ! !
  1027. !String methodsFor: 'enumerating'!
  1028. do: aBlock
  1029. <for(var i=0;i<self.length;i++){aBlock._value_(self.charAt(i));}>
  1030. !
  1031. withIndexDo: aBlock
  1032. <for(var i=0;i<self.length;i++){aBlock._value_value_(self.charAt(i), i+1);}>
  1033. ! !
  1034. !String methodsFor: 'printing'!
  1035. printNl
  1036. <console.log(self)>
  1037. !
  1038. printOn: aStream
  1039. aStream
  1040. nextPutAll: '''';
  1041. nextPutAll: self;
  1042. nextPutAll: ''''
  1043. ! !
  1044. !String methodsFor: 'regular expressions'!
  1045. match: aRegexp
  1046. <return self.search(aRegexp) !!= -1>
  1047. !
  1048. matchesOf: aRegularExpression
  1049. <return self.match(aRegularExpression)>
  1050. !
  1051. replace: aString with: anotherString
  1052. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1053. !
  1054. replaceRegexp: aRegexp with: aString
  1055. <return self.replace(aRegexp, aString)>
  1056. !
  1057. trimBoth
  1058. ^self trimBoth: '\s'
  1059. !
  1060. trimBoth: separators
  1061. ^(self trimLeft: separators) trimRight: separators
  1062. !
  1063. trimLeft
  1064. ^self trimLeft: '\s'
  1065. !
  1066. trimLeft: separators
  1067. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1068. !
  1069. trimRight
  1070. ^self trimRight: '\s'
  1071. !
  1072. trimRight: separators
  1073. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1074. ! !
  1075. !String methodsFor: 'split join'!
  1076. join: aCollection
  1077. ^ String
  1078. streamContents: [:stream | aCollection
  1079. do: [:each | stream nextPutAll: each asString]
  1080. separatedBy: [stream nextPutAll: self]]
  1081. !
  1082. lineIndicesDo: aBlock
  1083. "execute aBlock with 3 arguments for each line:
  1084. - start index of line
  1085. - end index of line without line delimiter
  1086. - end index of line including line delimiter(s) CR, LF or CRLF"
  1087. | cr lf start sz nextLF nextCR |
  1088. start := 1.
  1089. sz := self size.
  1090. cr := String cr.
  1091. nextCR := self indexOf: cr startingAt: 1.
  1092. lf := String lf.
  1093. nextLF := self indexOf: lf startingAt: 1.
  1094. [ start <= sz ] whileTrue: [
  1095. (nextLF = 0 and: [ nextCR = 0 ])
  1096. ifTrue: [ "No more CR, nor LF, the string is over"
  1097. aBlock value: start value: sz value: sz.
  1098. ^self ].
  1099. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1100. ifTrue: [ "Found a LF"
  1101. aBlock value: start value: nextLF - 1 value: nextLF.
  1102. start := 1 + nextLF.
  1103. nextLF := self indexOf: lf startingAt: start ]
  1104. ifFalse: [ 1 + nextCR = nextLF
  1105. ifTrue: [ "Found a CR-LF pair"
  1106. aBlock value: start value: nextCR - 1 value: nextLF.
  1107. start := 1 + nextLF.
  1108. nextCR := self indexOf: cr startingAt: start.
  1109. nextLF := self indexOf: lf startingAt: start ]
  1110. ifFalse: [ "Found a CR"
  1111. aBlock value: start value: nextCR - 1 value: nextCR.
  1112. start := 1 + nextCR.
  1113. nextCR := self indexOf: cr startingAt: start ]]]
  1114. !
  1115. lineNumber: anIndex
  1116. "Answer a string containing the characters in the given line number."
  1117. | lineCount |
  1118. lineCount := 0.
  1119. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1120. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1121. ^nil
  1122. !
  1123. lines
  1124. "Answer an array of lines composing this receiver without the line ending delimiters."
  1125. | lines |
  1126. lines := Array new.
  1127. self linesDo: [:aLine | lines add: aLine].
  1128. ^lines
  1129. !
  1130. linesDo: aBlock
  1131. "Execute aBlock with each line in this string. The terminating line
  1132. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1133. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1134. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1135. !
  1136. subStrings: aString
  1137. ^ self tokenize: aString
  1138. !
  1139. tokenize: aString
  1140. <return self.split(aString)>
  1141. ! !
  1142. !String methodsFor: 'testing'!
  1143. includesSubString: subString
  1144. < return self.indexOf(subString) !!= -1 >
  1145. !
  1146. isImmutable
  1147. ^ true
  1148. !
  1149. isString
  1150. ^true
  1151. !
  1152. isVowel
  1153. "Answer true if the receiver is a one character string containing a voyel"
  1154. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1155. ! !
  1156. !String class methodsFor: 'accessing'!
  1157. cr
  1158. <return '\r'>
  1159. !
  1160. crlf
  1161. <return '\r\n'>
  1162. !
  1163. esc
  1164. ^ self fromCharCode: 27
  1165. !
  1166. lf
  1167. <return '\n'>
  1168. !
  1169. space
  1170. <return ' '>
  1171. !
  1172. streamClass
  1173. ^StringStream
  1174. !
  1175. tab
  1176. <return '\t'>
  1177. ! !
  1178. !String class methodsFor: 'instance creation'!
  1179. fromCharCode: anInteger
  1180. <return String.fromCharCode(anInteger)>
  1181. !
  1182. fromString: aString
  1183. <return String(aString)>
  1184. !
  1185. value: aUTFCharCode
  1186. <return String.fromCharCode(aUTFCharCode);>
  1187. ! !
  1188. !String class methodsFor: 'random'!
  1189. random
  1190. "Returns random alphanumeric string beginning with letter"
  1191. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1192. !
  1193. randomNotIn: aString
  1194. | result |
  1195. [ result := self random. aString includesSubString: result ] whileTrue.
  1196. ^result
  1197. ! !
  1198. Collection subclass: #Set
  1199. instanceVariableNames: 'elements'
  1200. package: 'Kernel-Collections'!
  1201. !Set commentStamp!
  1202. I represent an unordered set of objects without duplicates.!
  1203. !Set methodsFor: 'accessing'!
  1204. size
  1205. ^elements size
  1206. ! !
  1207. !Set methodsFor: 'adding/removing'!
  1208. add: anObject
  1209. <
  1210. var found;
  1211. for(var i=0; i < self['@elements'].length; i++) {
  1212. if(_st(anObject).__eq(self['@elements'][i])) {
  1213. found = true;
  1214. break;
  1215. }
  1216. }
  1217. if(!!found) {self['@elements'].push(anObject)}
  1218. >
  1219. !
  1220. remove: anObject
  1221. elements remove: anObject
  1222. !
  1223. remove: anObject ifAbsent: aBlock
  1224. elements remove: anObject ifAbsent: aBlock
  1225. ! !
  1226. !Set methodsFor: 'comparing'!
  1227. = aCollection
  1228. self class = aCollection class ifFalse: [ ^ false ].
  1229. self size = aCollection size ifFalse: [ ^ false ].
  1230. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1231. ^ true
  1232. ! !
  1233. !Set methodsFor: 'converting'!
  1234. asArray
  1235. ^elements copy
  1236. ! !
  1237. !Set methodsFor: 'enumerating'!
  1238. collect: aBlock
  1239. ^self class withAll: (elements collect: aBlock)
  1240. !
  1241. detect: aBlock ifNone: anotherBlock
  1242. ^elements detect: aBlock ifNone: anotherBlock
  1243. !
  1244. do: aBlock
  1245. elements do: aBlock
  1246. !
  1247. select: aBlock
  1248. | collection |
  1249. collection := self class new.
  1250. self do: [:each |
  1251. (aBlock value: each) ifTrue: [
  1252. collection add: each]].
  1253. ^collection
  1254. ! !
  1255. !Set methodsFor: 'initialization'!
  1256. initialize
  1257. super initialize.
  1258. elements := #()
  1259. ! !
  1260. !Set methodsFor: 'printing'!
  1261. printOn: aStream
  1262. super printOn: aStream.
  1263. aStream nextPutAll: ' ('.
  1264. self
  1265. do: [ :each | each printOn: aStream ]
  1266. separatedBy: [ aStream nextPutAll: ' ' ].
  1267. aStream nextPutAll: ')'
  1268. ! !
  1269. !Set methodsFor: 'testing'!
  1270. includes: anObject
  1271. ^elements includes: anObject
  1272. ! !
  1273. Object subclass: #Queue
  1274. instanceVariableNames: 'read readIndex write'
  1275. package: 'Kernel-Collections'!
  1276. !Queue commentStamp!
  1277. I am a one-sided queue.
  1278. ## Usage
  1279. Use `#nextPut:` to add items to the queue.
  1280. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1281. ## Implementation notes
  1282. A Queue uses two OrderedCollections inside,
  1283. `read` is at the front, is not modified and only read using `readIndex`.
  1284. `write` is at the back and is appended new items.
  1285. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1286. As a consequence, no data moving is done by me, write appending may do data moving
  1287. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1288. !Queue methodsFor: 'accessing'!
  1289. next
  1290. ^self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1291. !
  1292. nextIfAbsent: aBlock
  1293. | result |
  1294. result := read at: readIndex ifAbsent: [
  1295. write isEmpty ifTrue: [
  1296. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1297. ^aBlock value ].
  1298. read := write.
  1299. readIndex := 1.
  1300. write := OrderedCollection new.
  1301. read first ].
  1302. read at: readIndex put: nil.
  1303. readIndex := readIndex + 1.
  1304. ^result
  1305. !
  1306. nextPut: anObject
  1307. write add: anObject
  1308. ! !
  1309. !Queue methodsFor: 'initialization'!
  1310. initialize
  1311. super initialize.
  1312. read := OrderedCollection new.
  1313. write := OrderedCollection new.
  1314. readIndex := 1
  1315. ! !
  1316. Object subclass: #RegularExpression
  1317. instanceVariableNames: ''
  1318. package: 'Kernel-Collections'!
  1319. !RegularExpression commentStamp!
  1320. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1321. !RegularExpression methodsFor: 'evaluating'!
  1322. compile: aString
  1323. <return self.compile(aString)>
  1324. !
  1325. exec: aString
  1326. <return self.exec(aString) || nil>
  1327. !
  1328. test: aString
  1329. <return self.test(aString)>
  1330. ! !
  1331. !RegularExpression class methodsFor: 'instance creation'!
  1332. fromString: aString
  1333. ^self fromString: aString flag: ''
  1334. !
  1335. fromString: aString flag: anotherString
  1336. <return new RegExp(aString, anotherString)>
  1337. ! !
  1338. Object subclass: #Stream
  1339. instanceVariableNames: 'collection position streamSize'
  1340. package: 'Kernel-Collections'!
  1341. !Stream commentStamp!
  1342. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1343. My instances are read/write streams to the contents sequence collection.!
  1344. !Stream methodsFor: 'accessing'!
  1345. collection
  1346. ^collection
  1347. !
  1348. contents
  1349. ^self collection
  1350. copyFrom: 1
  1351. to: self streamSize
  1352. !
  1353. position
  1354. ^position ifNil: [position := 0]
  1355. !
  1356. position: anInteger
  1357. position := anInteger
  1358. !
  1359. setCollection: aCollection
  1360. collection := aCollection
  1361. !
  1362. setStreamSize: anInteger
  1363. streamSize := anInteger
  1364. !
  1365. size
  1366. ^self streamSize
  1367. !
  1368. streamSize
  1369. ^streamSize
  1370. ! !
  1371. !Stream methodsFor: 'actions'!
  1372. close
  1373. !
  1374. flush
  1375. !
  1376. reset
  1377. self position: 0
  1378. !
  1379. resetContents
  1380. self reset.
  1381. self setStreamSize: 0
  1382. ! !
  1383. !Stream methodsFor: 'enumerating'!
  1384. do: aBlock
  1385. [self atEnd] whileFalse: [aBlock value: self next]
  1386. ! !
  1387. !Stream methodsFor: 'positioning'!
  1388. setToEnd
  1389. self position: self size
  1390. !
  1391. skip: anInteger
  1392. self position: ((self position + anInteger) min: self size max: 0)
  1393. ! !
  1394. !Stream methodsFor: 'reading'!
  1395. next
  1396. ^self atEnd
  1397. ifTrue: [nil]
  1398. ifFalse: [
  1399. self position: self position + 1.
  1400. collection at: self position]
  1401. !
  1402. next: anInteger
  1403. | tempCollection |
  1404. tempCollection := self collection class new.
  1405. anInteger timesRepeat: [
  1406. self atEnd ifFalse: [
  1407. tempCollection add: self next]].
  1408. ^tempCollection
  1409. !
  1410. peek
  1411. ^self atEnd ifFalse: [
  1412. self collection at: self position + 1]
  1413. ! !
  1414. !Stream methodsFor: 'testing'!
  1415. atEnd
  1416. ^self position = self size
  1417. !
  1418. atStart
  1419. ^self position = 0
  1420. !
  1421. isEmpty
  1422. ^self size = 0
  1423. ! !
  1424. !Stream methodsFor: 'writing'!
  1425. << anObject
  1426. self write: anObject
  1427. !
  1428. nextPut: anObject
  1429. self position: self position + 1.
  1430. self collection at: self position put: anObject.
  1431. self setStreamSize: (self streamSize max: self position)
  1432. !
  1433. nextPutAll: aCollection
  1434. aCollection do: [:each |
  1435. self nextPut: each]
  1436. !
  1437. nextPutString: aString
  1438. self nextPut: aString
  1439. !
  1440. write: anObject
  1441. anObject putOn: self
  1442. ! !
  1443. !Stream class methodsFor: 'instance creation'!
  1444. on: aCollection
  1445. ^self new
  1446. setCollection: aCollection;
  1447. setStreamSize: aCollection size;
  1448. yourself
  1449. ! !
  1450. Stream subclass: #StringStream
  1451. instanceVariableNames: ''
  1452. package: 'Kernel-Collections'!
  1453. !StringStream commentStamp!
  1454. I am a Stream specific to `String` objects.!
  1455. !StringStream methodsFor: 'reading'!
  1456. next: anInteger
  1457. | tempCollection |
  1458. tempCollection := self collection class new.
  1459. anInteger timesRepeat: [
  1460. self atEnd ifFalse: [
  1461. tempCollection := tempCollection, self next]].
  1462. ^tempCollection
  1463. ! !
  1464. !StringStream methodsFor: 'writing'!
  1465. cr
  1466. ^self nextPutAll: String cr
  1467. !
  1468. crlf
  1469. ^self nextPutAll: String crlf
  1470. !
  1471. lf
  1472. ^self nextPutAll: String lf
  1473. !
  1474. nextPut: aString
  1475. self nextPutAll: aString
  1476. !
  1477. nextPutAll: aString
  1478. | pre post |
  1479. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1480. pre := self collection copyFrom: 1 to: self position.
  1481. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1482. self setCollection: pre, aString, post
  1483. ].
  1484. self position: self position + aString size.
  1485. self setStreamSize: (self streamSize max: self position)
  1486. !
  1487. nextPutString: aString
  1488. self nextPutAll: aString
  1489. !
  1490. space
  1491. self nextPut: ' '
  1492. !
  1493. tab
  1494. ^self nextPutAll: String tab
  1495. ! !