Kernel-Collections.st 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  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. asSymbolPrintOn: aStream
  1107. aStream nextPutAll: '#'.
  1108. self asString isSelector
  1109. ifTrue: [ aStream nextPut: self ]
  1110. ifFalse: [ self printOn: aStream ]
  1111. !
  1112. printOn: aStream
  1113. self asString printOn: aStream
  1114. !
  1115. symbolPrintString
  1116. ^ String streamContents: [ :str | self asSymbolPrintOn: str ]
  1117. ! !
  1118. !CharacterArray methodsFor: 'streaming'!
  1119. putOn: aStream
  1120. aStream nextPutString: self
  1121. ! !
  1122. !CharacterArray class methodsFor: 'instance creation'!
  1123. fromString: aString
  1124. self subclassResponsibility
  1125. ! !
  1126. CharacterArray subclass: #String
  1127. instanceVariableNames: ''
  1128. package: 'Kernel-Collections'!
  1129. !String commentStamp!
  1130. 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.
  1131. String inherits many useful methods from its hierarchy, such as
  1132. `Collection >> #,`!
  1133. !String methodsFor: 'accessing'!
  1134. asciiValue
  1135. <inlineJS: 'return self.charCodeAt(0);'>
  1136. !
  1137. at: anIndex ifAbsent: aBlock
  1138. <inlineJS: 'return String(self)[anIndex - 1] || aBlock._value()'>
  1139. !
  1140. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1141. <inlineJS: '
  1142. var result = String(self)[anIndex - 1];
  1143. return result ? aBlock._value_(result) : anotherBlock._value();
  1144. '>
  1145. !
  1146. charCodeAt: anInteger
  1147. <inlineJS: 'return self.charCodeAt(anInteger - 1)'>
  1148. !
  1149. size
  1150. <inlineJS: 'return self.length'>
  1151. ! !
  1152. !String methodsFor: 'comparing'!
  1153. < aString
  1154. <inlineJS: 'return String(self) < aString._asString()'>
  1155. !
  1156. <= aString
  1157. <inlineJS: 'return String(self) <= aString._asString()'>
  1158. !
  1159. = aString
  1160. <inlineJS:
  1161. 'return aString !!= null && String(self) === (typeof aString === "string" ? aString : aString.valueOf())'>
  1162. !
  1163. == aString
  1164. <inlineJS: '
  1165. if (typeof aString === "string") return String(self) === aString;
  1166. else if (aString !!= null && typeof aString === "object") return String(self) === aString.valueOf();
  1167. else return false;
  1168. '>
  1169. !
  1170. > aString
  1171. <inlineJS: 'return String(self) > aString._asString()'>
  1172. !
  1173. >= aString
  1174. <inlineJS: 'return String(self) >= aString._asString()'>
  1175. ! !
  1176. !String methodsFor: 'converting'!
  1177. asJSON
  1178. ^ self
  1179. !
  1180. asJavaScriptMethodName
  1181. <inlineJS: 'return $core.st2js(self)'>
  1182. !
  1183. asJavascript
  1184. <inlineJS: '
  1185. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1186. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1187. else
  1188. return "\"" + self + "\"";
  1189. '>
  1190. !
  1191. asLowercase
  1192. <inlineJS: 'return self.toLowerCase()'>
  1193. !
  1194. asMutator
  1195. "Answer a setter selector. For example,
  1196. #name asMutator returns #name:"
  1197. self last = ':' ifFalse: [ ^ self, ':' ].
  1198. ^ self
  1199. !
  1200. asNumber
  1201. <inlineJS: 'return Number(self)'>
  1202. !
  1203. asRegexp
  1204. ^ RegularExpression fromString: self
  1205. !
  1206. asString
  1207. ^ self
  1208. !
  1209. asSymbol
  1210. ^ self
  1211. !
  1212. asUppercase
  1213. <inlineJS: 'return self.toUpperCase()'>
  1214. !
  1215. capitalized
  1216. ^ self ifNotEmpty: [ self first asUppercase, self allButFirst ]
  1217. !
  1218. crlfSanitized
  1219. ^ self lines join: String lf
  1220. !
  1221. escaped
  1222. <inlineJS: 'return escape(self)'>
  1223. !
  1224. reversed
  1225. <inlineJS: 'return self.split("").reverse().join("")'>
  1226. !
  1227. unescaped
  1228. <inlineJS: 'return unescape(self)'>
  1229. !
  1230. uriComponentDecoded
  1231. <inlineJS: 'return decodeURIComponent(self)'>
  1232. !
  1233. uriComponentEncoded
  1234. <inlineJS: 'return encodeURIComponent(self)'>
  1235. !
  1236. uriDecoded
  1237. <inlineJS: 'return decodeURI(self)'>
  1238. !
  1239. uriEncoded
  1240. <inlineJS: 'return encodeURI(self)'>
  1241. ! !
  1242. !String methodsFor: 'copying'!
  1243. , aString
  1244. <inlineJS: 'return String(self) + aString'>
  1245. !
  1246. copyFrom: anIndex to: anotherIndex
  1247. <inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
  1248. !
  1249. deepCopy
  1250. ^ self shallowCopy
  1251. !
  1252. shallowCopy
  1253. ^ self
  1254. ! !
  1255. !String methodsFor: 'evaluating'!
  1256. value: anObject
  1257. ^ anObject perform: self
  1258. ! !
  1259. !String methodsFor: 'printing'!
  1260. printNl
  1261. <inlineJS: 'console.log(self)'>
  1262. !
  1263. printOn: aStream
  1264. aStream
  1265. nextPutAll: '''';
  1266. nextPutAll: (self replace: '''' with: '''''');
  1267. nextPutAll: ''''
  1268. ! !
  1269. !String methodsFor: 'private'!
  1270. numericallyIndexable
  1271. <inlineJS: 'return String(self)'>
  1272. ! !
  1273. !String methodsFor: 'regular expressions'!
  1274. match: aRegexp
  1275. <inlineJS: 'return self.search(aRegexp) !!= -1'>
  1276. !
  1277. matchesOf: aRegularExpression
  1278. <inlineJS: 'return self.match(aRegularExpression)'>
  1279. !
  1280. replace: aString with: anotherString
  1281. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1282. !
  1283. replaceRegexp: aRegexp with: aString
  1284. <inlineJS: 'return self.replace(aRegexp, aString)'>
  1285. !
  1286. trimBoth
  1287. ^ self trimBoth: '\s'
  1288. !
  1289. trimBoth: separators
  1290. ^ (self trimLeft: separators) trimRight: separators
  1291. !
  1292. trimLeft
  1293. ^ self trimLeft: '\s'
  1294. !
  1295. trimLeft: separators
  1296. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1297. !
  1298. trimRight
  1299. ^ self trimRight: '\s'
  1300. !
  1301. trimRight: separators
  1302. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1303. ! !
  1304. !String methodsFor: 'split join'!
  1305. join: aCollection
  1306. ^ String
  1307. streamContents: [ :stream | aCollection
  1308. do: [ :each | stream nextPutAll: each asString ]
  1309. separatedBy: [ stream nextPutAll: self ]]
  1310. !
  1311. lineIndicesDo: aBlock
  1312. "execute aBlock with 3 arguments for each line:
  1313. - start index of line
  1314. - end index of line without line delimiter
  1315. - end index of line including line delimiter(s) CR, LF or CRLF"
  1316. | cr lf start sz nextLF nextCR |
  1317. start := 1.
  1318. sz := self size.
  1319. cr := String cr.
  1320. nextCR := self indexOf: cr startingAt: 1.
  1321. lf := String lf.
  1322. nextLF := self indexOf: lf startingAt: 1.
  1323. [ start <= sz ] whileTrue: [
  1324. (nextLF = 0 and: [ nextCR = 0 ])
  1325. ifTrue: [ "No more CR, nor LF, the string is over"
  1326. aBlock value: start value: sz value: sz.
  1327. ^ self ].
  1328. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1329. ifTrue: [ "Found a LF"
  1330. aBlock value: start value: nextLF - 1 value: nextLF.
  1331. start := 1 + nextLF.
  1332. nextLF := self indexOf: lf startingAt: start ]
  1333. ifFalse: [ 1 + nextCR = nextLF
  1334. ifTrue: [ "Found a CR-LF pair"
  1335. aBlock value: start value: nextCR - 1 value: nextLF.
  1336. start := 1 + nextLF.
  1337. nextCR := self indexOf: cr startingAt: start.
  1338. nextLF := self indexOf: lf startingAt: start ]
  1339. ifFalse: [ "Found a CR"
  1340. aBlock value: start value: nextCR - 1 value: nextCR.
  1341. start := 1 + nextCR.
  1342. nextCR := self indexOf: cr startingAt: start ] ]]
  1343. !
  1344. lineNumber: anIndex
  1345. "Answer a string containing the characters in the given line number."
  1346. | lineCount |
  1347. lineCount := 0.
  1348. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1349. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1350. ^ nil
  1351. !
  1352. lines
  1353. "Answer an array of lines composing this receiver without the line ending delimiters."
  1354. <inlineJS: '
  1355. var result = self.split(/\r\n|\r|\n/);
  1356. if (!!result[result.length-1]) result.pop();
  1357. return result;
  1358. '>
  1359. !
  1360. linesDo: aBlock
  1361. "Execute aBlock with each line in this string. The terminating line
  1362. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1363. self lines do: aBlock
  1364. !
  1365. subStrings: aString
  1366. ^ self tokenize: aString
  1367. !
  1368. tokenize: aString
  1369. <inlineJS: 'return self.split(aString)'>
  1370. ! !
  1371. !String methodsFor: 'testing'!
  1372. includesSubString: subString
  1373. <inlineJS: 'return self.indexOf(subString) !!= -1'>
  1374. !
  1375. isCapitalized
  1376. ^ self first asUppercase == self first
  1377. !
  1378. isImmutable
  1379. ^ true
  1380. !
  1381. isSelector
  1382. <inlineJS:
  1383. 'return !!!!self.match(/^([a-zA-Z][a-zA-Z0-9]*|[\\+*/=><,@%~|&-]+|([a-zA-Z][a-zA-Z0-9]*\:)+)$/)'
  1384. >
  1385. !
  1386. isString
  1387. ^ true
  1388. !
  1389. isVowel
  1390. "Answer true if the receiver is a one character string containing a voyel"
  1391. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1392. ! !
  1393. !String class methodsFor: 'accessing'!
  1394. cr
  1395. <inlineJS: 'return "\r"'>
  1396. !
  1397. crlf
  1398. <inlineJS: 'return "\r\n"'>
  1399. !
  1400. esc
  1401. ^ self fromCharCode: 27
  1402. !
  1403. lf
  1404. <inlineJS: 'return "\n"'>
  1405. !
  1406. space
  1407. <inlineJS: 'return " "'>
  1408. !
  1409. streamClass
  1410. ^ StringStream
  1411. !
  1412. tab
  1413. <inlineJS: 'return "\t"'>
  1414. ! !
  1415. !String class methodsFor: 'instance creation'!
  1416. fromCharCode: anInteger
  1417. <inlineJS: 'return String.fromCharCode(anInteger)'>
  1418. !
  1419. fromString: aString
  1420. <inlineJS: 'return String(aString)'>
  1421. !
  1422. value: aUTFCharCode
  1423. <inlineJS: 'return String.fromCharCode(aUTFCharCode);'>
  1424. ! !
  1425. !String class methodsFor: 'random'!
  1426. random
  1427. "Returns random alphanumeric string beginning with letter"
  1428. <inlineJS: 'return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);'>
  1429. !
  1430. randomNotIn: aString
  1431. | result |
  1432. [ result := self random. aString includesSubString: result ] whileTrue.
  1433. ^ result
  1434. ! !
  1435. Collection subclass: #Set
  1436. instanceVariableNames: 'defaultBucket slowBucketStores fastBuckets size'
  1437. package: 'Kernel-Collections'!
  1438. !Set commentStamp!
  1439. I represent an unordered set of objects without duplicates.
  1440. ## Implementation notes
  1441. I put elements into different stores based on their type.
  1442. The goal is to store some elements into native JS object property names to be fast.
  1443. If an unboxed element has typeof 'string', 'boolean' or 'number', or an element is nil, null or undefined,
  1444. I store it as a property name in an empty (== Object.create(null)) JS object, different for each type
  1445. (for simplicity, nil/null/undefined is treated as one and included with the two booleans).
  1446. If element happen to be an object, I try to store them in `ArrayBucketStore`. I have two of them by default,
  1447. one hashed using the Smalltalk class name, the other one using the JS constructor name. It is possible to have more or less
  1448. instances of `ArrayBucketStores`, see `#initializeSlowBucketStores`.
  1449. As a last resort, if none of the `ArrayBucketStore` instances can find a suitable bucket, the `defaultBucket` is used,
  1450. which is an `Array`.!
  1451. !Set methodsFor: 'accessing'!
  1452. size
  1453. ^ size
  1454. ! !
  1455. !Set methodsFor: 'adding/removing'!
  1456. add: anObject
  1457. | bucket |
  1458. bucket := self bucketsOfElement: anObject.
  1459. ^ bucket second
  1460. ifNil: [
  1461. | object slowBucket |
  1462. object := bucket first.
  1463. slowBucket := bucket third.
  1464. slowBucket
  1465. indexOf: object
  1466. ifAbsent: [
  1467. slowBucket add: object.
  1468. size := size + 1 ].
  1469. object ]
  1470. ifNotNil: [ :primitiveBucket |
  1471. self
  1472. add: bucket first
  1473. in: primitiveBucket ]
  1474. !
  1475. remove: anObject ifAbsent: aBlock
  1476. | bucket |
  1477. bucket := self bucketsOfElement: anObject.
  1478. ^ bucket second
  1479. ifNil: [ bucket third remove: bucket first ifAbsent: [ ^aBlock value ]. size := size - 1 ]
  1480. ifNotNil: [ :primitiveBucket | self remove: bucket first in: primitiveBucket ]
  1481. !
  1482. removeAll
  1483. <inlineJS: '
  1484. self[''@fastBuckets''] = {
  1485. "boolean": { store: Object.create(null), fn: function (x) { return {"true": true, "false": false, "null": null}[x]; } },
  1486. "number": { store: Object.create(null), fn: Number },
  1487. "string": { store: Object.create(null) }
  1488. };
  1489. self[''@slowBucketStores''].forEach(function (x) { x._removeAll(); });
  1490. self[''@defaultBucket'']._removeAll();
  1491. self[''@size''] = 0;
  1492. '>
  1493. ! !
  1494. !Set methodsFor: 'comparing'!
  1495. = aCollection
  1496. self class = aCollection class ifFalse: [ ^ false ].
  1497. self size = aCollection size ifFalse: [ ^ false ].
  1498. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1499. ^ true
  1500. ! !
  1501. !Set methodsFor: 'enumerating'!
  1502. collect: aBlock
  1503. | collection |
  1504. collection := self class new.
  1505. self do: [ :each | collection add: (aBlock value: each) ].
  1506. ^ collection
  1507. !
  1508. detect: aBlock ifNone: anotherBlock
  1509. self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
  1510. ^ anotherBlock value
  1511. !
  1512. do: aBlock
  1513. <inlineJS: '
  1514. var el, keys, i;
  1515. el = self[''@fastBuckets''];
  1516. keys = Object.keys(el);
  1517. for (i = 0; i < keys.length; ++i) {
  1518. var fastBucket = el[keys[i]], fn = fastBucket.fn, store = Object.keys(fastBucket.store);
  1519. if (fn) { for (var j = 0; j < store.length; ++j) { aBlock._value_(fn(store[j])); } }
  1520. else { store._do_(aBlock); }
  1521. }
  1522. el = self[''@slowBucketStores''];
  1523. for (i = 0; i < el.length; ++i) { el[i]._do_(aBlock); }
  1524. self[''@defaultBucket'']._do_(aBlock);
  1525. '>
  1526. !
  1527. select: aBlock
  1528. | collection |
  1529. collection := self class new.
  1530. self do: [ :each |
  1531. (aBlock value: each) ifTrue: [
  1532. collection add: each ] ].
  1533. ^ collection
  1534. ! !
  1535. !Set methodsFor: 'initialization'!
  1536. initialize
  1537. super initialize.
  1538. defaultBucket := #().
  1539. self
  1540. initializeSlowBucketStores;
  1541. removeAll
  1542. !
  1543. initializeSlowBucketStores
  1544. slowBucketStores := {
  1545. ArrayBucketStore hashBlock: [ :x | self classNameOf: x ].
  1546. ArrayBucketStore hashBlock: [ :x | self jsConstructorNameOf: x ]
  1547. }
  1548. ! !
  1549. !Set methodsFor: 'printing'!
  1550. printOn: aStream
  1551. super printOn: aStream.
  1552. aStream nextPutAll: ' ('.
  1553. self
  1554. do: [ :each | each printOn: aStream ]
  1555. separatedBy: [ aStream nextPutAll: ' ' ].
  1556. aStream nextPutAll: ')'
  1557. ! !
  1558. !Set methodsFor: 'private'!
  1559. add: anObject in: anotherObject
  1560. <inlineJS: '
  1561. if (anObject in anotherObject.store) { return false; }
  1562. self[''@size'']++;
  1563. anotherObject.store[anObject] = true;
  1564. return anObject;
  1565. '>
  1566. !
  1567. bucketsOfElement: anObject
  1568. "Find the appropriate bucket for `anObject`.
  1569. For optimization purposes, directly answer an array with:
  1570. - the object to be store
  1571. - the primitive bucket
  1572. - the slow bucket"
  1573. <inlineJS: '
  1574. // include nil to well-known objects under "boolean" fastBucket
  1575. if (anObject == null || anObject.isNil) return [ null, self[''@fastBuckets''].boolean ];
  1576. var prim = anObject.valueOf();
  1577. if (typeof prim === "object" || typeof prim === "function" || !!self[''@fastBuckets''][typeof prim]) {
  1578. var bucket = null;
  1579. self[''@slowBucketStores''].some(function (store) {
  1580. return bucket = store._bucketOfElement_(anObject);
  1581. });
  1582. return [ anObject, null, bucket || self[''@defaultBucket''] ];
  1583. }
  1584. return [ prim, self[''@fastBuckets''][typeof prim] ];
  1585. '>
  1586. !
  1587. classNameOf: anObject
  1588. "Answer the class name of `anObject`, or `undefined`
  1589. if `anObject` is not an Smalltalk object"
  1590. <inlineJS: 'return anObject.klass !!= null && anObject.klass.className'>
  1591. !
  1592. includes: anObject in: anotherObject
  1593. <inlineJS: 'return anObject in anotherObject.store'>
  1594. !
  1595. jsConstructorNameOf: anObject
  1596. <inlineJS: 'return anObject.constructor && anObject.constructor.name'>
  1597. !
  1598. remove: anObject in: anotherObject
  1599. <inlineJS: 'if (delete anotherObject.store[anObject]) self[''@size'']--'>
  1600. ! !
  1601. !Set methodsFor: 'testing'!
  1602. includes: anObject
  1603. | bucket |
  1604. bucket := self bucketsOfElement: anObject.
  1605. ^ bucket second
  1606. ifNil: [ bucket third includes: bucket first ]
  1607. ifNotNil: [ :primitiveBucket | self includes: bucket first in: primitiveBucket ]
  1608. ! !
  1609. Object subclass: #ProtoStream
  1610. instanceVariableNames: ''
  1611. package: 'Kernel-Collections'!
  1612. !ProtoStream commentStamp!
  1613. I am the abstract base for different accessor for a sequence of objects. This sequence is referred to as my "contents".
  1614. My instances are read/write streams modifying the contents.!
  1615. !ProtoStream methodsFor: 'accessing'!
  1616. contents
  1617. self subclassResponsibility
  1618. ! !
  1619. !ProtoStream methodsFor: 'actions'!
  1620. reset
  1621. self subclassResponsibility
  1622. !
  1623. resetContents
  1624. self subclassResponsibility
  1625. ! !
  1626. !ProtoStream methodsFor: 'enumerating'!
  1627. do: aBlock
  1628. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1629. ! !
  1630. !ProtoStream methodsFor: 'positioning'!
  1631. setToEnd
  1632. self subclassResponsibility
  1633. !
  1634. setToStart
  1635. self reset
  1636. ! !
  1637. !ProtoStream methodsFor: 'reading'!
  1638. next
  1639. ^ self atEnd
  1640. ifTrue: [ nil ]
  1641. ifFalse: [ self subclassResponsibility ]
  1642. !
  1643. peek
  1644. ^ self atEnd
  1645. ifTrue: [ nil ]
  1646. ifFalse: [ self subclassResponsibility ]
  1647. ! !
  1648. !ProtoStream methodsFor: 'testing'!
  1649. atEnd
  1650. self subclassResponsibility
  1651. !
  1652. atStart
  1653. self subclassResponsibility
  1654. !
  1655. isEmpty
  1656. ^ self atStart and: [ self atEnd ]
  1657. ! !
  1658. !ProtoStream methodsFor: 'writing'!
  1659. << anObject
  1660. self write: anObject
  1661. !
  1662. nextPut: anObject
  1663. self subclassResponsibility
  1664. !
  1665. nextPutAll: aCollection
  1666. aCollection do: [ :each |
  1667. self nextPut: each ]
  1668. !
  1669. nextPutJSObject: aJSObject
  1670. self nextPut: aJSObject
  1671. !
  1672. nextPutString: aString
  1673. self nextPut: aString
  1674. !
  1675. write: anObject
  1676. anObject putOn: self
  1677. ! !
  1678. !ProtoStream class methodsFor: 'instance creation'!
  1679. on: aCollection
  1680. ^ self new
  1681. setCollection: aCollection;
  1682. setStreamSize: aCollection size;
  1683. yourself
  1684. ! !
  1685. ProtoStream subclass: #Stream
  1686. instanceVariableNames: 'collection position streamSize'
  1687. package: 'Kernel-Collections'!
  1688. !Stream commentStamp!
  1689. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1690. My instances are read/write streams to the contents sequence collection.!
  1691. !Stream methodsFor: 'accessing'!
  1692. collection
  1693. ^ collection
  1694. !
  1695. contents
  1696. ^ self collection
  1697. copyFrom: 1
  1698. to: self streamSize
  1699. !
  1700. position
  1701. ^ position ifNil: [ position := 0 ]
  1702. !
  1703. position: anInteger
  1704. position := anInteger
  1705. !
  1706. setCollection: aCollection
  1707. collection := aCollection
  1708. !
  1709. setStreamSize: anInteger
  1710. streamSize := anInteger
  1711. !
  1712. size
  1713. ^ self streamSize
  1714. !
  1715. streamSize
  1716. ^ streamSize
  1717. ! !
  1718. !Stream methodsFor: 'actions'!
  1719. close
  1720. !
  1721. flush
  1722. !
  1723. reset
  1724. self position: 0
  1725. !
  1726. resetContents
  1727. self reset.
  1728. self setStreamSize: 0
  1729. ! !
  1730. !Stream methodsFor: 'positioning'!
  1731. setToEnd
  1732. self position: self size
  1733. !
  1734. skip: anInteger
  1735. self position: ((self position + anInteger) min: self size max: 0)
  1736. ! !
  1737. !Stream methodsFor: 'reading'!
  1738. next
  1739. ^ self atEnd
  1740. ifTrue: [ nil ]
  1741. ifFalse: [
  1742. self position: self position + 1.
  1743. collection at: self position ]
  1744. !
  1745. next: anInteger
  1746. | tempCollection |
  1747. tempCollection := self collection class new.
  1748. anInteger timesRepeat: [
  1749. self atEnd ifFalse: [
  1750. tempCollection add: self next ]].
  1751. ^ tempCollection
  1752. !
  1753. peek
  1754. ^ self atEnd ifFalse: [
  1755. self collection at: self position + 1 ]
  1756. ! !
  1757. !Stream methodsFor: 'testing'!
  1758. atEnd
  1759. ^ self position = self size
  1760. !
  1761. atStart
  1762. ^ self position = 0
  1763. !
  1764. isEmpty
  1765. ^ self size = 0
  1766. ! !
  1767. !Stream methodsFor: 'writing'!
  1768. nextPut: anObject
  1769. self position: self position + 1.
  1770. self collection at: self position put: anObject.
  1771. self setStreamSize: (self streamSize max: self position)
  1772. ! !
  1773. !Stream class methodsFor: 'instance creation'!
  1774. on: aCollection
  1775. ^ self new
  1776. setCollection: aCollection;
  1777. setStreamSize: aCollection size;
  1778. yourself
  1779. ! !
  1780. Stream subclass: #StringStream
  1781. instanceVariableNames: ''
  1782. package: 'Kernel-Collections'!
  1783. !StringStream commentStamp!
  1784. I am a Stream specific to `String` objects.!
  1785. !StringStream methodsFor: 'reading'!
  1786. next: anInteger
  1787. | tempCollection |
  1788. tempCollection := self collection class new.
  1789. anInteger timesRepeat: [
  1790. self atEnd ifFalse: [
  1791. tempCollection := tempCollection, self next ]].
  1792. ^ tempCollection
  1793. ! !
  1794. !StringStream methodsFor: 'writing'!
  1795. cr
  1796. ^ self nextPutAll: String cr
  1797. !
  1798. crlf
  1799. ^ self nextPutAll: String crlf
  1800. !
  1801. lf
  1802. ^ self nextPutAll: String lf
  1803. !
  1804. nextPut: aString
  1805. self nextPutAll: aString
  1806. !
  1807. nextPutAll: aString
  1808. | pre post |
  1809. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1810. pre := self collection copyFrom: 1 to: self position.
  1811. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1812. self setCollection: pre, aString, post
  1813. ].
  1814. self position: self position + aString size.
  1815. self setStreamSize: (self streamSize max: self position)
  1816. !
  1817. nextPutString: aString
  1818. self nextPutAll: aString
  1819. !
  1820. print: anObject
  1821. anObject printOn: self
  1822. !
  1823. space
  1824. self nextPut: ' '
  1825. !
  1826. tab
  1827. ^ self nextPutAll: String tab
  1828. ! !
  1829. Object subclass: #Queue
  1830. instanceVariableNames: 'read readIndex write'
  1831. package: 'Kernel-Collections'!
  1832. !Queue commentStamp!
  1833. I am a one-sided queue.
  1834. ## Usage
  1835. Use `#nextPut:` to add items to the queue.
  1836. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1837. ## Implementation notes
  1838. A Queue uses two OrderedCollections inside,
  1839. `read` is at the front, is not modified and only read using `readIndex`.
  1840. `write` is at the back and is appended new items.
  1841. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1842. As a consequence, no data moving is done by me, write appending may do data moving
  1843. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1844. !Queue methodsFor: 'accessing'!
  1845. next
  1846. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1847. !
  1848. nextIfAbsent: aBlock
  1849. | result |
  1850. result := read at: readIndex ifAbsent: [
  1851. write ifEmpty: [
  1852. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1853. ^ aBlock value ].
  1854. read := write.
  1855. readIndex := 1.
  1856. write := OrderedCollection new.
  1857. read first ].
  1858. read at: readIndex put: nil.
  1859. readIndex := readIndex + 1.
  1860. ^ result
  1861. !
  1862. nextPut: anObject
  1863. write add: anObject
  1864. ! !
  1865. !Queue methodsFor: 'initialization'!
  1866. initialize
  1867. super initialize.
  1868. read := OrderedCollection new.
  1869. write := OrderedCollection new.
  1870. readIndex := 1
  1871. ! !
  1872. Object subclass: #RegularExpression
  1873. instanceVariableNames: ''
  1874. package: 'Kernel-Collections'!
  1875. !RegularExpression commentStamp!
  1876. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1877. !RegularExpression methodsFor: 'evaluating'!
  1878. compile: aString
  1879. <inlineJS: 'return self.compile(aString)'>
  1880. !
  1881. exec: aString
  1882. <inlineJS: 'return self.exec(aString) || nil'>
  1883. !
  1884. test: aString
  1885. <inlineJS: 'return self.test(aString)'>
  1886. ! !
  1887. !RegularExpression class methodsFor: 'instance creation'!
  1888. fromString: aString
  1889. ^ self fromString: aString flag: ''
  1890. !
  1891. fromString: aString flag: anotherString
  1892. <inlineJS: 'return new RegExp(aString, anotherString)'>
  1893. ! !