Kernel-Collections.st 48 KB

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