Kernel-Collections.st 37 KB

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