Kernel-Collections.st 36 KB

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