Skip to content

Commit e452602

Browse files
committed
implement std::pair
1 parent 69cff1e commit e452602

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Pair/Pair.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Pair<S extends Comparable<S>, T extends Comparable<T>> implements Comparable<Pair<S,T>>{
2+
S first;
3+
T second;
4+
5+
public Pair(S s, T t){
6+
first = s;
7+
second = t;
8+
}
9+
public boolean equals(Object another){
10+
if(this==another) return true;
11+
if(!(another instanceof Pair)) return false;
12+
Pair otherPair = (Pair)another;
13+
return this.first.equals(otherPair.first) && this.second.equals(otherPair.second);
14+
}
15+
public int compareTo(Pair<S,T> another){
16+
if(this.second.compareTo(another.second) == 0) return this.first.compareTo(another.first);
17+
else return this.second.compareTo(another.second);
18+
}
19+
public int hashCode(){
20+
return first.hashCode() * 10007 + second.hashCode();
21+
}
22+
public String toString(){
23+
return String.format("(%s, %s)", first, second);
24+
}
25+
}

Pair/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# クラス Permutation
2+
3+
本機能は AtCoderLibrary ではなく C++標準ライブラリ `std::pair` の移植です.
4+
2要素の組を表現するオブジェクト型です.
5+
6+
## コンストラクタ
7+
8+
### Pair<S extends Comparable<S>, T extends Comparable<T>>
9+
10+
```java
11+
public Pair(S s, T t)
12+
```
13+
2要素 $s, t$ の組を作成します.
14+
15+
## メソッド
16+
17+
### equals
18+
19+
```java
20+
public boolean equals(Object another)
21+
```
22+
この組がオブジェクト `another` と等しい時にtrueを返す関数です.
23+
ここで, Pairどうしが等しいとは, 第一要素どうしが等しくかつ第二要素どうしが等しいことを指します.
24+
25+
26+
### compareTo
27+
28+
```java
29+
public int compareTo(Pair<S,T> another)
30+
```
31+
このオブジェクトと指定されたオブジェクトの順序を比較します.
32+
第一要素どうしが等しくない場合はその順序を, 等しい場合は第二要素どうしの順序を返します(いわゆる辞書順).

0 commit comments

Comments
 (0)