Kernel-Collections.st 49 KB

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