Kernel-Collections.st 49 KB

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