Kernel-Collections.st 48 KB

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