1
0

Kernel-Collections.st 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. Smalltalk current createPackage: 'Kernel-Collections' properties: #{}!
  2. Object subclass: #Collection
  3. instanceVariableNames: ''
  4. category: 'Kernel-Collections'!
  5. !Collection methodsFor: 'accessing'!
  6. size
  7. self subclassResponsibility
  8. !
  9. readStream
  10. ^self stream
  11. !
  12. writeStream
  13. ^self stream
  14. !
  15. stream
  16. ^self streamClass on: self
  17. !
  18. streamClass
  19. ^self class streamClass
  20. ! !
  21. !Collection methodsFor: 'adding/removing'!
  22. add: anObject
  23. self subclassResponsibility
  24. !
  25. addAll: aCollection
  26. aCollection do: [:each |
  27. self add: each].
  28. ^aCollection
  29. !
  30. remove: anObject
  31. ^self remove: anObject ifAbsent: [self errorNotFound]
  32. !
  33. remove: anObject ifAbsent: aBlock
  34. self subclassResponsibility
  35. ! !
  36. !Collection methodsFor: 'converting'!
  37. asArray
  38. | array index |
  39. array := Array new.
  40. index := 0.
  41. self do: [:each |
  42. index := index + 1.
  43. array at: index put: each].
  44. ^array
  45. !
  46. asSet
  47. ^Set withAll: self
  48. !
  49. asJSONString
  50. ^JSON stringify: (self collect: [:each | each asJSONString])
  51. ! !
  52. !Collection methodsFor: 'copying'!
  53. , aCollection
  54. ^self copy
  55. addAll: aCollection;
  56. yourself
  57. !
  58. copyWith: anObject
  59. ^self copy add: anObject; yourself
  60. !
  61. copyWithAll: aCollection
  62. ^self copy addAll: aCollection; yourself
  63. !
  64. copyWithoutAll: aCollection
  65. "Answer a copy of the receiver that does not contain any elements
  66. equal to those in aCollection."
  67. ^ self reject: [:each | aCollection includes: each]
  68. ! !
  69. !Collection methodsFor: 'enumerating'!
  70. do: aBlock
  71. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  72. !
  73. collect: aBlock
  74. | newCollection |
  75. newCollection := self class new.
  76. self do: [:each |
  77. newCollection add: (aBlock value: each)].
  78. ^newCollection
  79. !
  80. detect: aBlock
  81. ^self detect: aBlock ifNone: [self errorNotFound]
  82. !
  83. detect: aBlock ifNone: anotherBlock
  84. <
  85. for(var i = 0; i < self.length; i++)
  86. if(aBlock(self[i]))
  87. return self[i];
  88. return anotherBlock();
  89. >
  90. !
  91. do: aBlock separatedBy: anotherBlock
  92. | first |
  93. first := true.
  94. self do: [:each |
  95. first
  96. ifTrue: [first := false]
  97. ifFalse: [anotherBlock value].
  98. aBlock value: each]
  99. !
  100. inject: anObject into: aBlock
  101. | result |
  102. result := anObject.
  103. self do: [:each |
  104. result := aBlock value: result value: each].
  105. ^result
  106. !
  107. reject: aBlock
  108. ^self select: [:each | (aBlock value: each) = false]
  109. !
  110. select: aBlock
  111. | stream |
  112. stream := self class new writeStream.
  113. self do: [:each |
  114. (aBlock value: each) ifTrue: [
  115. stream nextPut: each]].
  116. ^stream contents
  117. ! !
  118. !Collection methodsFor: 'error handling'!
  119. errorNotFound
  120. self error: 'Object is not in the collection'
  121. ! !
  122. !Collection methodsFor: 'testing'!
  123. includes: anObject
  124. <
  125. var i = self.length;
  126. while (i--) {
  127. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  128. }
  129. return false
  130. >
  131. !
  132. notEmpty
  133. ^self isEmpty not
  134. !
  135. isEmpty
  136. ^self size = 0
  137. !
  138. ifNotEmpty: aBlock
  139. self notEmpty ifTrue: aBlock.
  140. !
  141. ifEmpty: aBlock
  142. self isEmpty ifTrue: aBlock.
  143. ! !
  144. !Collection class methodsFor: 'accessing'!
  145. streamClass
  146. ^Stream
  147. ! !
  148. !Collection class methodsFor: 'instance creation'!
  149. with: anObject
  150. ^self new
  151. add: anObject;
  152. yourself
  153. !
  154. with: anObject with: anotherObject
  155. ^self new
  156. add: anObject;
  157. add: anotherObject;
  158. yourself
  159. !
  160. with: firstObject with: secondObject with: thirdObject
  161. ^self new
  162. add: firstObject;
  163. add: secondObject;
  164. add: thirdObject;
  165. yourself
  166. !
  167. withAll: aCollection
  168. ^self new
  169. addAll: aCollection;
  170. yourself
  171. ! !
  172. Collection subclass: #SequenceableCollection
  173. instanceVariableNames: ''
  174. category: 'Kernel-Collections'!
  175. !SequenceableCollection methodsFor: 'accessing'!
  176. at: anIndex
  177. ^self at: anIndex ifAbsent: [
  178. self errorNotFound]
  179. !
  180. at: anIndex ifAbsent: aBlock
  181. self subclassResponsibility
  182. !
  183. at: anIndex put: anObject
  184. self subclassResponsibility
  185. !
  186. first
  187. ^self at: 1
  188. !
  189. fourth
  190. ^self at: 4
  191. !
  192. last
  193. ^self at: self size
  194. !
  195. second
  196. ^self at: 2
  197. !
  198. third
  199. ^self at: 3
  200. !
  201. allButFirst
  202. ^self copyFrom: 2 to: self size
  203. !
  204. allButLast
  205. ^self copyFrom: 1 to: self size - 1
  206. !
  207. indexOf: anObject
  208. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  209. !
  210. indexOf: anObject ifAbsent: aBlock
  211. <
  212. for(var i=0;i<self.length;i++){
  213. if(self[i].__eq(anObject)) {return i+1}
  214. }
  215. return aBlock();
  216. >
  217. !
  218. indexOf: anObject startingAt: start ifAbsent: aBlock
  219. <
  220. for(var i=start-1;i<self.length;i++){
  221. if(self[i].__eq(anObject)) {return i+1}
  222. }
  223. return aBlock();
  224. >
  225. !
  226. indexOf: anObject startingAt: start
  227. "Answer the index of the first occurence of anElement after start
  228. within the receiver. If the receiver does not contain anElement,
  229. answer 0."
  230. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  231. !
  232. atRandom
  233. ^ self at: self size atRandom
  234. ! !
  235. !SequenceableCollection methodsFor: 'adding'!
  236. removeLast
  237. self remove: self last
  238. !
  239. addLast: anObject
  240. self add: anObject
  241. ! !
  242. !SequenceableCollection methodsFor: 'converting'!
  243. reversed
  244. self subclassResponsibility
  245. ! !
  246. !SequenceableCollection methodsFor: 'copying'!
  247. copyFrom: anIndex to: anotherIndex
  248. self subclassResponsibility
  249. ! !
  250. !SequenceableCollection methodsFor: 'enumerating'!
  251. withIndexDo: aBlock
  252. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  253. ! !
  254. SequenceableCollection subclass: #String
  255. instanceVariableNames: ''
  256. category: 'Kernel-Collections'!
  257. !String methodsFor: 'accessing'!
  258. size
  259. <return self.length>
  260. !
  261. at: anIndex put: anObject
  262. self errorReadOnly
  263. !
  264. at: anIndex ifAbsent: aBlock
  265. <return self[anIndex - 1] || aBlock()>
  266. !
  267. escaped
  268. <return escape(self)>
  269. !
  270. unescaped
  271. <return unescape(self)>
  272. !
  273. asciiValue
  274. <return self.charCodeAt(0);>
  275. ! !
  276. !String methodsFor: 'adding'!
  277. add: anObject
  278. self errorReadOnly
  279. !
  280. remove: anObject
  281. self errorReadOnly
  282. ! !
  283. !String methodsFor: 'comparing'!
  284. = aString
  285. aString class = self class ifFalse: [^false].
  286. <return String(self) == aString>
  287. !
  288. > aString
  289. <return String(self) >> aString>
  290. !
  291. < aString
  292. <return String(self) < aString>
  293. !
  294. >= aString
  295. <return String(self) >>= aString>
  296. !
  297. <= aString
  298. <return String(self) <= aString>
  299. !
  300. == aString
  301. aString class = self class ifFalse: [^false].
  302. <return String(self) === String(aString)>
  303. ! !
  304. !String methodsFor: 'converting'!
  305. asSelector
  306. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  307. | selector |
  308. selector := '_', self.
  309. selector := selector replace: ':' with: '_'.
  310. selector := selector replace: '[+]' with: '_plus'.
  311. selector := selector replace: '-' with: '_minus'.
  312. selector := selector replace: '[*]' with: '_star'.
  313. selector := selector replace: '[/]' with: '_slash'.
  314. selector := selector replace: '>' with: '_gt'.
  315. selector := selector replace: '<' with: '_lt'.
  316. selector := selector replace: '=' with: '_eq'.
  317. selector := selector replace: ',' with: '_comma'.
  318. selector := selector replace: '[@]' with: '_at'.
  319. ^selector
  320. !
  321. asJavascript
  322. <
  323. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  324. return "unescape(\"" + escape(self) + "\")";
  325. else
  326. return "\"" + self + "\"";
  327. >
  328. !
  329. tokenize: aString
  330. <return self.split(aString)>
  331. !
  332. asString
  333. ^self
  334. !
  335. asNumber
  336. <return Number(self)>
  337. !
  338. asLowercase
  339. <return self.toLowerCase()>
  340. !
  341. asUppercase
  342. <return self.toUpperCase()>
  343. !
  344. reversed
  345. <return self.split("").reverse().join("")>
  346. !
  347. asJavaScriptSelector
  348. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  349. !
  350. asJSONString
  351. ^self
  352. ! !
  353. !String methodsFor: 'copying'!
  354. , aString
  355. <return self + aString>
  356. !
  357. copyFrom: anIndex to: anotherIndex
  358. <return self.substring(anIndex - 1, anotherIndex)>
  359. !
  360. shallowCopy
  361. ^self class fromString: self
  362. !
  363. deepCopy
  364. ^self shallowCopy
  365. ! !
  366. !String methodsFor: 'error handling'!
  367. errorReadOnly
  368. self error: 'Object is read-only'
  369. ! !
  370. !String methodsFor: 'printing'!
  371. printString
  372. ^'''', self, ''''
  373. !
  374. printNl
  375. <console.log(self)>
  376. ! !
  377. !String methodsFor: 'regular expressions'!
  378. replace: aString with: anotherString
  379. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  380. !
  381. replaceRegexp: aRegexp with: aString
  382. <return self.replace(aRegexp, aString)>
  383. !
  384. match: aRegexp
  385. <return self.search(aRegexp) !!= -1>
  386. !
  387. trimLeft: separators
  388. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  389. !
  390. trimRight: separators
  391. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  392. !
  393. trimLeft
  394. ^self trimLeft: '\s'
  395. !
  396. trimRight
  397. ^self trimRight: '\s'
  398. !
  399. trimBoth
  400. ^self trimBoth: '\s'
  401. !
  402. trimBoth: separators
  403. ^(self trimLeft: separators) trimRight: separators
  404. ! !
  405. !String methodsFor: 'split join'!
  406. join: aCollection
  407. ^ String
  408. streamContents: [:stream | aCollection
  409. do: [:each | stream nextPutAll: each asString]
  410. separatedBy: [stream nextPutAll: self]]
  411. !
  412. lineIndicesDo: aBlock
  413. "execute aBlock with 3 arguments for each line:
  414. - start index of line
  415. - end index of line without line delimiter
  416. - end index of line including line delimiter(s) CR, LF or CRLF"
  417. | cr lf start sz nextLF nextCR |
  418. start := 1.
  419. sz := self size.
  420. cr := String cr.
  421. nextCR := self indexOf: cr startingAt: 1.
  422. lf := String lf.
  423. nextLF := self indexOf: lf startingAt: 1.
  424. [ start <= sz ] whileTrue: [
  425. (nextLF = 0 and: [ nextCR = 0 ])
  426. ifTrue: [ "No more CR, nor LF, the string is over"
  427. aBlock value: start value: sz value: sz.
  428. ^self ].
  429. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  430. ifTrue: [ "Found a LF"
  431. aBlock value: start value: nextLF - 1 value: nextLF.
  432. start := 1 + nextLF.
  433. nextLF := self indexOf: lf startingAt: start ]
  434. ifFalse: [ 1 + nextCR = nextLF
  435. ifTrue: [ "Found a CR-LF pair"
  436. aBlock value: start value: nextCR - 1 value: nextLF.
  437. start := 1 + nextLF.
  438. nextCR := self indexOf: cr startingAt: start.
  439. nextLF := self indexOf: lf startingAt: start ]
  440. ifFalse: [ "Found a CR"
  441. aBlock value: start value: nextCR - 1 value: nextCR.
  442. start := 1 + nextCR.
  443. nextCR := self indexOf: cr startingAt: start ]]]
  444. !
  445. linesDo: aBlock
  446. "Execute aBlock with each line in this string. The terminating line
  447. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  448. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  449. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  450. !
  451. lines
  452. "Answer an array of lines composing this receiver without the line ending delimiters."
  453. | lines |
  454. lines := Array new.
  455. self linesDo: [:aLine | lines add: aLine].
  456. ^lines
  457. !
  458. lineNumber: anIndex
  459. "Answer a string containing the characters in the given line number."
  460. | lineCount |
  461. lineCount := 0.
  462. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  463. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  464. ^nil
  465. ! !
  466. !String methodsFor: 'testing'!
  467. isString
  468. ^true
  469. !
  470. includesSubString: subString
  471. < return self.indexOf(subString) !!= -1 >
  472. ! !
  473. !String class methodsFor: 'accessing'!
  474. streamClass
  475. ^StringStream
  476. !
  477. cr
  478. <return '\r'>
  479. !
  480. lf
  481. <return '\n'>
  482. !
  483. space
  484. <return ' '>
  485. !
  486. tab
  487. <return '\t'>
  488. !
  489. crlf
  490. <return '\r\n'>
  491. ! !
  492. !String class methodsFor: 'instance creation'!
  493. fromString: aString
  494. <return new self.fn(aString)>
  495. !
  496. streamContents: blockWithArg
  497. |stream|
  498. stream := (self streamClass on: String new).
  499. blockWithArg value: stream.
  500. ^ stream contents
  501. !
  502. value: aUTFCharCode
  503. <return String.fromCharCode(aUTFCharCode);>
  504. ! !
  505. SequenceableCollection subclass: #Array
  506. instanceVariableNames: ''
  507. category: 'Kernel-Collections'!
  508. !Array methodsFor: 'accessing'!
  509. size
  510. <return self.length>
  511. !
  512. at: anIndex put: anObject
  513. <return self[anIndex - 1] = anObject>
  514. !
  515. at: anIndex ifAbsent: aBlock
  516. <
  517. var value = self[anIndex - 1];
  518. if(value === undefined) {
  519. return aBlock();
  520. } else {
  521. return value;
  522. }
  523. >
  524. ! !
  525. !Array methodsFor: 'adding/removing'!
  526. add: anObject
  527. <self.push(anObject); return anObject;>
  528. !
  529. remove: anObject
  530. <
  531. for(var i=0;i<self.length;i++) {
  532. if(self[i] == anObject) {
  533. self.splice(i,1);
  534. break;
  535. }
  536. }
  537. >
  538. !
  539. removeFrom: aNumber to: anotherNumber
  540. <self.splice(aNumber - 1,anotherNumber - 1)>
  541. ! !
  542. !Array methodsFor: 'comparing'!
  543. = aCollection
  544. (self class = aCollection class and: [
  545. self size = aCollection size]) ifFalse: [^false].
  546. self withIndexDo: [:each :i |
  547. (aCollection at: i) = each ifFalse: [^false]].
  548. ^true
  549. ! !
  550. !Array methodsFor: 'converting'!
  551. asJavascript
  552. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  553. !
  554. reversed
  555. <return self._copy().reverse()>
  556. ! !
  557. !Array methodsFor: 'copying'!
  558. shallowCopy
  559. | newCollection |
  560. newCollection := self class new.
  561. self do: [:each | newCollection add: each].
  562. ^newCollection
  563. !
  564. deepCopy
  565. | newCollection |
  566. newCollection := self class new.
  567. self do: [:each | newCollection add: each deepCopy].
  568. ^newCollection
  569. !
  570. copyFrom: anIndex to: anotherIndex
  571. | array |
  572. array := self class new.
  573. anIndex to: anotherIndex do: [:each |
  574. array add: (self at: each)].
  575. ^array
  576. ! !
  577. !Array methodsFor: 'enumerating'!
  578. join: aString
  579. <return self.join(aString)>
  580. !
  581. sort
  582. ^self basicPerform: 'sort'
  583. !
  584. sort: aBlock
  585. <
  586. return self.sort(function(a, b) {
  587. if(aBlock(a,b)) {return -1} else {return 1}
  588. })
  589. >
  590. !
  591. sorted
  592. ^self copy sort
  593. !
  594. sorted: aBlock
  595. ^self copy sort: aBlock
  596. !
  597. printString
  598. | str |
  599. str := '' writeStream.
  600. str nextPutAll: super printString, ' ('.
  601. self
  602. do: [:each | str nextPutAll: each printString]
  603. separatedBy: [str nextPutAll: ' '].
  604. str nextPutAll: ')'.
  605. ^str contents
  606. ! !
  607. Object subclass: #RegularExpression
  608. instanceVariableNames: ''
  609. category: 'Kernel-Collections'!
  610. !RegularExpression methodsFor: 'evaluating'!
  611. compile: aString
  612. <return self.compile(aString)>
  613. !
  614. exec: aString
  615. <return self.exec(aString) || nil>
  616. !
  617. test: aString
  618. <return self.test(aString)>
  619. ! !
  620. !RegularExpression class methodsFor: 'instance creation'!
  621. fromString: aString flag: anotherString
  622. <return new RegExp(aString, anotherString)>
  623. !
  624. fromString: aString
  625. ^self fromString: aString flag: ''
  626. ! !
  627. Object subclass: #Association
  628. instanceVariableNames: 'key value'
  629. category: 'Kernel-Collections'!
  630. !Association methodsFor: 'accessing'!
  631. key: aKey
  632. key := aKey
  633. !
  634. key
  635. ^key
  636. !
  637. value: aValue
  638. value := aValue
  639. !
  640. value
  641. ^value
  642. ! !
  643. !Association methodsFor: 'comparing'!
  644. = anAssociation
  645. ^self class = anAssociation class and: [
  646. self key = anAssociation key and: [
  647. self value = anAssociation value]]
  648. !
  649. storeOn: aStream
  650. "Store in the format (key->value)"
  651. "aStream nextPutAll: '('."
  652. key storeOn: aStream.
  653. aStream nextPutAll: '->'.
  654. value storeOn: aStream.
  655. "aStream nextPutAll: ')'"
  656. ! !
  657. !Association class methodsFor: 'instance creation'!
  658. key: aKey value: aValue
  659. ^self new
  660. key: aKey;
  661. value: aValue;
  662. yourself
  663. ! !
  664. Object subclass: #Stream
  665. instanceVariableNames: 'collection position streamSize'
  666. category: 'Kernel-Collections'!
  667. !Stream methodsFor: 'accessing'!
  668. collection
  669. ^collection
  670. !
  671. setCollection: aCollection
  672. collection := aCollection
  673. !
  674. position
  675. ^position ifNil: [position := 0]
  676. !
  677. position: anInteger
  678. position := anInteger
  679. !
  680. streamSize
  681. ^streamSize
  682. !
  683. setStreamSize: anInteger
  684. streamSize := anInteger
  685. !
  686. contents
  687. ^self collection
  688. copyFrom: 1
  689. to: self streamSize
  690. !
  691. size
  692. ^self streamSize
  693. ! !
  694. !Stream methodsFor: 'actions'!
  695. reset
  696. self position: 0
  697. !
  698. close
  699. !
  700. flush
  701. !
  702. resetContents
  703. self reset.
  704. self setStreamSize: 0
  705. ! !
  706. !Stream methodsFor: 'enumerating'!
  707. do: aBlock
  708. [self atEnd] whileFalse: [aBlock value: self next]
  709. ! !
  710. !Stream methodsFor: 'positioning'!
  711. setToEnd
  712. self position: self size
  713. !
  714. skip: anInteger
  715. self position: ((self position + anInteger) min: self size max: 0)
  716. ! !
  717. !Stream methodsFor: 'reading'!
  718. next
  719. self position: self position + 1.
  720. ^collection at: self position
  721. !
  722. next: anInteger
  723. | tempCollection |
  724. tempCollection := self collection class new.
  725. anInteger timesRepeat: [
  726. self atEnd ifFalse: [
  727. tempCollection add: self next]].
  728. ^tempCollection
  729. !
  730. peek
  731. ^self atEnd ifFalse: [
  732. self collection at: self position + 1]
  733. ! !
  734. !Stream methodsFor: 'testing'!
  735. atEnd
  736. ^self position = self size
  737. !
  738. atStart
  739. ^self position = 0
  740. !
  741. isEmpty
  742. ^self size = 0
  743. ! !
  744. !Stream methodsFor: 'writing'!
  745. nextPut: anObject
  746. self position: self position + 1.
  747. self collection at: self position put: anObject.
  748. self setStreamSize: (self streamSize max: self position)
  749. !
  750. nextPutAll: aCollection
  751. aCollection do: [:each |
  752. self nextPut: each]
  753. ! !
  754. !Stream class methodsFor: 'instance creation'!
  755. on: aCollection
  756. ^self new
  757. setCollection: aCollection;
  758. setStreamSize: aCollection size;
  759. yourself
  760. ! !
  761. Stream subclass: #StringStream
  762. instanceVariableNames: ''
  763. category: 'Kernel-Collections'!
  764. !StringStream methodsFor: 'reading'!
  765. next: anInteger
  766. | tempCollection |
  767. tempCollection := self collection class new.
  768. anInteger timesRepeat: [
  769. self atEnd ifFalse: [
  770. tempCollection := tempCollection, self next]].
  771. ^tempCollection
  772. ! !
  773. !StringStream methodsFor: 'writing'!
  774. nextPut: aString
  775. self nextPutAll: aString
  776. !
  777. nextPutAll: aString
  778. self setCollection:
  779. (self collection copyFrom: 1 to: self position),
  780. aString,
  781. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  782. self position: self position + aString size.
  783. self setStreamSize: (self streamSize max: self position)
  784. !
  785. cr
  786. ^self nextPutAll: String cr
  787. !
  788. crlf
  789. ^self nextPutAll: String crlf
  790. !
  791. lf
  792. ^self nextPutAll: String lf
  793. !
  794. space
  795. self nextPut: ' '
  796. ! !
  797. Collection subclass: #Set
  798. instanceVariableNames: 'elements'
  799. category: 'Kernel-Collections'!
  800. !Set methodsFor: 'accessing'!
  801. size
  802. ^elements size
  803. ! !
  804. !Set methodsFor: 'adding/removing'!
  805. add: anObject
  806. <
  807. var found;
  808. for(var i in self['@elements']) {
  809. if(anObject == self['@elements'][i]) {
  810. found = true;
  811. break;
  812. }
  813. }
  814. if(!!found) {self['@elements'].push(anObject)}
  815. >
  816. !
  817. remove: anObject
  818. elements remove: anObject
  819. ! !
  820. !Set methodsFor: 'comparing'!
  821. = aCollection
  822. ^self class = aCollection class and: [
  823. elements = aCollection asArray]
  824. ! !
  825. !Set methodsFor: 'converting'!
  826. asArray
  827. ^elements copy
  828. ! !
  829. !Set methodsFor: 'enumerating'!
  830. detect: aBlock ifNone: anotherBlock
  831. ^elements detect: aBlock ifNone: anotherBlock
  832. !
  833. do: aBlock
  834. elements do: aBlock
  835. ! !
  836. !Set methodsFor: 'initialization'!
  837. initialize
  838. super initialize.
  839. elements := #()
  840. ! !
  841. !Set methodsFor: 'testing'!
  842. includes: anObject
  843. ^elements includes: anObject
  844. ! !
  845. Collection subclass: #HashedCollection
  846. instanceVariableNames: ''
  847. category: 'Kernel-Collections'!
  848. !HashedCollection methodsFor: 'accessing'!
  849. size
  850. ^self keys size
  851. !
  852. associations
  853. | associations |
  854. associations := #().
  855. self keys do: [:each |
  856. associations add: (Association key: each value: (self at: each))].
  857. ^associations
  858. !
  859. keys
  860. <
  861. var keys = [];
  862. for(var i in self) {
  863. if(self.hasOwnProperty(i)) {
  864. keys.push(i);
  865. }
  866. };
  867. return keys;
  868. >
  869. !
  870. values
  871. ^self keys collect: [:each | self at: each]
  872. !
  873. at: aKey put: aValue
  874. ^self basicAt: aKey put: aValue
  875. !
  876. at: aKey ifAbsent: aBlock
  877. ^(self includesKey: aKey)
  878. ifTrue: [self basicAt: aKey]
  879. ifFalse: aBlock
  880. !
  881. at: aKey ifAbsentPut: aBlock
  882. ^self at: aKey ifAbsent: [
  883. self at: aKey put: aBlock value]
  884. !
  885. at: aKey ifPresent: aBlock
  886. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  887. !
  888. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  889. ^(self basicAt: aKey)
  890. ifNil: anotherBlock
  891. ifNotNil: [aBlock value: (self at: aKey)]
  892. !
  893. at: aKey
  894. ^self at: aKey ifAbsent: [self errorNotFound]
  895. ! !
  896. !HashedCollection methodsFor: 'adding/removing'!
  897. add: anAssociation
  898. self at: anAssociation key put: anAssociation value
  899. !
  900. addAll: aHashedCollection
  901. super addAll: aHashedCollection associations.
  902. ^aHashedCollection
  903. !
  904. removeKey: aKey
  905. self remove: aKey
  906. !
  907. remove: aKey ifAbsent: aBlock
  908. ^self removeKey: aKey ifAbsent: aBlock
  909. !
  910. removeKey: aKey ifAbsent: aBlock
  911. ^(self includesKey: aKey)
  912. ifFalse: [aBlock value]
  913. ifTrue: [self basicDelete: aKey]
  914. ! !
  915. !HashedCollection methodsFor: 'comparing'!
  916. = aHashedCollection
  917. self class = aHashedCollection class ifFalse: [^false].
  918. self size = aHashedCollection size ifFalse: [^false].
  919. ^self associations = aHashedCollection associations
  920. ! !
  921. !HashedCollection methodsFor: 'converting'!
  922. asDictionary
  923. ^Dictionary fromPairs: self associations
  924. ! !
  925. !HashedCollection methodsFor: 'copying'!
  926. shallowCopy
  927. | copy |
  928. copy := self class new.
  929. self associationsDo: [:each |
  930. copy at: each key put: each value].
  931. ^copy
  932. !
  933. , aCollection
  934. self shouldNotImplement
  935. !
  936. copyFrom: anIndex to: anotherIndex
  937. self shouldNotImplement
  938. !
  939. deepCopy
  940. | copy |
  941. copy := self class new.
  942. self associationsDo: [:each |
  943. copy at: each key put: each value deepCopy].
  944. ^copy
  945. ! !
  946. !HashedCollection methodsFor: 'enumerating'!
  947. associationsDo: aBlock
  948. self associations do: aBlock
  949. !
  950. keysAndValuesDo: aBlock
  951. self associationsDo: [:each |
  952. aBlock value: each key value: each value]
  953. !
  954. do: aBlock
  955. self values do: aBlock
  956. !
  957. select: aBlock
  958. | newDict |
  959. newDict := self class new.
  960. self keysAndValuesDo: [:key :value |
  961. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  962. ^newDict
  963. !
  964. collect: aBlock
  965. | newDict |
  966. newDict := self class new.
  967. self keysAndValuesDo: [:key :value |
  968. newDict at: key put: (aBlock value: value)].
  969. ^newDict
  970. !
  971. detect: aBlock ifNone: anotherBlock
  972. ^self values detect: aBlock ifNone: anotherBlock
  973. !
  974. includes: anObject
  975. ^self values includes: anObject
  976. ! !
  977. !HashedCollection methodsFor: 'printing'!
  978. printString
  979. ^String streamContents: [:aStream|
  980. aStream
  981. nextPutAll: super printString;
  982. nextPutAll: '('.
  983. self associations
  984. do: [:anAssociation|
  985. aStream
  986. nextPutAll: anAssociation key printString;
  987. nextPutAll: ' -> ';
  988. nextPutAll: anAssociation value printString]
  989. separatedBy: [aStream nextPutAll: ' , '].
  990. aStream nextPutAll: ')']
  991. !
  992. storeOn: aStream
  993. aStream nextPutAll: '#{'.
  994. self associations
  995. do: [:each | each storeOn: aStream]
  996. separatedBy: [ aStream nextPutAll: '. '].
  997. aStream nextPutAll: '}'
  998. ! !
  999. !HashedCollection methodsFor: 'testing'!
  1000. includesKey: aKey
  1001. <return self.hasOwnProperty(aKey)>
  1002. ! !
  1003. !HashedCollection class methodsFor: 'instance creation'!
  1004. fromPairs: aCollection
  1005. | dict |
  1006. dict := self new.
  1007. aCollection do: [:each | dict add: each].
  1008. ^dict
  1009. ! !
  1010. HashedCollection subclass: #Dictionary
  1011. instanceVariableNames: 'keys values'
  1012. category: 'Kernel-Collections'!
  1013. !Dictionary methodsFor: 'accessing'!
  1014. at: aKey ifAbsent: aBlock
  1015. <
  1016. var index;
  1017. for(var i=0;i<self['@keys'].length;i++){
  1018. if(self['@keys'][i].__eq(aKey)) {index = i;}
  1019. };
  1020. if(typeof index === 'undefined') {
  1021. return aBlock();
  1022. } else {
  1023. return self['@values'][index];
  1024. }
  1025. >
  1026. !
  1027. keys
  1028. ^keys copy
  1029. !
  1030. values
  1031. ^values copy
  1032. !
  1033. at: aKey put: aValue
  1034. <
  1035. var index = self['@keys'].indexOf(aKey);
  1036. if(index === -1) {
  1037. self['@values'].push(aValue);
  1038. self['@keys'].push(aKey);
  1039. } else {
  1040. self['@values'][index] = aValue;
  1041. };
  1042. return aValue;
  1043. >
  1044. ! !
  1045. !Dictionary methodsFor: 'adding/removing'!
  1046. removeKey: aKey ifAbsent: aBlock
  1047. <
  1048. var index = self['@keys'].indexOf(aKey);
  1049. if(index === -1) {
  1050. return aBlock()
  1051. } else {
  1052. self['@keys'].splice(i, 1);
  1053. self['@values'].splice(i, 1);
  1054. return aKey
  1055. };
  1056. >
  1057. ! !
  1058. !Dictionary methodsFor: 'converting'!
  1059. asHashedCollection
  1060. ^HashedCollection fromPairs: self associations
  1061. !
  1062. asJSONString
  1063. ^self asHashedCollection asJSONString
  1064. ! !
  1065. !Dictionary methodsFor: 'initialization'!
  1066. initialize
  1067. super initialize.
  1068. keys := #().
  1069. values := #()
  1070. ! !
  1071. !Dictionary methodsFor: 'testing'!
  1072. includesKey: aKey
  1073. ^keys includes: aKey
  1074. ! !