Kernel-Collections.st 37 KB

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