⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kcollect.st

📁 四皇后问题
💻 ST
字号:
Class KeyedCollection :Collection
[
	add: anElement
		^ self error: 'Must add with explicit key'
|
	addAll: aCollection
                aCollection binaryDo: [:x :y | self at: x put: y].
                ^ aCollection
|
	asDictionary			| newCollection |
		newCollection <- Dictionary new.
		self binaryDo: 
			[:key :val | newCollection at: key put: val].
		^ newCollection
|
	at: key
                ^ self at: key ifAbsent:
                   [self error:
                         (key printString , ': association not found').
                    ^ key]
|
	atAll: aCollection put: anObject
		aCollection do: [:x | self at: x put: anObject]
|
	binaryDo: aBlock                | item |
		self do: [:x | aBlock value: self currentKey
					value: x ].
                ^ nil
|
	coerce: aCollection	| newobj |
		newobj <- self class new.
		aCollection binaryDo: [:x :y | newobj at: x put: y].
		^ newobj
|
	collect: aBlock 
		^ self coerce:
		     (self inject: Dictionary new
                           into: [:x :y | x at: self currentKey
					    put: (aBlock value: y) . x ] )
|
	includesKey: key
                self at: key ifAbsent: [^ false].
                ^ true
|
	indexOf: anElement
		^ self indexOf: anElement
		ifAbsent: [self error: 'indexOf element not found']
|
	indexOf: anElement ifAbsent: exceptionBlock
                self do: [:x | (x = anElement) 
  				  ifTrue: [ ^ self currentKey ]].
                 ^ exceptionBlock value
|
	keys                             | newset |
                newset <- Set new.
                self keysDo: [:x | newset add: x].
                ^ newset
|
	keysDo: aBlock
                ^ self do: [ :x | aBlock value: self currentKey ]
|
	keysSelect: aBlock          
		^ self coerce:
		     (self inject: Dictionary new
                           into: [:x :y | (aBlock value: y currentKey)
                                           ifTrue: [x at: self currentKey
                                                      put: y]. x ] )
|
	remove: anElement
		^ self error: 'object must be removed with explicit key'
|
	removeKey: key
                ^ self removeKey: key ifAbsent:
                   [self error: 'no element associated with key'. ^ key]
|
	removeKey: key ifAbsent: exceptionBlock
		^ self error: 'subclass should implement RemoveKey:ifAbsent:'
|
	select: aBlock          
		^ self coerce:
		     (self inject: Dictionary new
                           into: [:x :y | (aBlock value: y)
                                           ifTrue: [x at: self currentKey
                                                      put: y]. x ] )
|
	values                           | newbag |
                newbag <- Bag new.
                self do: [:x | newbag add: x].
                ^ newbag
]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -