Kernel-Collections.st 37 KB

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