Kernel-Collections.st 49 KB

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