You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ko/overviews/collections/sets.md
+31-31Lines changed: 31 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -61,25 +61,25 @@ language: ko
61
61
| 사용법 | 하는일 |
62
62
| ------ | ------ |
63
63
|**추가:**||
64
-
|`xs += x`|Adds element `x` to set `xs` as a side effect and returns `xs`itself.|
65
-
|`xs += (x, y, z)`|Adds the given elements to set `xs` as a side effect and returns `xs`itself.|
66
-
|`xs ++= ys`|Adds all elements in `ys` to set `xs` as a side effect and returns `xs`itself.|
67
-
|`xs add x`|Adds element `x` to `xs` and returns `true` if `x` was not previously contained in the set, `false` if it was.|
64
+
|`xs += x`|`xs`에 원소 `x`를 부작용을 사용해 추가하고, `xs`자신을 반환한다.|
65
+
|`xs += (x, y, z)`|`xs`에 지정된 원소들을 부작용을 사용해 추가하고, `xs`자신을 반환한다.|
66
+
|`xs ++= ys`|`xs`에 `ys`의 원소들을 부작용을 사용해 추가하고, `xs`자신을 반환한다.|
67
+
|`xs add x`|`xs`에 원소 `x`를 부작용을 사용해 추가하되, `x`가 집합에 이미 포함되어 있었다면 거짓을, 그렇지 않았다면 참을 반환한다.|
68
68
|**제거:**||
69
-
|`xs -= x`|Removes element `x` from set `xs` as a side effect and returns `xs`itself.|
70
-
|`xs -= (x, y, z)`|Removes the given elements from set `xs` as a side effect and returns `xs`itself.|
71
-
|`xs --= ys`|Removes all elements in `ys` from set `xs` as a side effect and returns `xs`itself.|
72
-
|`xs remove x`|Removes element `x` from `xs` and returns `true` if `x` was previously contained in the set, `false` if it was not.|
73
-
|`xs retain p`|Keeps only those elements in `xs` that satisfy predicate `p`.|
74
-
|`xs.clear()`|Removes all elements from `xs`.|
69
+
|`xs -= x`|`xs`에서 원소 `x`를 부작용을 사용해 제거하고, `xs`자신을 반환한다.|
70
+
|`xs -= (x, y, z)`|`xs`에서 지정된 원소들을 부작용을 사용해 제거하고, `xs`자신을 반환한다.|
71
+
|`xs --= ys`|`xs`에서 `ys`의 원소들을 부작용을 사용해 제거하고, `xs`자신을 반환한다.|
72
+
|`xs remove x`|`xs`에서 원소 `x`를 부작용을 사용해 제거하되, `x`가 집합에 이미 포함되어 있었다면 참을, 그렇지 않았다면 거짓을 반환한다.|
73
+
|`xs retain p`|`xs`에서 술어 `p`를 만족하는 원소를 남기고 나머지를 제거한다.|
74
+
|`xs.clear()`|`xs`의 모든 원소를 제거한다.|
75
75
|**변경:**||
76
-
|`xs(x) = b`|(or, written out, `xs.update(x, b)`). If boolean argument`b` is `true`, adds `x` to `xs`, otherwise removes`x` from `xs`.|
76
+
|`xs(x) = b`|(명시적으로 `xs.update(x, b)`라고 쓸 수 있음)`b`가 `true`면 `x`를 `xs`에 추가하고, 그렇지 않으면`x`를 `xs`에서 제거한다.|
77
77
|**복제:**||
78
-
|`xs.clone`|A new mutable set with the same elements as `xs`.|
78
+
|`xs.clone`|`xs`와 같은 원소를 포함하는 새 변경 가능한 집합을 만든다.|
79
79
80
-
Just like an immutable set, a mutable set offers the `+` and `++` operations for element additions and the `-` and `--`operations for element removals. But these are less often used for mutable sets since they involve copying the set. As a more efficient alternative, mutable sets offer the update methods `+=` and `-=`. The operation `s += elem` adds `elem` to the set `s` as a side effect, and returns the mutated set as a result. Likewise, `s -= elem` removes`elem` from the set, and returns the mutated set as a result. Besides `+=` and `-=` there are also the bulk operations `++=` and `--=`which add or remove all elements of a traversable or an iterator.
80
+
변경 불가능한 집합과 마찬가지로 변경가능한 집합도 원소 추가를 위한 `+`, `++`와 원소 제거를 위한 `-`, `--`연산을 제공한다. 하지만 이 연산들은 집합을 복사하기 때문에 변경 가능한 집합에서는 잘 사용되지 않는다. 더 효율적인 방식으로 `+=`, `-=`가 있다. `s += elem`는 `elem`을 집합 `s`에 부작용을 통해 추가하며, 결과로 집합 자신을 반환한다. 마찬가지로, `s -= elem`은 원소`elem`을 집합에서 제거하고, 집합 자신을 결과로 반환한다. `+=`와 `-=`와 별개로 반복자나 순회가능 클래스의 원소를 한꺼번에 추가, 삭제하는 `++=`와 `--=`연산도 있다.
81
81
82
-
The choice of the method names `+=` and `-=` means that very similar code can work with either mutable or immutable sets. Consider first the following REPL dialogue which uses an immutable set `s`:
82
+
메소드 이름 `+=`와 `-=`는 변경 가능하거나 불가능한 집합 모두에 아주 비슷한 코드를 사용할 수 있음을 의미한다. 먼저 변경 불가능한 집합 `s`를 사용하는 REPL 실행예를 보자.
We used `+=` and `-=` on a `var` of type `immutable.Set`. A statement such as `s += 4`is an abbreviation for `s = s + 4`. So this invokes the addition method `+` on the set `s` and then assigns the result back to the `s` variable. Consider now an analogous interaction with a mutable set.
91
+
여기서 타입이 `immutable.Set`인 `var`에 `+=`와 `-=` 연산을 적용했다. `s += 4`식은 `s = s + 4`을 줄인 것이다. 따라서 집합 `s`에 대해 추가 메소드 `+`가 호출 된 다음 이 결과가 다시 변수 `s`에 대입된다. 이제 이와 비슷한 변경 가능한 집합의 동작을 살펴보자.
92
92
93
93
94
94
scala> val s = collection.mutable.Set(1, 2, 3)
@@ -98,53 +98,53 @@ We used `+=` and `-=` on a `var` of type `immutable.Set`. A statement such as `s
98
98
scala> s -= 2
99
99
res4: s.type = Set(1, 4, 3)
100
100
101
-
The end effect is very similar to the previous interaction; we start with a `Set(1, 2, 3)` end end up with a `Set(1, 3, 4)`. However, even though the statements look the same as before, they do something different. `s += 4` now invokes the `+=` method on the mutable set value `s`, changing the set in place. Likewise,`s -= 2`now invokes the`-=`method on the same set.
101
+
최종 결과는 앞의 예와 아주 비슷하다. `Set(1, 2, 3)`에서 시작해서 `Set(1, 3, 4)`으로 끝난다. 비록 사용된 명령은 앞에서와 같지만 실제 하는 일은 많이 다른 것이다. `s += 4`는 이제 변경 가능한 집합 `s`의 `+=` 메소드를 호출해 집합의 내부 상태를 변경한다. 마찬가지로`s -= 2`또한 같은 집합의`-=`메소드를 호출한다.
102
102
103
-
Comparing the two interactions shows an important principle. You often can replace a mutable collection stored in a `val` by an immutable collection stored in a `var`, and _vice versa_. This works at least as long as there are no alias references to the collection through which one can observe whether it was updated in place or whether a new collection was created.
103
+
이 둘 간의 차이는 중요한 원칙을 보여준다. 바로 때로 `val`에 저장된 변경 가능한 컬렉션을 `var`에 저장된 변경 불가능한 것으로 바꾸거나, 그 _역으로_ 바꾸는 것이 가능하다는 사실이다. 외부에서 새 컬렉션이 만들어졌거나 내용이 부작용을 통해 변경되었는지를 관찰할 수 있는 동일 객체에 대한 다른 이름의 참조(alias)가 존재하지 않는 한 이 방식은 잘 동작한다.
104
104
105
-
Mutable sets also provide add and remove as variants of `+=` and `-=`. The difference is that `add` and `remove` return a Boolean result indicating whether the operation had an effect on the set.
105
+
변경 가능한 집합은 또한 `+=`와 `-=`의 변형으로 `add`와 `remove`도 제공한다. 차이는 `add`와 `remove`는 연산에 집합에 작용했는지 여부를 알려주는 불린 값을 돌려준다는 점에 있다.
106
106
107
-
The current default implementation of a mutable set uses a hashtable to store the set's elements. The default implementation of an immutable set uses a representation that adapts to the number of elements of the set. An empty set is represented by just a singleton object. Sets of sizes up to four are represented by a single object that stores all elements as fields. Beyond that size, immutable sets are implemented as [hash tries](#hash-tries).
107
+
현재 변경 가능 집합의 기본 구현은 해시 테이블을 사용해 원소를 저장한다. 변경 불가능한 집합의 기본 구현은 원소의 갯수에 따라 다른 표현방식을 사용한다. 빈 집합은 싱글턴 객체로 표현된다. 원소가 4개 이하인 집합은 모든 원소 객체를 필드로 저장하는 객체로 표현한다. 그보다 큰 변경 불가능 집합은 [해시 트라이(hash trie)](#hash-tries)로 구현되어 있다.
108
108
109
-
A consequence of these representation choices is that, for sets of small sizes (say up to 4), immutable sets are usually more compact and also more efficient than mutable sets. So, if you expect the size of a set to be small, try making it immutable.
109
+
이런 구현의 차이로 인해 더 작은 크기(4 이하)인 경우 변경 불가능한 집합이 변경 가능한 집합보다 더 작고 효율적이다. 따라서 크기가 작은 집합을 예상한다면 변경 불가능한 집합을 사용하도록 하라.
110
110
111
-
Two subtraits of sets are `SortedSet` and `BitSet`.
111
+
집합에는 `SortedSet(정렬된 집합)`과 `BitSet(비트집합)`이 있다.
112
112
113
-
### Sorted Sets ###
113
+
### 정렬된 집합 ###
114
114
115
-
A [SortedSet](http://www.scala-lang.org/api/current/scala/collection/SortedSet.html) is a set that produces its elements (using `iterator` or `foreach`) in a given ordering (which can be freely chosen at the time the set is created). The default representation of a [SortedSet](http://www.scala-lang.org/api/current/scala/collection/SortedSet.html) is an ordered binary tree which maintains the invariant that all elements in the left subtree of a node are smaller than all elements in the right subtree. That way, a simple in order traversal can return all tree elements in increasing order. Scala's class[immutable.TreeSet](http://www.scala-lang.org/api/current/scala/collection/immutable/TreeSet.html) uses a _red-black_ tree implementation to maintain this ordering invariant and at the same time keep the tree _balanced_-- meaning that all paths from the root of the tree to a leaf have lengths that differ only by at most one element.
115
+
[SortedSet(정렬된 집합)](http://www.scala-lang.org/api/current/scala/collection/SortedSet.html)은 주어진 순서에 따라 원소를 내어놓는(`iterator`나 `foreach` 사용) 집합이다(순서는 집합을 만들 때 자유롭게 결정할 수 있다). [SortedSet](http://www.scala-lang.org/api/current/scala/collection/SortedSet.html)의 기본 구현은 어떤 노드의 왼쪽 하위 트리에 속한 모든 원소가 오른쪽 하위 트리에 속한 모든 원소보다 작다는 불변조건(invariant)을 만족시키는 순서가 있는 이진 트리이다. 따라서 간단한 중위순회(in order traversal)를 통해 트리의 모든 원소를 증가하는 순서로 반환할 수 있다. 스칼라의 클래스 [immutable.TreeSet(트리집합)](http://www.scala-lang.org/api/current/scala/collection/immutable/TreeSet.html)은 이 불변조건을 유지하면서 동시에 _균형잡힌(balanced)_ 특성을 유지하기 위해 _적-흑(red-black)_ 트리를 구현한다. 균형이 잡힌 트리는 루트(root) 노드로부터 리프(leaf) 노드에 이르는 길이가 1 이하로 차이가 나는 경우를 말한다.
116
116
117
-
To create an empty [TreeSet](http://www.scala-lang.org/api/current/scala/collection/immutable/TreeSet.html), you could first specify the desired ordering:
117
+
빈 [TreeSet(트리집합)](http://www.scala-lang.org/api/current/scala/collection/immutable/TreeSet.html)을 만들려면 우선 원하는 순서를 지정해야 한다.
118
118
119
119
scala> val myOrdering = Ordering.fromLessThan[String](_ > _)
120
120
myOrdering: scala.math.Ordering[String] = ...
121
121
122
-
Then, to create an empty tree set with that ordering, use:
Or you can leave out the ordering argument but give an element type or the empty set. In that case, the default ordering on the element type will be used.
127
+
트리의 순서를 지정하지 않는 대신 원소의 타입을 지정해 빈 트리를 만들 수도 있다. 그렇게 하면 원소의 타입에 따른 기본 순서를 사용하게 된다.
If you create new sets from a tree-set (for instance by concatenation or filtering) they will keep the same ordering as the original set. For instance,
132
+
트리 집합으로부터 (트리를 서로 붙이거나, 걸러내는 등의 방법을 사용해) 새 집합을 만드는 경우, 원래의 집합과 같은 순서를 사용할 것이다. 예를 들면 다음과 같다.
Sorted sets also support ranges of elements. For instance, the `range`method returns all elements from a starting element up to, but excluding, and end element. Or, the `from`method returns all elements greater or equal than a starting element in the set's ordering. The result of calls to both methods is again a sorted set. Examples:
137
+
정렬된 집합은 원소의 범위도 지원한다. 예를 들어 `range`메소드는 지정된 시작 원소로부터 시작해서 지정된 끝 엘리먼트 직전까지의 모든 원소를 반환한다. `from`메소드는 집합에서의 순서상 지정된 원소와 같거나 큰 모든 원소를 반환한다. 다음은 그 예이다.
Bitsets are sets of non-negative integer elements that are implemented in one or more words of packed bits. The internal representation of a [BitSet](http://www.scala-lang.org/api/current/scala/collection/BitSet.html) uses an array of `Long`s. The first `Long` covers elements from 0 to 63, the second from 64 to 127, and so on (Immutable bitsets of elements in the range of 0 to 127 optimize the array away and store the bits directly in a one or two `Long` fields.) For every `Long`, each of its 64 bits is set to 1 if the corresponding element is contained in the set, and is unset otherwise. It follows that the size of a bitset depends on the largest integer that's stored in it. If `N` is that largest integer, then the size of the set is `N/64``Long` words, or `N/8`bytes, plus a small number of extra bytes for status information.
147
+
[BitSet(비트집합)](http://www.scala-lang.org/api/current/scala/collection/BitSet.html)은 음이 아닌 정수 원소들로 이루어진 집합으로, 비트를 한데 묶은(packed) 하나 또는 그 이상의 워드로 되어 있다. [BitSet(비트집합)](http://www.scala-lang.org/api/current/scala/collection/BitSet.html)의 내부 표현은 `Long`의 배열을 사용한다. 첫 `Long`은 0부터 63을 나타내고, 두번째 것은 64부터 127을 나타내는 방식을 사용한다(0부터 127 사이의 수만 표현하는 변경 불가능한 비트셋은 배열을 최적화해서 한두개의 `Long` 필드만을 사용한다). 어떤 원소가 속해 있다면 해당 원소에 대응하는 `Long` 필드내의 비트가 1로 설정되고, 그렇지 않으면 0으로 설정된다. 따라서 비트집합의 크기는 내부에 저장되는 가장 큰 정수의 값에 따라 결정된다. 만약 가장 큰 정수가 `N`이라면 집합의 크기는 `N/64`개의 `Long`워드 또는 `N/8` 바이트이며, 상태정보 저장을 위해 몇 바이트가 추가된다.
148
148
149
-
Bitsets are hence more compact than other sets if they contain many small elements. Another advantage of bitsets is that operations such as membership test with `contains`, or element addition and removal with `+=` and `-=`are all extremely efficient.
149
+
따라서 크기가 작은 정수 원소를 여러개 포함하는 경우 비트집합을 사용하면 다른 집합에 비해 작은 크기로 가능하다. 비트 집합의 또 다른 잇점은 `contains`를 사용한 포함관계 검사나 `+=`, `-=`등을 사용한 원소 추가/제거가 모두 아주 효율적이라는 점이다.
0 commit comments