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