Skip to content

Commit a3f2532

Browse files
committed
initial commit
0 parents  commit a3f2532

13 files changed

+1448
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.class
2+
*-gcc
3+
*-clang
4+
*-rs
5+
*-nim
6+
*.jar
7+
*.kexe
8+
*-swift
9+
*.pyc
10+
nimcache

Main.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
4+
class SplitResult {
5+
SplitResult(Node lower, Node equal, Node greater) {
6+
this.lower = lower;
7+
this.equal = equal;
8+
this.greater = greater;
9+
}
10+
11+
public Node lower;
12+
public Node equal;
13+
public Node greater;
14+
}
15+
16+
class NodePair {
17+
NodePair(Node first, Node second) {
18+
this.first = first;
19+
this.second = second;
20+
}
21+
22+
public Node first;
23+
public Node second;
24+
}
25+
26+
class Node {
27+
public static Random random = new Random();
28+
29+
Node(int x) {
30+
this.x = x;
31+
}
32+
33+
int x;
34+
int y = random.nextInt();
35+
Node left = null;
36+
Node right = null;
37+
38+
public static Node merge(Node lower, Node greater) {
39+
if (lower == null)
40+
return greater;
41+
42+
if (greater == null)
43+
return lower;
44+
45+
if (lower.y < greater.y) {
46+
lower.right = merge(lower.right, greater);
47+
return lower;
48+
} else {
49+
greater.left = merge(lower, greater.left);
50+
return greater;
51+
}
52+
}
53+
54+
public static NodePair splitBinary(Node orig, int value) {
55+
if (orig == null)
56+
return new NodePair(null, null);
57+
58+
if (orig.x < value) {
59+
NodePair splitPair = splitBinary(orig.right, value);
60+
orig.right = splitPair.first;
61+
return new NodePair(orig, splitPair.second);
62+
} else {
63+
NodePair splitPair = splitBinary(orig.left, value);
64+
orig.left = splitPair.second;
65+
return new NodePair(splitPair.first, orig);
66+
}
67+
}
68+
69+
public static Node merge(Node lower, Node equal, Node greater) {
70+
return merge(merge(lower, equal), greater);
71+
}
72+
73+
public static SplitResult split(Node orig, int value) {
74+
NodePair lowerOther = splitBinary(orig, value);
75+
NodePair equalGreater = splitBinary(lowerOther.second, value + 1);
76+
return new SplitResult(lowerOther.first, equalGreater.first, equalGreater.second);
77+
}
78+
}
79+
80+
class Tree {
81+
public boolean hasValue(int x) {
82+
SplitResult splited = Node.split(mRoot, x);
83+
boolean res = splited.equal != null;
84+
mRoot = Node.merge(splited.lower, splited.equal, splited.greater);
85+
return res;
86+
}
87+
88+
public void insert(int x) {
89+
SplitResult splited = Node.split(mRoot, x);
90+
if (splited.equal == null)
91+
splited.equal = new Node(x);
92+
mRoot = Node.merge(splited.lower, splited.equal, splited.greater);
93+
}
94+
95+
public void erase(int x) {
96+
SplitResult splited = Node.split(mRoot, x);
97+
mRoot = Node.merge(splited.lower, splited.greater);
98+
}
99+
100+
private Node mRoot = null;
101+
}
102+
103+
class Main {
104+
public static void main(String[] args) {
105+
Tree tree = new Tree();
106+
int cur = 5;
107+
int res = 0;
108+
109+
for (int i = 1; i < 1000000; i++) {
110+
int a = i % 3;
111+
cur = (cur * 57 + 43) % 10007;
112+
if (a == 0) {
113+
tree.insert(cur);
114+
} else if (a == 1) {
115+
tree.erase(cur);
116+
} else if (a == 2) {
117+
boolean hasVal = tree.hasValue(cur);
118+
if (hasVal)
119+
res++;
120+
}
121+
}
122+
System.out.println(res);
123+
}
124+
}

README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
| Language | Real Time, seconds | Slowdown Time | Memory, MB | Normalized Memory | Binary Size, MB |
2+
| -------------------------------- | ------------------ | ------------- | ---------- | ----------------- | --------------------------------- |
3+
| C++ "ref-counted" (clang / gcc) | 0.55 | x2.5 | 0.5 | x1.3 | 0.023 + libstdc++ |
4+
| C++ "raw-pointers" (clang / gcc) | 0.22 | x1 | 0.38 | x1 | 0.011 + libstdc++ |
5+
| Rust "ref-counted" | 0.66 | x3 | 0.5 | x1.3 | 0.479 |
6+
| Rust "idiomatic" | 0.37 | x1.7 | 0.5 | x1.3 | 0.475 |
7+
| JavaScript | 1.12 | x5 | 52 | x137 | N/A |
8+
| Java (no-limit / -Xm*50M) | 0.50 / 0.50 | x2.3 | 142 / 29 | x374 / x76 | N/A |
9+
| Kotlin JVM (no-limit / -Xm*50M) | 0.53 / 0.51 | x2.4 | 144 / 30 | x379 / x79 | N/A |
10+
| Kotlin Native | 5.88 | x26.7 | 1.2 | x3.2 | 0.239 |
11+
| Swift | 2.04 | x9.3 | 2.5 | x6.6 | 0.020 + Swift shared libraries |
12+
| Nim | 3.94 | x17.9 | 0.5 | x1.3 | 0.283 |
13+
| Python (CPython 3.6) | 12.25 | x55.7 | 5 | x13 | N/A |
14+
| Python (PyPy 6.0.0) | 3.20 | x14.5 | 48.5 | x128 | N/A |
15+
16+
# Compilation and Run
17+
18+
## C++
19+
20+
```
21+
clang++ -O3 -s -o main-clang main.cpp
22+
```
23+
24+
```
25+
g++ -O3 -s -o main-gcc main.cpp
26+
```
27+
28+
```
29+
./main-clang
30+
```
31+
32+
## Rust
33+
34+
```
35+
rustc +nightly -O -o main-rs main.rs
36+
strip -s main-rs
37+
```
38+
39+
```
40+
./main-rs
41+
```
42+
43+
## JavaScript
44+
45+
```
46+
node main.js
47+
```
48+
49+
## Java
50+
51+
```
52+
javac -g:none Main.java
53+
```
54+
55+
```
56+
java Main
57+
```
58+
59+
```
60+
java -Xms50M -Xmx50M Main
61+
```
62+
63+
## Kotlin JVM
64+
65+
```
66+
kotlinc -include-runtime -d main-kt.jar ./main-jvm.kt
67+
```
68+
69+
```
70+
java -jar main-kt.jar
71+
```
72+
73+
```
74+
java -jar -Xms50M -Xmx50M main-kt.jar
75+
```
76+
77+
## Kotlin Native
78+
79+
```
80+
kotlinc-native -opt -o main-kt main.kt
81+
```
82+
83+
```
84+
./main-kt.kexe
85+
```
86+
87+
## Swift
88+
89+
```
90+
swiftc -O -o main-swift main.swift
91+
strip -s main-swift
92+
```
93+
94+
```
95+
./main-swift
96+
```
97+
98+
## Nim
99+
100+
```
101+
nim compile --opt:speed --out:main-nim main.nim
102+
strip -s main-nim
103+
```
104+
105+
```
106+
./main-nim
107+
```
108+
109+
## Python (CPython)
110+
111+
```
112+
python main.py
113+
```
114+
115+
## Python (PyPy)
116+
117+
```
118+
pypy main.py
119+
```

main-idiomatic.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#![feature(rustc_private)]
2+
extern crate rand;
3+
4+
type NodeCell = Option<Box<Node>>;
5+
6+
struct Node {
7+
x: i32,
8+
y: i32,
9+
left: Option<Box<Node>>,
10+
right: Option<Box<Node>>,
11+
}
12+
13+
impl Node {
14+
fn new(x: i32) -> Self {
15+
Self {
16+
x,
17+
y: rand::random::<i32>(),
18+
left: None,
19+
right: None,
20+
}
21+
}
22+
}
23+
24+
fn merge<'a>(lower: NodeCell, greater: NodeCell) -> NodeCell {
25+
match (lower, greater) {
26+
(None, greater) => greater,
27+
28+
(lower, None) => lower,
29+
30+
(Some(mut lower_node), Some(mut greater_node)) => {
31+
if lower_node.y < greater_node.y {
32+
lower_node.right = merge(lower_node.right.take(), Some(greater_node));
33+
Some(lower_node)
34+
} else {
35+
greater_node.left = merge(Some(lower_node), greater_node.left.take());
36+
Some(greater_node)
37+
}
38+
}
39+
}
40+
}
41+
42+
fn split_binary<'a>(orig: NodeCell, value: i32) -> (NodeCell, NodeCell) {
43+
if let Some(mut orig_node) = orig {
44+
if orig_node.x < value {
45+
let split_pair = split_binary(orig_node.right.take(), value);
46+
orig_node.right = split_pair.0;
47+
(Some(orig_node), split_pair.1)
48+
} else {
49+
let split_pair = split_binary(orig_node.left.take(), value);
50+
orig_node.left = split_pair.1;
51+
(split_pair.0, Some(orig_node))
52+
}
53+
} else {
54+
(None, None)
55+
}
56+
}
57+
58+
fn merge3(lower: NodeCell, equal: NodeCell, greater: NodeCell) -> NodeCell {
59+
merge(merge(lower, equal), greater)
60+
}
61+
62+
struct SplitResult {
63+
lower: NodeCell,
64+
equal: NodeCell,
65+
greater: NodeCell,
66+
}
67+
68+
fn split(orig: NodeCell, value: i32) -> SplitResult {
69+
let (lower, equal_greater) = split_binary(orig, value);
70+
let (equal, greater) = split_binary(equal_greater, value + 1);
71+
SplitResult {
72+
lower,
73+
equal,
74+
greater,
75+
}
76+
}
77+
78+
struct Tree {
79+
root: NodeCell,
80+
}
81+
82+
impl Tree {
83+
pub fn new() -> Self {
84+
Self { root: None }
85+
}
86+
87+
pub fn has_value(&mut self, x: i32) -> bool {
88+
let splited = split(self.root.take(), x);
89+
let res = splited.equal.is_some();
90+
self.root = merge3(splited.lower, splited.equal, splited.greater);
91+
res
92+
}
93+
94+
pub fn insert(&mut self, x: i32) {
95+
let mut splited = split(self.root.take(), x);
96+
if splited.equal.is_none() {
97+
splited.equal = Some(Box::new(Node::new(x)));
98+
}
99+
self.root = merge3(splited.lower, splited.equal, splited.greater);
100+
}
101+
102+
pub fn erase(&mut self, x: i32) {
103+
let splited = split(self.root.take(), x);
104+
self.root = merge(splited.lower, splited.greater);
105+
}
106+
}
107+
108+
fn main() {
109+
let mut tree = Tree::new();
110+
let mut cur = 5;
111+
let mut res = 0;
112+
for i in 1..1000000 {
113+
let a = i % 3;
114+
cur = (cur * 57 + 43) % 10007;
115+
match a {
116+
0 => tree.insert(cur),
117+
1 => tree.erase(cur),
118+
2 => res += if tree.has_value(cur) { 1 } else { 0 },
119+
_ => {}
120+
}
121+
}
122+
println!("{}", res);
123+
}

0 commit comments

Comments
 (0)