Kernel-Collections.st 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. Smalltalk 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: #BucketStore
  41. instanceVariableNames: 'buckets hashBlock'
  42. package: 'Kernel-Collections'!
  43. !BucketStore commentStamp!
  44. I am an helper class for hash-based stores.
  45. I hold buckets which are selected by a hash, specified using `#hashBlock:`.
  46. The hash can be any object, and
  47. it is used as a JS property (that is, in ES5
  48. its toString() value counts).
  49. ## API
  50. I maintain a list of buckets. Client code can use this API:
  51. - `#bucketOfElement:` (to ask a bucket for element, I can return JS null if n/a)
  52. - `#do:` (to enumerate all elements of all buckets)
  53. - `#removeAll` (to remove all buckets)
  54. Client code itself should add/remove elements
  55. in a bucket. The `nil` object should not be put into any bucket.
  56. Types of buckets are the responsibility of subclasses via `#newBucket`.!
  57. !BucketStore methodsFor: 'accessing'!
  58. bucketOfElement: anObject
  59. <inlineJS: '
  60. var hash = self[''@hashBlock''](anObject);
  61. if (!!hash) return null;
  62. var buckets = self[''@buckets''],
  63. bucket = buckets[hash];
  64. if (!!bucket) { bucket = buckets[hash] = self._newBucket(); }
  65. return bucket;
  66. '>
  67. !
  68. hashBlock: aBlock
  69. hashBlock := aBlock
  70. ! !
  71. !BucketStore methodsFor: 'adding/removing'!
  72. removeAll
  73. <inlineJS: 'self[''@buckets''] = Object.create(null);'>
  74. ! !
  75. !BucketStore methodsFor: 'enumerating'!
  76. do: aBlock
  77. <inlineJS: '
  78. var buckets = self[''@buckets''];
  79. var keys = Object.keys(buckets);
  80. for (var i = 0; i < keys.length; ++i) { buckets[keys[i]]._do_(aBlock); }
  81. '>
  82. ! !
  83. !BucketStore methodsFor: 'initialization'!
  84. initialize
  85. super initialize.
  86. self removeAll
  87. ! !
  88. !BucketStore methodsFor: 'private'!
  89. newBucket
  90. self subclassResponsibility
  91. ! !
  92. !BucketStore class methodsFor: 'instance creation'!
  93. hashBlock: aBlock
  94. ^ self new
  95. hashBlock: aBlock;
  96. yourself
  97. ! !
  98. BucketStore subclass: #ArrayBucketStore
  99. instanceVariableNames: ''
  100. package: 'Kernel-Collections'!
  101. !ArrayBucketStore commentStamp!
  102. I am a concrete `BucketStore` with buckets being instance of `Array`.!
  103. !ArrayBucketStore methodsFor: 'private'!
  104. newBucket
  105. ^ #()
  106. ! !
  107. Object subclass: #Collection
  108. instanceVariableNames: ''
  109. package: 'Kernel-Collections'!
  110. !Collection commentStamp!
  111. I am the abstract superclass of all classes that represent a group of elements.
  112. I provide a set of useful methods to the Collection hierarchy such as enumerating and converting methods.!
  113. !Collection methodsFor: 'accessing'!
  114. anyOne
  115. "Answer a representative sample of the receiver. This method can
  116. be helpful when needing to preinfer the nature of the contents of
  117. semi-homogeneous collections."
  118. self ifEmpty: [ self error: 'Collection is empty' ].
  119. self do: [ :each | ^ each ]
  120. !
  121. occurrencesOf: anObject
  122. "Answer how many of the receiver's elements are equal to anObject."
  123. | tally |
  124. tally := 0.
  125. self do: [ :each | anObject = each ifTrue: [ tally := tally + 1 ]].
  126. ^ tally
  127. !
  128. single
  129. "Answer a single element.
  130. Raise an error if collection holds less or more than one element."
  131. self ifEmpty: [ self error: 'Collection is empty' ].
  132. self size > 1 ifTrue: [ self error: 'Collection holds more than one element' ].
  133. ^ self anyOne
  134. !
  135. size
  136. self subclassResponsibility
  137. ! !
  138. !Collection methodsFor: 'adding/removing'!
  139. add: anObject
  140. self subclassResponsibility
  141. !
  142. addAll: aCollection
  143. aCollection do: [ :each |
  144. self add: each ].
  145. ^ aCollection
  146. !
  147. remove: anObject
  148. ^ self remove: anObject ifAbsent: [ self errorNotFound ]
  149. !
  150. remove: anObject ifAbsent: aBlock
  151. self subclassResponsibility
  152. !
  153. removeAll
  154. self subclassResponsibility
  155. ! !
  156. !Collection methodsFor: 'converting'!
  157. asArray
  158. ^ Array withAll: self
  159. !
  160. asJSON
  161. ^ self asArray collect: [ :each | each asJSON ]
  162. !
  163. asOrderedCollection
  164. ^ self asArray
  165. !
  166. asSet
  167. ^ Set withAll: self
  168. ! !
  169. !Collection methodsFor: 'copying'!
  170. , aCollection
  171. ^ self copy
  172. addAll: aCollection;
  173. yourself
  174. !
  175. copyWith: anObject
  176. ^ self copy add: anObject; yourself
  177. !
  178. copyWithAll: aCollection
  179. ^ self copy addAll: aCollection; yourself
  180. !
  181. copyWithout: anObject
  182. "Answer a copy of the receiver that does not contain
  183. any occurrences of anObject."
  184. ^ self reject: [ :each | each = anObject ]
  185. !
  186. copyWithoutAll: aCollection
  187. "Answer a copy of the receiver that does not contain any elements
  188. equal to those in aCollection."
  189. ^ self reject: [ :each | aCollection includes: each ]
  190. !
  191. deepCopy
  192. ^ self collect: [ :each | each deepCopy ]
  193. !
  194. shallowCopy
  195. ^ self collect: [ :each | each ]
  196. ! !
  197. !Collection methodsFor: 'enumerating'!
  198. allSatisfy: aBlock
  199. "Evaluate aBlock with the elements of the receiver.
  200. If aBlock returns false for any element return false.
  201. Otherwise return true."
  202. self do: [ :each | (aBlock value: each) ifFalse: [ ^ false ] ].
  203. ^ true
  204. !
  205. anySatisfy: aBlock
  206. "Evaluate aBlock with the elements of the receiver.
  207. If aBlock returns true for any element return true.
  208. Otherwise return false."
  209. self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].
  210. ^ false
  211. !
  212. collect: aBlock
  213. | stream |
  214. stream := self class new writeStream.
  215. self do: [ :each |
  216. stream nextPut: (aBlock value: each) ].
  217. ^ stream contents
  218. !
  219. detect: aBlock
  220. ^ self detect: aBlock ifNone: [ self errorNotFound ]
  221. !
  222. detect: aBlock ifNone: anotherBlock
  223. self subclassResponsibility
  224. !
  225. do: aBlock
  226. self subclassResponsibility
  227. !
  228. do: aBlock separatedBy: anotherBlock
  229. | actionBeforeElement |
  230. actionBeforeElement := [ actionBeforeElement := anotherBlock ].
  231. self do: [ :each |
  232. actionBeforeElement value.
  233. aBlock value: each ]
  234. !
  235. inject: anObject into: aBlock
  236. | result |
  237. result := anObject.
  238. self do: [ :each |
  239. result := aBlock value: result value: each ].
  240. ^ result
  241. !
  242. intersection: aCollection
  243. "Answer the set theoretic intersection of two collections."
  244. | set outputSet |
  245. set := self asSet.
  246. outputSet := Set new.
  247. aCollection do: [ :each |
  248. ((set includes: each) and: [ (outputSet includes: each) not ])
  249. ifTrue: [
  250. outputSet add: each ]].
  251. ^ self class withAll: outputSet asArray
  252. !
  253. noneSatisfy: aBlock
  254. "Evaluate aBlock with the elements of the receiver.
  255. If aBlock returns false for all elements return true.
  256. Otherwise return false"
  257. self do: [ :item | (aBlock value: item) ifTrue: [ ^ false ] ].
  258. ^ true
  259. !
  260. reject: aBlock
  261. ^ self select: [ :each | (aBlock value: each) = false ]
  262. !
  263. select: aBlock
  264. | stream |
  265. stream := self class new writeStream.
  266. self do: [ :each |
  267. (aBlock value: each) ifTrue: [
  268. stream nextPut: each ] ].
  269. ^ stream contents
  270. !
  271. select: selectBlock thenCollect: collectBlock
  272. | stream |
  273. stream := self class new writeStream.
  274. self do: [ :each |
  275. (selectBlock value: each) ifTrue: [
  276. stream nextPut: (collectBlock value: each) ] ].
  277. ^ stream contents
  278. ! !
  279. !Collection methodsFor: 'error handling'!
  280. errorNotFound
  281. self error: 'Object is not in the collection'
  282. ! !
  283. !Collection methodsFor: 'streaming'!
  284. putOn: aStream
  285. self do: [ :each | each putOn: aStream ]
  286. ! !
  287. !Collection methodsFor: 'testing'!
  288. ifEmpty: aBlock
  289. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver.
  290. 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:
  291. self classifyMethodAs:
  292. (myProtocol ifEmpty: ['As yet unclassified'])"
  293. ^ self isEmpty
  294. ifTrue: "aBlock" [ aBlock value ]
  295. ifFalse: [ self ]
  296. !
  297. ifEmpty: aBlock ifNotEmpty: anotherBlock
  298. ^ self isEmpty
  299. ifTrue: "aBlock" [ aBlock value ]
  300. ifFalse: [ anotherBlock value: self ]
  301. !
  302. ifNotEmpty: aBlock
  303. ^ self notEmpty
  304. ifTrue: [ aBlock value: self ]
  305. ifFalse: [ self ]
  306. !
  307. ifNotEmpty: aBlock ifEmpty: anotherBlock
  308. ^ self notEmpty
  309. ifTrue: [ aBlock value: self ]
  310. ifFalse: "anotherBlock" [ anotherBlock value ]
  311. !
  312. includes: anObject
  313. ^ self anySatisfy: [ :each | each = anObject ]
  314. !
  315. isEmpty
  316. ^ self size = 0
  317. !
  318. notEmpty
  319. ^ self isEmpty not
  320. ! !
  321. !Collection class methodsFor: 'accessing'!
  322. classTag
  323. "Returns a tag or general category for this class.
  324. Typically used to help tools do some reflection.
  325. Helios, for example, uses this to decide what icon the class should display."
  326. ^ 'collection'
  327. ! !
  328. !Collection class methodsFor: 'instance creation'!
  329. new: anInteger
  330. ^ self new
  331. !
  332. with: anObject
  333. ^ self new
  334. add: anObject;
  335. yourself
  336. !
  337. with: anObject with: anotherObject
  338. ^ self new
  339. add: anObject;
  340. add: anotherObject;
  341. yourself
  342. !
  343. with: firstObject with: secondObject with: thirdObject
  344. ^ self new
  345. add: firstObject;
  346. add: secondObject;
  347. add: thirdObject;
  348. yourself
  349. !
  350. withAll: aCollection
  351. ^ self new
  352. addAll: aCollection;
  353. yourself
  354. ! !
  355. Collection subclass: #IndexableCollection
  356. instanceVariableNames: ''
  357. package: 'Kernel-Collections'!
  358. !IndexableCollection commentStamp!
  359. I am a key-value store collection, that is,
  360. I store values under indexes.
  361. As a rule of thumb, if a collection has `#at:` and `#at:put:`,
  362. it is an IndexableCollection.!
  363. !IndexableCollection methodsFor: 'accessing'!
  364. at: anIndex
  365. "Lookup the given index in the receiver.
  366. If it is present, answer the value stored at anIndex.
  367. Otherwise, raise an error."
  368. ^ self at: anIndex ifAbsent: [ self errorNotFound ]
  369. !
  370. at: anIndex ifAbsent: aBlock
  371. "Lookup the given index in the receiver.
  372. If it is present, answer the value stored at anIndex.
  373. Otherwise, answer the value of aBlock."
  374. self subclassResponsibility
  375. !
  376. at: aKey ifAbsentPut: aBlock
  377. ^ self at: aKey ifAbsent: [
  378. self at: aKey put: aBlock value ]
  379. !
  380. at: anIndex ifPresent: aBlock
  381. "Lookup the given index in the receiver.
  382. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  383. Otherwise, answer nil."
  384. ^ self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  385. !
  386. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  387. "Lookup the given index in the receiver.
  388. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  389. Otherwise, answer the value of anotherBlock."
  390. self subclassResponsibility
  391. !
  392. at: anIndex put: anObject
  393. "Store anObject under the given index in the receiver."
  394. self subclassResponsibility
  395. !
  396. indexOf: anObject
  397. "Lookup index at which anObject is stored in the receiver.
  398. If not present, raise an error."
  399. ^ self indexOf: anObject ifAbsent: [ self errorNotFound ]
  400. !
  401. indexOf: anObject ifAbsent: aBlock
  402. "Lookup index at which anObject is stored in the receiver.
  403. If not present, return value of executing aBlock."
  404. self subclassResponsibility
  405. ! !
  406. !IndexableCollection methodsFor: 'enumerating'!
  407. with: anotherCollection do: aBlock
  408. "Calls aBlock with every value from self
  409. and with indetically-indexed value from anotherCollection"
  410. self withIndexDo: [ :each :index |
  411. aBlock value: each value: (anotherCollection at: index) ]
  412. !
  413. withIndexDo: aBlock
  414. "Calls aBlock with every value from self
  415. and with its index as the second argument"
  416. self subclassResponsibility
  417. ! !
  418. IndexableCollection subclass: #AssociativeCollection
  419. instanceVariableNames: ''
  420. package: 'Kernel-Collections'!
  421. !AssociativeCollection commentStamp!
  422. I am a base class for object-indexed collections (Dictionary et.al.).!
  423. !AssociativeCollection methodsFor: 'accessing'!
  424. associations
  425. | associations |
  426. associations := #().
  427. self associationsDo: [ :each | associations add: each ].
  428. ^ associations
  429. !
  430. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  431. "Lookup the given key in the receiver.
  432. If it is present, answer the value of evaluating the oneArgBlock
  433. with the value associated with the key, otherwise answer the value
  434. of absentBlock."
  435. ^ (self includesKey: aKey)
  436. ifTrue: [ aBlock value: (self at: aKey) ]
  437. ifFalse: [ anotherBlock value ]
  438. !
  439. indexOf: anObject ifAbsent: aBlock
  440. ^ self keys
  441. detect: [ :each | (self at: each) = anObject ]
  442. ifNone: aBlock
  443. !
  444. keyAtValue: anObject
  445. ^ self keyAtValue: anObject ifAbsent: [ self errorNotFound ]
  446. !
  447. keyAtValue: anObject ifAbsent: aBlock
  448. ^ self indexOf: anObject ifAbsent: aBlock
  449. !
  450. keys
  451. self subclassResponsibility
  452. !
  453. size
  454. ^ self keys size
  455. !
  456. values
  457. self subclassResponsibility
  458. ! !
  459. !AssociativeCollection methodsFor: 'adding/removing'!
  460. add: anAssociation
  461. self at: anAssociation key put: anAssociation value
  462. !
  463. addAll: anAssociativeCollection
  464. super addAll: anAssociativeCollection associations.
  465. ^ anAssociativeCollection
  466. !
  467. remove: aKey ifAbsent: aBlock
  468. ^ self removeKey: aKey ifAbsent: aBlock
  469. !
  470. removeAll
  471. ^ self keys do: [ :each | self removeKey: each ]
  472. !
  473. removeKey: aKey
  474. ^ self remove: aKey
  475. !
  476. removeKey: aKey ifAbsent: aBlock
  477. self subclassResponsibility
  478. ! !
  479. !AssociativeCollection methodsFor: 'comparing'!
  480. = anAssocitativeCollection
  481. self class = anAssocitativeCollection class ifFalse: [ ^ false ].
  482. self size = anAssocitativeCollection size ifFalse: [ ^ false ].
  483. ^ self associations = anAssocitativeCollection associations
  484. ! !
  485. !AssociativeCollection methodsFor: 'converting'!
  486. asDictionary
  487. ^ Dictionary from: self associations
  488. !
  489. asHashedCollection
  490. ^ HashedCollection from: self associations
  491. !
  492. asJSON
  493. | hash |
  494. hash := HashedCollection new.
  495. self keysAndValuesDo: [ :key :value |
  496. hash at: key put: value asJSON ].
  497. ^ hash
  498. ! !
  499. !AssociativeCollection methodsFor: 'copying'!
  500. deepCopy
  501. | copy |
  502. copy := self class new.
  503. self keysAndValuesDo: [ :key :value |
  504. copy at: key put: value deepCopy ].
  505. ^ copy
  506. !
  507. shallowCopy
  508. | copy |
  509. copy := self class new.
  510. self keysAndValuesDo: [ :key :value |
  511. copy at: key put: value ].
  512. ^ copy
  513. ! !
  514. !AssociativeCollection methodsFor: 'enumerating'!
  515. associationsDo: aBlock
  516. self keysAndValuesDo: [ :key :value |
  517. aBlock value: (Association key: key value: value) ]
  518. !
  519. collect: aBlock
  520. | newDict |
  521. newDict := self class new.
  522. self keysAndValuesDo: [ :key :value |
  523. newDict at: key put: (aBlock value: value) ].
  524. ^ newDict
  525. !
  526. detect: aBlock ifNone: anotherBlock
  527. ^ self values detect: aBlock ifNone: anotherBlock
  528. !
  529. do: aBlock
  530. self valuesDo: aBlock
  531. !
  532. includes: anObject
  533. ^ self values includes: anObject
  534. !
  535. keysAndValuesDo: aBlock
  536. self keysDo: [ :each |
  537. aBlock value: each value: (self at: each) ]
  538. !
  539. keysDo: aBlock
  540. self subclassResponsibility
  541. !
  542. select: aBlock
  543. | newDict |
  544. newDict := self class new.
  545. self keysAndValuesDo: [ :key :value |
  546. (aBlock value: value) ifTrue: [ newDict at: key put: value ]].
  547. ^ newDict
  548. !
  549. valuesDo: aBlock
  550. self subclassResponsibility
  551. !
  552. withIndexDo: aBlock
  553. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  554. ! !
  555. !AssociativeCollection methodsFor: 'printing'!
  556. printOn: aStream
  557. super printOn: aStream.
  558. aStream nextPutAll: ' ('.
  559. self associations
  560. do: [ :each | each printOn: aStream ]
  561. separatedBy: [ aStream nextPutAll: ' , ' ].
  562. aStream nextPutAll: ')'
  563. ! !
  564. !AssociativeCollection methodsFor: 'testing'!
  565. includesKey: aKey
  566. self subclassResponsibility
  567. ! !
  568. !AssociativeCollection class methodsFor: 'instance creation'!
  569. from: aCollection
  570. | newCollection |
  571. newCollection := self new.
  572. aCollection do: [ :each | newCollection add: each ].
  573. ^ newCollection
  574. !
  575. fromPairs: aCollection
  576. "This message is poorly named and has been replaced by #from:"
  577. ^ self from: aCollection
  578. !
  579. newFromPairs: aCollection
  580. "Accept an array of elements where every two elements form an
  581. association - the odd element being the key, and the even element the value."
  582. | newCollection |
  583. aCollection size even ifFalse: [
  584. self error: '#newFromPairs only accepts arrays of an even length' ].
  585. newCollection := self new.
  586. ( 1 to: aCollection size by: 2 ) do: [ :each |
  587. newCollection at: (aCollection at: each) put: (aCollection at: each + 1) ].
  588. ^ newCollection
  589. ! !
  590. AssociativeCollection subclass: #Dictionary
  591. instanceVariableNames: 'keys values'
  592. package: 'Kernel-Collections'!
  593. !Dictionary commentStamp!
  594. I represent a set of elements that can be viewed from one of two perspectives: a set of associations,
  595. or a container of values that are externally named where the name can be any object that responds to `=`.
  596. The external name is referred to as the key.!
  597. !Dictionary methodsFor: 'accessing'!
  598. at: aKey ifAbsent: aBlock
  599. <inlineJS: '
  600. var index = self._positionOfKey_(aKey);
  601. return index >=0 ? self[''@values''][index] : aBlock._value();
  602. '>
  603. !
  604. at: aKey put: aValue
  605. <inlineJS: '
  606. var index = self._positionOfKey_(aKey);
  607. if(index === -1) {
  608. var keys = self[''@keys''];
  609. index = keys.length;
  610. keys.push(aKey);
  611. }
  612. return self[''@values''][index] = aValue;
  613. '>
  614. !
  615. indexOf: anObject ifAbsent: aBlock
  616. | index |
  617. index := values
  618. indexOf: anObject
  619. ifAbsent: [ 0 ].
  620. ^ index = 0
  621. ifTrue: [ aBlock value ]
  622. ifFalse: [ keys at: index ]
  623. !
  624. keys
  625. ^ keys copy
  626. !
  627. values
  628. ^ values
  629. ! !
  630. !Dictionary methodsFor: 'adding/removing'!
  631. removeAll
  632. keys removeAll.
  633. values removeAll
  634. !
  635. removeKey: aKey ifAbsent: aBlock
  636. <inlineJS: '
  637. var index = self._positionOfKey_(aKey);
  638. if(index === -1) {
  639. return aBlock._value()
  640. } else {
  641. var keys = self[''@keys''], values = self[''@values''];
  642. var value = values[index], l = keys.length;
  643. keys[index] = keys[l-1];
  644. keys.pop();
  645. values[index] = values[l-1];
  646. values.pop();
  647. return value;
  648. }
  649. '>
  650. ! !
  651. !Dictionary methodsFor: 'enumerating'!
  652. keysAndValuesDo: aBlock
  653. ^ keys with: values do: aBlock
  654. !
  655. keysDo: aBlock
  656. ^ keys do: aBlock
  657. !
  658. valuesDo: aBlock
  659. ^ values do: aBlock
  660. ! !
  661. !Dictionary methodsFor: 'initialization'!
  662. initialize
  663. super initialize.
  664. keys := #().
  665. values := #()
  666. ! !
  667. !Dictionary methodsFor: 'private'!
  668. positionOfKey: anObject
  669. <inlineJS: '
  670. var keys = self[''@keys''];
  671. for(var i=0;i<keys.length;i++){
  672. if(keys[i].__eq(anObject)) { return i;}
  673. }
  674. return -1;
  675. '>
  676. ! !
  677. !Dictionary methodsFor: 'testing'!
  678. includesKey: aKey
  679. <inlineJS: 'return self._positionOfKey_(aKey) >= 0;'>
  680. ! !
  681. AssociativeCollection subclass: #HashedCollection
  682. instanceVariableNames: ''
  683. package: 'Kernel-Collections'!
  684. !HashedCollection commentStamp!
  685. I am a traditional JavaScript object, or a Smalltalk `Dictionary`.
  686. Unlike a `Dictionary`, I can only have strings as keys.!
  687. !HashedCollection methodsFor: 'accessing'!
  688. at: aKey ifAbsent: aBlock
  689. ^ (self includesKey: aKey)
  690. ifTrue: [ self basicAt: aKey ]
  691. ifFalse: [ aBlock value ]
  692. !
  693. at: aKey put: aValue
  694. ^ self basicAt: aKey put: aValue
  695. !
  696. keys
  697. <inlineJS: 'return Object.keys(self)'>
  698. !
  699. values
  700. <inlineJS: '
  701. return self._keys().map(function(key){
  702. return self._at_(key);
  703. });
  704. '>
  705. ! !
  706. !HashedCollection methodsFor: 'adding/removing'!
  707. removeKey: aKey ifAbsent: aBlock
  708. ^ self
  709. at: aKey
  710. ifPresent: [ :removed | self basicDelete: aKey. removed ]
  711. ifAbsent: [ aBlock value ]
  712. ! !
  713. !HashedCollection methodsFor: 'enumerating'!
  714. keysDo: aBlock
  715. self keys do: aBlock
  716. !
  717. valuesDo: aBlock
  718. self values do: aBlock
  719. ! !
  720. !HashedCollection methodsFor: 'testing'!
  721. includesKey: aKey
  722. <inlineJS: 'return self.hasOwnProperty(aKey)'>
  723. ! !
  724. IndexableCollection subclass: #SequenceableCollection
  725. instanceVariableNames: ''
  726. package: 'Kernel-Collections'!
  727. !SequenceableCollection commentStamp!
  728. I am an IndexableCollection
  729. with numeric indexes starting with 1.!
  730. !SequenceableCollection methodsFor: 'accessing'!
  731. allButFirst
  732. ^ self copyFrom: 2 to: self size
  733. !
  734. allButLast
  735. ^ self copyFrom: 1 to: self size - 1
  736. !
  737. anyOne
  738. ^ self at: 1
  739. !
  740. atRandom
  741. ^ self at: self size atRandom
  742. !
  743. first
  744. ^ self at: 1
  745. !
  746. first: aNumber
  747. "Answer the first `aNumber` elements of the receiver.
  748. Raise an error if there are not enough elements in the receiver."
  749. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  750. ^ self copyFrom: 1 to: aNumber
  751. !
  752. fourth
  753. ^ self at: 4
  754. !
  755. indexOf: anObject ifAbsent: aBlock
  756. <inlineJS: '
  757. self = self._numericallyIndexable();
  758. for(var i=0; i < self.length; i++) {
  759. if($recv(self[i]).__eq(anObject)) {return i+1}
  760. };
  761. return aBlock._value();
  762. '>
  763. !
  764. indexOf: anObject startingAt: start
  765. "Answer the index of the first occurence of anElement after start
  766. within the receiver. If the receiver does not contain anElement,
  767. answer 0."
  768. ^ self indexOf: anObject startingAt: start ifAbsent: [ 0 ]
  769. !
  770. indexOf: anObject startingAt: start ifAbsent: aBlock
  771. <inlineJS: '
  772. self = self._numericallyIndexable();
  773. for(var i=start - 1; i < self.length; i++){
  774. if($recv(self[i]).__eq(anObject)) {return i+1}
  775. }
  776. return aBlock._value();
  777. '>
  778. !
  779. last
  780. ^ self at: self size
  781. !
  782. last: aNumber
  783. "Answer the last aNumber elements of the receiver.
  784. Raise an error if there are not enough elements in the receiver."
  785. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  786. ^ self copyFrom: self size - aNumber + 1 to: self size
  787. !
  788. second
  789. ^ self at: 2
  790. !
  791. single
  792. <inlineJS: '
  793. if (self.length == 0) throw new Error("Collection is empty");
  794. if (self.length > 1) throw new Error("Collection holds more than one element.");
  795. return self[0];
  796. '>
  797. !
  798. third
  799. ^ self at: 3
  800. ! !
  801. !SequenceableCollection methodsFor: 'adding/removing'!
  802. addLast: anObject
  803. self add: anObject
  804. !
  805. removeLast
  806. ^ self remove: self last
  807. ! !
  808. !SequenceableCollection methodsFor: 'comparing'!
  809. = aCollection
  810. (self class = aCollection class and: [
  811. self size = aCollection size ]) ifFalse: [ ^ false ].
  812. self withIndexDo: [ :each :i |
  813. (aCollection at: i) = each ifFalse: [ ^ false ]].
  814. ^ true
  815. ! !
  816. !SequenceableCollection methodsFor: 'converting'!
  817. reversed
  818. self subclassResponsibility
  819. ! !
  820. !SequenceableCollection methodsFor: 'copying'!
  821. copyFrom: anIndex to: anotherIndex
  822. self subclassResponsibility
  823. ! !
  824. !SequenceableCollection methodsFor: 'enumerating'!
  825. detect: aBlock ifNone: anotherBlock
  826. <inlineJS: '
  827. self = self._numericallyIndexable();
  828. for(var i = 0; i < self.length; i++)
  829. if(aBlock._value_(self[i]))
  830. return self[i];
  831. return anotherBlock._value();
  832. '>
  833. !
  834. do: aBlock
  835. <inlineJS: '
  836. self = self._numericallyIndexable();
  837. for(var i=0; i < self.length; i++) {
  838. aBlock._value_(self[i]);
  839. }
  840. '>
  841. !
  842. with: anotherCollection do: aBlock
  843. <inlineJS: '
  844. self = self._numericallyIndexable();
  845. anotherCollection = anotherCollection._numericallyIndexable();
  846. for(var i=0; i<self.length; i++) {
  847. aBlock._value_value_(self[i], anotherCollection[i]);
  848. }
  849. '>
  850. !
  851. withIndexDo: aBlock
  852. <inlineJS: '
  853. self = self._numericallyIndexable();
  854. for(var i=0; i < self.length; i++) {
  855. aBlock._value_value_(self[i], i+1);
  856. }
  857. '>
  858. ! !
  859. !SequenceableCollection methodsFor: 'private'!
  860. numericallyIndexable
  861. "This is an internal converting message.
  862. It answeres a representation of the receiver
  863. that can use foo[i] in JavaScript code.
  864. It fixes IE8, where boxed String is unable
  865. to numerically index its characters,
  866. but primitive string can."
  867. self subclassResponsibility
  868. ! !
  869. !SequenceableCollection methodsFor: 'streaming'!
  870. newStream
  871. ^ self streamClass on: self
  872. !
  873. readStream
  874. "For Pharo compatibility"
  875. ^ self stream
  876. !
  877. stream
  878. ^ self newStream
  879. !
  880. streamClass
  881. ^ self class streamClass
  882. !
  883. writeStream
  884. "For Pharo compatibility"
  885. ^ self stream
  886. ! !
  887. !SequenceableCollection methodsFor: 'testing'!
  888. beginsWith: prefix
  889. self size < prefix size ifTrue: [ ^ false ].
  890. ^ (self first: prefix size) = prefix
  891. !
  892. endsWith: suffix
  893. self size < suffix size ifTrue: [ ^ false ].
  894. ^ (self last: suffix size) = suffix
  895. !
  896. includes: anObject
  897. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  898. ! !
  899. !SequenceableCollection class methodsFor: 'accessing'!
  900. streamClass
  901. ^ Stream
  902. ! !
  903. !SequenceableCollection class methodsFor: 'streaming'!
  904. streamContents: aBlock
  905. | stream |
  906. stream := (self streamClass on: self new).
  907. aBlock value: stream.
  908. ^ stream contents
  909. ! !
  910. SequenceableCollection subclass: #Array
  911. instanceVariableNames: ''
  912. package: 'Kernel-Collections'!
  913. !Array commentStamp!
  914. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  915. I am directly mapped to JavaScript Number.
  916. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  917. !Array methodsFor: 'accessing'!
  918. at: anIndex ifAbsent: aBlock
  919. <inlineJS: '
  920. return anIndex >= 1 && anIndex <= self.length
  921. ? self[anIndex - 1]
  922. : aBlock._value()
  923. '>
  924. !
  925. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  926. <inlineJS: '
  927. return anIndex >= 1 && anIndex <= self.length
  928. ? aBlock._value_(self[anIndex - 1])
  929. : anotherBlock._value()
  930. '>
  931. !
  932. at: anIndex put: anObject
  933. <inlineJS: 'return self[anIndex - 1] = anObject'>
  934. !
  935. size
  936. <inlineJS: 'return self.length'>
  937. ! !
  938. !Array methodsFor: 'adding/removing'!
  939. add: anObject
  940. <inlineJS: 'self.push(anObject); return anObject;'>
  941. !
  942. addAll: aCollection
  943. <inlineJS: '
  944. if (Array.isArray(aCollection) && aCollection.length < 65000) self.push.apply(self, aCollection);
  945. else $globals.Array.superclass.fn.prototype._addAll_.call(self, aCollection);
  946. return aCollection;
  947. '>
  948. !
  949. addFirst: anObject
  950. <inlineJS: 'self.unshift(anObject); return anObject;'>
  951. !
  952. remove: anObject ifAbsent: aBlock
  953. | index |
  954. index := self indexOf: anObject ifAbsent: [ 0 ].
  955. ^ index = 0
  956. ifFalse: [ self removeIndex: index. anObject ]
  957. ifTrue: [ aBlock value ]
  958. !
  959. removeAll
  960. <inlineJS: 'self.length = 0'>
  961. !
  962. removeFrom: aNumber to: anotherNumber
  963. <inlineJS: 'self.splice(aNumber -1, anotherNumber - aNumber + 1)'>
  964. !
  965. removeIndex: anInteger
  966. <inlineJS: 'self.splice(anInteger - 1, 1)'>
  967. !
  968. removeLast
  969. <inlineJS: 'return self.pop();'>
  970. ! !
  971. !Array methodsFor: 'converting'!
  972. asJavascript
  973. ^ '[', ((self collect: [:each | each asJavascript ]) join: ', '), ']'
  974. !
  975. reversed
  976. <inlineJS: 'return self.slice().reverse()'>
  977. ! !
  978. !Array methodsFor: 'copying'!
  979. copyFrom: anIndex to: anotherIndex
  980. <inlineJS: '
  981. if (anIndex >= 1 && anotherIndex <= self.length) {
  982. return self.slice(anIndex - 1, anotherIndex);
  983. } else {
  984. self._at_(anIndex);
  985. self._at_(self.length + 1);
  986. throw new Error("Incorrect indexes in #copyFrom:to: not caught by #at:");
  987. }
  988. '>
  989. !
  990. shallowCopy
  991. <inlineJS: 'return self.slice()'>
  992. ! !
  993. !Array methodsFor: 'enumerating'!
  994. collect: aBlock
  995. "Optimized version"
  996. <inlineJS: 'return self.map(function(each) {return aBlock._value_(each)})'>
  997. !
  998. join: aString
  999. <inlineJS: 'return self.join(aString)'>
  1000. !
  1001. select: aBlock
  1002. "Optimized version"
  1003. <inlineJS: 'return self.filter(function(each) {return aBlock._value_(each)})'>
  1004. !
  1005. sort
  1006. ^ self sort: [ :a :b | a < b ]
  1007. !
  1008. sort: aBlock
  1009. <inlineJS: '
  1010. return self.sort(function(a, b) {
  1011. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  1012. })
  1013. '>
  1014. !
  1015. sorted
  1016. ^ self copy sort
  1017. !
  1018. sorted: aBlock
  1019. ^ self copy sort: aBlock
  1020. ! !
  1021. !Array methodsFor: 'printing'!
  1022. printOn: aStream
  1023. super printOn: aStream.
  1024. aStream nextPutAll: ' ('.
  1025. self
  1026. do: [ :each | each printOn: aStream ]
  1027. separatedBy: [ aStream nextPutAll: ' ' ].
  1028. aStream nextPutAll: ')'
  1029. ! !
  1030. !Array methodsFor: 'private'!
  1031. numericallyIndexable
  1032. ^ self
  1033. ! !
  1034. !Array class methodsFor: 'instance creation'!
  1035. new: anInteger
  1036. <inlineJS: 'return new Array(anInteger)'>
  1037. !
  1038. with: anObject
  1039. ^ (self new: 1)
  1040. at: 1 put: anObject;
  1041. yourself
  1042. !
  1043. with: anObject with: anObject2
  1044. ^ (self new: 2)
  1045. at: 1 put: anObject;
  1046. at: 2 put: anObject2;
  1047. yourself
  1048. !
  1049. with: anObject with: anObject2 with: anObject3
  1050. ^ (self new: 3)
  1051. at: 1 put: anObject;
  1052. at: 2 put: anObject2;
  1053. at: 3 put: anObject3;
  1054. yourself
  1055. !
  1056. withAll: aCollection
  1057. | instance index |
  1058. index := 1.
  1059. instance := self new: aCollection size.
  1060. aCollection do: [ :each |
  1061. instance at: index put: each.
  1062. index := index + 1 ].
  1063. ^ instance
  1064. ! !
  1065. SequenceableCollection subclass: #CharacterArray
  1066. instanceVariableNames: ''
  1067. package: 'Kernel-Collections'!
  1068. !CharacterArray commentStamp!
  1069. I am the abstract superclass of string-like collections.!
  1070. !CharacterArray methodsFor: 'accessing'!
  1071. at: anIndex put: anObject
  1072. self errorReadOnly
  1073. ! !
  1074. !CharacterArray methodsFor: 'adding/removing'!
  1075. add: anObject
  1076. self errorReadOnly
  1077. !
  1078. remove: anObject
  1079. self errorReadOnly
  1080. ! !
  1081. !CharacterArray methodsFor: 'converting'!
  1082. asLowercase
  1083. ^ self class fromString: self asString asLowercase
  1084. !
  1085. asNumber
  1086. ^ self asString asNumber
  1087. !
  1088. asString
  1089. ^ self subclassResponsibility
  1090. !
  1091. asSymbol
  1092. ^ self asString
  1093. !
  1094. asUppercase
  1095. ^ self class fromString: self asString asUppercase
  1096. ! !
  1097. !CharacterArray methodsFor: 'copying'!
  1098. , aString
  1099. ^ self asString, aString asString
  1100. ! !
  1101. !CharacterArray methodsFor: 'error handling'!
  1102. errorReadOnly
  1103. self error: 'Object is read-only'
  1104. ! !
  1105. !CharacterArray methodsFor: 'printing'!
  1106. printOn: aStream
  1107. self asString printOn: aStream
  1108. ! !
  1109. !CharacterArray methodsFor: 'streaming'!
  1110. putOn: aStream
  1111. aStream nextPutString: self
  1112. ! !
  1113. !CharacterArray class methodsFor: 'instance creation'!
  1114. fromString: aString
  1115. self subclassResponsibility
  1116. ! !
  1117. CharacterArray subclass: #String
  1118. instanceVariableNames: ''
  1119. package: 'Kernel-Collections'!
  1120. !String commentStamp!
  1121. 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.
  1122. String inherits many useful methods from its hierarchy, such as
  1123. `Collection >> #,`!
  1124. !String methodsFor: 'accessing'!
  1125. asciiValue
  1126. <inlineJS: 'return self.charCodeAt(0);'>
  1127. !
  1128. at: anIndex ifAbsent: aBlock
  1129. <inlineJS: 'return String(self)[anIndex - 1] || aBlock._value()'>
  1130. !
  1131. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1132. <inlineJS: '
  1133. var result = String(self)[anIndex - 1];
  1134. return result ? aBlock._value_(result) : anotherBlock._value();
  1135. '>
  1136. !
  1137. charCodeAt: anInteger
  1138. <inlineJS: 'return self.charCodeAt(anInteger - 1)'>
  1139. !
  1140. size
  1141. <inlineJS: 'return self.length'>
  1142. ! !
  1143. !String methodsFor: 'comparing'!
  1144. < aString
  1145. <inlineJS: 'return String(self) < aString._asString()'>
  1146. !
  1147. <= aString
  1148. <inlineJS: 'return String(self) <= aString._asString()'>
  1149. !
  1150. = aString
  1151. <inlineJS:
  1152. 'return aString !!= null && String(self) === (typeof aString === "string" ? aString : aString.valueOf())'>
  1153. !
  1154. == aString
  1155. <inlineJS: '
  1156. if (typeof aString === "string") return String(self) === aString;
  1157. else if (aString !!= null && typeof aString === "object") return String(self) === aString.valueOf();
  1158. else return false;
  1159. '>
  1160. !
  1161. > aString
  1162. <inlineJS: 'return String(self) > aString._asString()'>
  1163. !
  1164. >= aString
  1165. <inlineJS: 'return String(self) >= aString._asString()'>
  1166. ! !
  1167. !String methodsFor: 'converting'!
  1168. asJSON
  1169. ^ self
  1170. !
  1171. asJavaScriptMethodName
  1172. <inlineJS: 'return $core.st2js(self)'>
  1173. !
  1174. asJavascript
  1175. <inlineJS: '
  1176. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1177. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1178. else
  1179. return "\"" + self + "\"";
  1180. '>
  1181. !
  1182. asLowercase
  1183. <inlineJS: 'return self.toLowerCase()'>
  1184. !
  1185. asMutator
  1186. "Answer a setter selector. For example,
  1187. #name asMutator returns #name:"
  1188. self last = ':' ifFalse: [ ^ self, ':' ].
  1189. ^ self
  1190. !
  1191. asNumber
  1192. <inlineJS: 'return Number(self)'>
  1193. !
  1194. asRegexp
  1195. ^ RegularExpression fromString: self
  1196. !
  1197. asString
  1198. ^ self
  1199. !
  1200. asSymbol
  1201. ^ self
  1202. !
  1203. asUppercase
  1204. <inlineJS: 'return self.toUpperCase()'>
  1205. !
  1206. capitalized
  1207. ^ self ifNotEmpty: [ self first asUppercase, self allButFirst ]
  1208. !
  1209. crlfSanitized
  1210. ^ self lines join: String lf
  1211. !
  1212. escaped
  1213. <inlineJS: 'return escape(self)'>
  1214. !
  1215. reversed
  1216. <inlineJS: 'return self.split("").reverse().join("")'>
  1217. !
  1218. unescaped
  1219. <inlineJS: 'return unescape(self)'>
  1220. !
  1221. uriComponentDecoded
  1222. <inlineJS: 'return decodeURIComponent(self)'>
  1223. !
  1224. uriComponentEncoded
  1225. <inlineJS: 'return encodeURIComponent(self)'>
  1226. !
  1227. uriDecoded
  1228. <inlineJS: 'return decodeURI(self)'>
  1229. !
  1230. uriEncoded
  1231. <inlineJS: 'return encodeURI(self)'>
  1232. ! !
  1233. !String methodsFor: 'copying'!
  1234. , aString
  1235. <inlineJS: 'return String(self) + aString'>
  1236. !
  1237. copyFrom: anIndex to: anotherIndex
  1238. <inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
  1239. !
  1240. deepCopy
  1241. ^ self shallowCopy
  1242. !
  1243. shallowCopy
  1244. ^ self
  1245. ! !
  1246. !String methodsFor: 'evaluating'!
  1247. value: anObject
  1248. ^ anObject perform: self
  1249. ! !
  1250. !String methodsFor: 'printing'!
  1251. printNl
  1252. <inlineJS: 'console.log(self)'>
  1253. !
  1254. printOn: aStream
  1255. aStream
  1256. nextPutAll: '''';
  1257. nextPutAll: self;
  1258. nextPutAll: ''''
  1259. ! !
  1260. !String methodsFor: 'private'!
  1261. numericallyIndexable
  1262. <inlineJS: 'return String(self)'>
  1263. ! !
  1264. !String methodsFor: 'regular expressions'!
  1265. match: aRegexp
  1266. <inlineJS: 'return self.search(aRegexp) !!= -1'>
  1267. !
  1268. matchesOf: aRegularExpression
  1269. <inlineJS: 'return self.match(aRegularExpression)'>
  1270. !
  1271. replace: aString with: anotherString
  1272. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1273. !
  1274. replaceRegexp: aRegexp with: aString
  1275. <inlineJS: 'return self.replace(aRegexp, aString)'>
  1276. !
  1277. trimBoth
  1278. ^ self trimBoth: '\s'
  1279. !
  1280. trimBoth: separators
  1281. ^ (self trimLeft: separators) trimRight: separators
  1282. !
  1283. trimLeft
  1284. ^ self trimLeft: '\s'
  1285. !
  1286. trimLeft: separators
  1287. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1288. !
  1289. trimRight
  1290. ^ self trimRight: '\s'
  1291. !
  1292. trimRight: separators
  1293. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1294. ! !
  1295. !String methodsFor: 'split join'!
  1296. join: aCollection
  1297. ^ String
  1298. streamContents: [ :stream | aCollection
  1299. do: [ :each | stream nextPutAll: each asString ]
  1300. separatedBy: [ stream nextPutAll: self ]]
  1301. !
  1302. lineIndicesDo: aBlock
  1303. "execute aBlock with 3 arguments for each line:
  1304. - start index of line
  1305. - end index of line without line delimiter
  1306. - end index of line including line delimiter(s) CR, LF or CRLF"
  1307. | cr lf start sz nextLF nextCR |
  1308. start := 1.
  1309. sz := self size.
  1310. cr := String cr.
  1311. nextCR := self indexOf: cr startingAt: 1.
  1312. lf := String lf.
  1313. nextLF := self indexOf: lf startingAt: 1.
  1314. [ start <= sz ] whileTrue: [
  1315. (nextLF = 0 and: [ nextCR = 0 ])
  1316. ifTrue: [ "No more CR, nor LF, the string is over"
  1317. aBlock value: start value: sz value: sz.
  1318. ^ self ].
  1319. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1320. ifTrue: [ "Found a LF"
  1321. aBlock value: start value: nextLF - 1 value: nextLF.
  1322. start := 1 + nextLF.
  1323. nextLF := self indexOf: lf startingAt: start ]
  1324. ifFalse: [ 1 + nextCR = nextLF
  1325. ifTrue: [ "Found a CR-LF pair"
  1326. aBlock value: start value: nextCR - 1 value: nextLF.
  1327. start := 1 + nextLF.
  1328. nextCR := self indexOf: cr startingAt: start.
  1329. nextLF := self indexOf: lf startingAt: start ]
  1330. ifFalse: [ "Found a CR"
  1331. aBlock value: start value: nextCR - 1 value: nextCR.
  1332. start := 1 + nextCR.
  1333. nextCR := self indexOf: cr startingAt: start ] ]]
  1334. !
  1335. lineNumber: anIndex
  1336. "Answer a string containing the characters in the given line number."
  1337. | lineCount |
  1338. lineCount := 0.
  1339. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1340. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1341. ^ nil
  1342. !
  1343. lines
  1344. "Answer an array of lines composing this receiver without the line ending delimiters."
  1345. <inlineJS: '
  1346. var result = self.split(/\r\n|\r|\n/);
  1347. if (!!result[result.length-1]) result.pop();
  1348. return result;
  1349. '>
  1350. !
  1351. linesDo: aBlock
  1352. "Execute aBlock with each line in this string. The terminating line
  1353. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1354. self lines do: aBlock
  1355. !
  1356. subStrings: aString
  1357. ^ self tokenize: aString
  1358. !
  1359. tokenize: aString
  1360. <inlineJS: 'return self.split(aString)'>
  1361. ! !
  1362. !String methodsFor: 'testing'!
  1363. includesSubString: subString
  1364. <inlineJS: 'return self.indexOf(subString) !!= -1'>
  1365. !
  1366. isCapitalized
  1367. ^ self first asUppercase == self first
  1368. !
  1369. isImmutable
  1370. ^ true
  1371. !
  1372. isString
  1373. ^ true
  1374. !
  1375. isVowel
  1376. "Answer true if the receiver is a one character string containing a voyel"
  1377. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1378. ! !
  1379. !String class methodsFor: 'accessing'!
  1380. cr
  1381. <inlineJS: 'return "\r"'>
  1382. !
  1383. crlf
  1384. <inlineJS: 'return "\r\n"'>
  1385. !
  1386. esc
  1387. ^ self fromCharCode: 27
  1388. !
  1389. lf
  1390. <inlineJS: 'return "\n"'>
  1391. !
  1392. space
  1393. <inlineJS: 'return " "'>
  1394. !
  1395. streamClass
  1396. ^ StringStream
  1397. !
  1398. tab
  1399. <inlineJS: 'return "\t"'>
  1400. ! !
  1401. !String class methodsFor: 'instance creation'!
  1402. fromCharCode: anInteger
  1403. <inlineJS: 'return String.fromCharCode(anInteger)'>
  1404. !
  1405. fromString: aString
  1406. <inlineJS: 'return String(aString)'>
  1407. !
  1408. value: aUTFCharCode
  1409. <inlineJS: 'return String.fromCharCode(aUTFCharCode);'>
  1410. ! !
  1411. !String class methodsFor: 'random'!
  1412. random
  1413. "Returns random alphanumeric string beginning with letter"
  1414. <inlineJS: 'return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);'>
  1415. !
  1416. randomNotIn: aString
  1417. | result |
  1418. [ result := self random. aString includesSubString: result ] whileTrue.
  1419. ^ result
  1420. ! !
  1421. Collection subclass: #Set
  1422. instanceVariableNames: 'defaultBucket slowBucketStores fastBuckets size'
  1423. package: 'Kernel-Collections'!
  1424. !Set commentStamp!
  1425. I represent an unordered set of objects without duplicates.
  1426. ## Implementation notes
  1427. I put elements into different stores based on their type.
  1428. The goal is to store some elements into native JS object property names to be fast.
  1429. If an unboxed element has typeof 'string', 'boolean' or 'number', or an element is nil, null or undefined,
  1430. I store it as a property name in an empty (== Object.create(null)) JS object, different for each type
  1431. (for simplicity, nil/null/undefined is treated as one and included with the two booleans).
  1432. If element happen to be an object, I try to store them in `ArrayBucketStore`. I have two of them by default,
  1433. one hashed using the Smalltalk class name, the other one using the JS constructor name. It is possible to have more or less
  1434. instances of `ArrayBucketStores`, see `#initializeSlowBucketStores`.
  1435. As a last resort, if none of the `ArrayBucketStore` instances can find a suitable bucket, the `defaultBucket` is used,
  1436. which is an `Array`.!
  1437. !Set methodsFor: 'accessing'!
  1438. size
  1439. ^ size
  1440. ! !
  1441. !Set methodsFor: 'adding/removing'!
  1442. add: anObject
  1443. | bucket |
  1444. bucket := self bucketsOfElement: anObject.
  1445. ^ bucket second
  1446. ifNil: [
  1447. | object slowBucket |
  1448. object := bucket first.
  1449. slowBucket := bucket third.
  1450. slowBucket
  1451. indexOf: object
  1452. ifAbsent: [
  1453. slowBucket add: object.
  1454. size := size + 1 ].
  1455. object ]
  1456. ifNotNil: [ :primitiveBucket |
  1457. self
  1458. add: bucket first
  1459. in: primitiveBucket ]
  1460. !
  1461. remove: anObject ifAbsent: aBlock
  1462. | bucket |
  1463. bucket := self bucketsOfElement: anObject.
  1464. ^ bucket second
  1465. ifNil: [ bucket third remove: bucket first ifAbsent: [ ^aBlock value ]. size := size - 1 ]
  1466. ifNotNil: [ :primitiveBucket | self remove: bucket first in: primitiveBucket ]
  1467. !
  1468. removeAll
  1469. <inlineJS: '
  1470. self[''@fastBuckets''] = {
  1471. "boolean": { store: Object.create(null), fn: function (x) { return {"true": true, "false": false, "null": null}[x]; } },
  1472. "number": { store: Object.create(null), fn: Number },
  1473. "string": { store: Object.create(null) }
  1474. };
  1475. self[''@slowBucketStores''].forEach(function (x) { x._removeAll(); });
  1476. self[''@defaultBucket'']._removeAll();
  1477. self[''@size''] = 0;
  1478. '>
  1479. ! !
  1480. !Set methodsFor: 'comparing'!
  1481. = aCollection
  1482. self class = aCollection class ifFalse: [ ^ false ].
  1483. self size = aCollection size ifFalse: [ ^ false ].
  1484. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1485. ^ true
  1486. ! !
  1487. !Set methodsFor: 'enumerating'!
  1488. collect: aBlock
  1489. | collection |
  1490. collection := self class new.
  1491. self do: [ :each | collection add: (aBlock value: each) ].
  1492. ^ collection
  1493. !
  1494. detect: aBlock ifNone: anotherBlock
  1495. self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
  1496. ^ anotherBlock value
  1497. !
  1498. do: aBlock
  1499. <inlineJS: '
  1500. var el, keys, i;
  1501. el = self[''@fastBuckets''];
  1502. keys = Object.keys(el);
  1503. for (i = 0; i < keys.length; ++i) {
  1504. var fastBucket = el[keys[i]], fn = fastBucket.fn, store = Object.keys(fastBucket.store);
  1505. if (fn) { for (var j = 0; j < store.length; ++j) { aBlock._value_(fn(store[j])); } }
  1506. else { store._do_(aBlock); }
  1507. }
  1508. el = self[''@slowBucketStores''];
  1509. for (i = 0; i < el.length; ++i) { el[i]._do_(aBlock); }
  1510. self[''@defaultBucket'']._do_(aBlock);
  1511. '>
  1512. !
  1513. select: aBlock
  1514. | collection |
  1515. collection := self class new.
  1516. self do: [ :each |
  1517. (aBlock value: each) ifTrue: [
  1518. collection add: each ] ].
  1519. ^ collection
  1520. ! !
  1521. !Set methodsFor: 'initialization'!
  1522. initialize
  1523. super initialize.
  1524. defaultBucket := #().
  1525. self
  1526. initializeSlowBucketStores;
  1527. removeAll
  1528. !
  1529. initializeSlowBucketStores
  1530. slowBucketStores := {
  1531. ArrayBucketStore hashBlock: [ :x | self classNameOf: x ].
  1532. ArrayBucketStore hashBlock: [ :x | self jsConstructorNameOf: x ]
  1533. }
  1534. ! !
  1535. !Set methodsFor: 'printing'!
  1536. printOn: aStream
  1537. super printOn: aStream.
  1538. aStream nextPutAll: ' ('.
  1539. self
  1540. do: [ :each | each printOn: aStream ]
  1541. separatedBy: [ aStream nextPutAll: ' ' ].
  1542. aStream nextPutAll: ')'
  1543. ! !
  1544. !Set methodsFor: 'private'!
  1545. add: anObject in: anotherObject
  1546. <inlineJS: '
  1547. if (anObject in anotherObject.store) { return false; }
  1548. self[''@size'']++;
  1549. anotherObject.store[anObject] = true;
  1550. return anObject;
  1551. '>
  1552. !
  1553. bucketsOfElement: anObject
  1554. "Find the appropriate bucket for `anObject`.
  1555. For optimization purposes, directly answer an array with:
  1556. - the object to be store
  1557. - the primitive bucket
  1558. - the slow bucket"
  1559. <inlineJS: '
  1560. // include nil to well-known objects under "boolean" fastBucket
  1561. if (anObject == null || anObject.isNil) return [ null, self[''@fastBuckets''].boolean ];
  1562. var prim = anObject.valueOf();
  1563. if (typeof prim === "object" || typeof prim === "function" || !!self[''@fastBuckets''][typeof prim]) {
  1564. var bucket = null;
  1565. self[''@slowBucketStores''].some(function (store) {
  1566. return bucket = store._bucketOfElement_(anObject);
  1567. });
  1568. return [ anObject, null, bucket || self[''@defaultBucket''] ];
  1569. }
  1570. return [ prim, self[''@fastBuckets''][typeof prim] ];
  1571. '>
  1572. !
  1573. classNameOf: anObject
  1574. "Answer the class name of `anObject`, or `undefined`
  1575. if `anObject` is not an Smalltalk object"
  1576. <inlineJS: 'return anObject.klass !!= null && anObject.klass.className'>
  1577. !
  1578. includes: anObject in: anotherObject
  1579. <inlineJS: 'return anObject in anotherObject.store'>
  1580. !
  1581. jsConstructorNameOf: anObject
  1582. <inlineJS: 'return anObject.constructor && anObject.constructor.name'>
  1583. !
  1584. remove: anObject in: anotherObject
  1585. <inlineJS: 'if (delete anotherObject.store[anObject]) self[''@size'']--'>
  1586. ! !
  1587. !Set methodsFor: 'testing'!
  1588. includes: anObject
  1589. | bucket |
  1590. bucket := self bucketsOfElement: anObject.
  1591. ^ bucket second
  1592. ifNil: [ bucket third includes: bucket first ]
  1593. ifNotNil: [ :primitiveBucket | self includes: bucket first in: primitiveBucket ]
  1594. ! !
  1595. Object subclass: #ProtoStream
  1596. instanceVariableNames: ''
  1597. package: 'Kernel-Collections'!
  1598. !ProtoStream commentStamp!
  1599. I am the abstract base for different accessor for a sequence of objects. This sequence is referred to as my "contents".
  1600. My instances are read/write streams modifying the contents.!
  1601. !ProtoStream methodsFor: 'accessing'!
  1602. contents
  1603. self subclassResponsibility
  1604. ! !
  1605. !ProtoStream methodsFor: 'actions'!
  1606. reset
  1607. self subclassResponsibility
  1608. !
  1609. resetContents
  1610. self subclassResponsibility
  1611. ! !
  1612. !ProtoStream methodsFor: 'enumerating'!
  1613. do: aBlock
  1614. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1615. ! !
  1616. !ProtoStream methodsFor: 'positioning'!
  1617. setToEnd
  1618. self subclassResponsibility
  1619. !
  1620. setToStart
  1621. self reset
  1622. ! !
  1623. !ProtoStream methodsFor: 'reading'!
  1624. next
  1625. ^ self atEnd
  1626. ifTrue: [ nil ]
  1627. ifFalse: [ self subclassResponsibility ]
  1628. !
  1629. peek
  1630. ^ self atEnd
  1631. ifTrue: [ nil ]
  1632. ifFalse: [ self subclassResponsibility ]
  1633. ! !
  1634. !ProtoStream methodsFor: 'testing'!
  1635. atEnd
  1636. self subclassResponsibility
  1637. !
  1638. atStart
  1639. self subclassResponsibility
  1640. !
  1641. isEmpty
  1642. ^ self atStart and: [ self atEnd ]
  1643. ! !
  1644. !ProtoStream methodsFor: 'writing'!
  1645. << anObject
  1646. self write: anObject
  1647. !
  1648. nextPut: anObject
  1649. self subclassResponsibility
  1650. !
  1651. nextPutAll: aCollection
  1652. aCollection do: [ :each |
  1653. self nextPut: each ]
  1654. !
  1655. nextPutJSObject: aJSObject
  1656. self nextPut: aJSObject
  1657. !
  1658. nextPutString: aString
  1659. self nextPut: aString
  1660. !
  1661. write: anObject
  1662. anObject putOn: self
  1663. ! !
  1664. !ProtoStream class methodsFor: 'instance creation'!
  1665. on: aCollection
  1666. ^ self new
  1667. setCollection: aCollection;
  1668. setStreamSize: aCollection size;
  1669. yourself
  1670. ! !
  1671. ProtoStream subclass: #Stream
  1672. instanceVariableNames: 'collection position streamSize'
  1673. package: 'Kernel-Collections'!
  1674. !Stream commentStamp!
  1675. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1676. My instances are read/write streams to the contents sequence collection.!
  1677. !Stream methodsFor: 'accessing'!
  1678. collection
  1679. ^ collection
  1680. !
  1681. contents
  1682. ^ self collection
  1683. copyFrom: 1
  1684. to: self streamSize
  1685. !
  1686. position
  1687. ^ position ifNil: [ position := 0 ]
  1688. !
  1689. position: anInteger
  1690. position := anInteger
  1691. !
  1692. setCollection: aCollection
  1693. collection := aCollection
  1694. !
  1695. setStreamSize: anInteger
  1696. streamSize := anInteger
  1697. !
  1698. size
  1699. ^ self streamSize
  1700. !
  1701. streamSize
  1702. ^ streamSize
  1703. ! !
  1704. !Stream methodsFor: 'actions'!
  1705. close
  1706. !
  1707. flush
  1708. !
  1709. reset
  1710. self position: 0
  1711. !
  1712. resetContents
  1713. self reset.
  1714. self setStreamSize: 0
  1715. ! !
  1716. !Stream methodsFor: 'positioning'!
  1717. setToEnd
  1718. self position: self size
  1719. !
  1720. skip: anInteger
  1721. self position: ((self position + anInteger) min: self size max: 0)
  1722. ! !
  1723. !Stream methodsFor: 'reading'!
  1724. next
  1725. ^ self atEnd
  1726. ifTrue: [ nil ]
  1727. ifFalse: [
  1728. self position: self position + 1.
  1729. collection at: self position ]
  1730. !
  1731. next: anInteger
  1732. | tempCollection |
  1733. tempCollection := self collection class new.
  1734. anInteger timesRepeat: [
  1735. self atEnd ifFalse: [
  1736. tempCollection add: self next ]].
  1737. ^ tempCollection
  1738. !
  1739. peek
  1740. ^ self atEnd ifFalse: [
  1741. self collection at: self position + 1 ]
  1742. ! !
  1743. !Stream methodsFor: 'testing'!
  1744. atEnd
  1745. ^ self position = self size
  1746. !
  1747. atStart
  1748. ^ self position = 0
  1749. !
  1750. isEmpty
  1751. ^ self size = 0
  1752. ! !
  1753. !Stream methodsFor: 'writing'!
  1754. nextPut: anObject
  1755. self position: self position + 1.
  1756. self collection at: self position put: anObject.
  1757. self setStreamSize: (self streamSize max: self position)
  1758. ! !
  1759. !Stream class methodsFor: 'instance creation'!
  1760. on: aCollection
  1761. ^ self new
  1762. setCollection: aCollection;
  1763. setStreamSize: aCollection size;
  1764. yourself
  1765. ! !
  1766. Stream subclass: #StringStream
  1767. instanceVariableNames: ''
  1768. package: 'Kernel-Collections'!
  1769. !StringStream commentStamp!
  1770. I am a Stream specific to `String` objects.!
  1771. !StringStream methodsFor: 'reading'!
  1772. next: anInteger
  1773. | tempCollection |
  1774. tempCollection := self collection class new.
  1775. anInteger timesRepeat: [
  1776. self atEnd ifFalse: [
  1777. tempCollection := tempCollection, self next ]].
  1778. ^ tempCollection
  1779. ! !
  1780. !StringStream methodsFor: 'writing'!
  1781. cr
  1782. ^ self nextPutAll: String cr
  1783. !
  1784. crlf
  1785. ^ self nextPutAll: String crlf
  1786. !
  1787. lf
  1788. ^ self nextPutAll: String lf
  1789. !
  1790. nextPut: aString
  1791. self nextPutAll: aString
  1792. !
  1793. nextPutAll: aString
  1794. | pre post |
  1795. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1796. pre := self collection copyFrom: 1 to: self position.
  1797. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1798. self setCollection: pre, aString, post
  1799. ].
  1800. self position: self position + aString size.
  1801. self setStreamSize: (self streamSize max: self position)
  1802. !
  1803. nextPutString: aString
  1804. self nextPutAll: aString
  1805. !
  1806. space
  1807. self nextPut: ' '
  1808. !
  1809. tab
  1810. ^ self nextPutAll: String tab
  1811. ! !
  1812. Object subclass: #Queue
  1813. instanceVariableNames: 'read readIndex write'
  1814. package: 'Kernel-Collections'!
  1815. !Queue commentStamp!
  1816. I am a one-sided queue.
  1817. ## Usage
  1818. Use `#nextPut:` to add items to the queue.
  1819. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1820. ## Implementation notes
  1821. A Queue uses two OrderedCollections inside,
  1822. `read` is at the front, is not modified and only read using `readIndex`.
  1823. `write` is at the back and is appended new items.
  1824. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1825. As a consequence, no data moving is done by me, write appending may do data moving
  1826. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1827. !Queue methodsFor: 'accessing'!
  1828. next
  1829. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1830. !
  1831. nextIfAbsent: aBlock
  1832. | result |
  1833. result := read at: readIndex ifAbsent: [
  1834. write ifEmpty: [
  1835. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1836. ^ aBlock value ].
  1837. read := write.
  1838. readIndex := 1.
  1839. write := OrderedCollection new.
  1840. read first ].
  1841. read at: readIndex put: nil.
  1842. readIndex := readIndex + 1.
  1843. ^ result
  1844. !
  1845. nextPut: anObject
  1846. write add: anObject
  1847. ! !
  1848. !Queue methodsFor: 'initialization'!
  1849. initialize
  1850. super initialize.
  1851. read := OrderedCollection new.
  1852. write := OrderedCollection new.
  1853. readIndex := 1
  1854. ! !
  1855. Object subclass: #RegularExpression
  1856. instanceVariableNames: ''
  1857. package: 'Kernel-Collections'!
  1858. !RegularExpression commentStamp!
  1859. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1860. !RegularExpression methodsFor: 'evaluating'!
  1861. compile: aString
  1862. <inlineJS: 'return self.compile(aString)'>
  1863. !
  1864. exec: aString
  1865. <inlineJS: 'return self.exec(aString) || nil'>
  1866. !
  1867. test: aString
  1868. <inlineJS: 'return self.test(aString)'>
  1869. ! !
  1870. !RegularExpression class methodsFor: 'instance creation'!
  1871. fromString: aString
  1872. ^ self fromString: aString flag: ''
  1873. !
  1874. fromString: aString flag: anotherString
  1875. <inlineJS: 'return new RegExp(aString, anotherString)'>
  1876. ! !