Skip to content

Commit b34f722

Browse files
committed
Merge branch 'dev'
2 parents 575a834 + 17ff169 commit b34f722

File tree

10 files changed

+310
-0
lines changed

10 files changed

+310
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.silently9527.unionfind;
2+
3+
public abstract class AbstractUF implements UF {
4+
protected int[] id;
5+
protected int count;
6+
7+
public AbstractUF(int N) {
8+
count = N;
9+
10+
id = new int[N];
11+
for (int i = 0; i < N; i++) {
12+
id[i] = i;
13+
}
14+
}
15+
16+
@Override
17+
public boolean connected(int p, int q) {
18+
return find(p) == find(q);
19+
}
20+
21+
@Override
22+
public int count() {
23+
return count;
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.silently9527.unionfind;
2+
3+
public class QuickFindImpl extends AbstractUF {
4+
public QuickFindImpl(int N) {
5+
super(N);
6+
}
7+
8+
@Override
9+
public int find(int p) {
10+
return id[p];
11+
}
12+
13+
@Override
14+
public void union(int p, int q) {
15+
int pId = find(p);
16+
int qId = find(q);
17+
18+
if (pId == qId) { //如果相等表示p与q已经属于同一分量中
19+
return;
20+
}
21+
22+
for (int i = 0; i < id.length; i++) {
23+
if (id[i] == pId) {
24+
id[i] = qId; //把分量中所有的值都统一成qId
25+
}
26+
}
27+
count--; //连通分量数减一
28+
}
29+
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.silently9527.unionfind;
2+
3+
public class QuickUnionImpl extends AbstractUF {
4+
public QuickUnionImpl(int N) {
5+
super(N);
6+
}
7+
8+
@Override
9+
public int find(int p) {
10+
//找出p所在分量的根触点
11+
while (p != id[p]) {
12+
p = id[p];
13+
}
14+
return p;
15+
}
16+
17+
@Override
18+
public void union(int p, int q) {
19+
int pRoot = find(p); //找出q p的根触点
20+
int qRoot = find(q);
21+
if (pRoot == qRoot) { //处于同一分量不做处理
22+
return;
23+
}
24+
id[pRoot] = qRoot; //根节点
25+
count--;
26+
}
27+
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.silently9527.unionfind;
2+
3+
public interface UF {
4+
void union(int p, int q); //在p与q之间添加一条连接
5+
6+
int find(int p); //找出p所在分量的标识符
7+
8+
boolean connected(int p, int q); //判断出p与q是否存在于同一个分量中
9+
10+
int count(); //统计出连通分量的数量
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.silently9527.unionfind;
2+
3+
public class WeightedQuickUnionImpl extends AbstractUF {
4+
private int[] sz;
5+
6+
public WeightedQuickUnionImpl(int N) {
7+
super(N);
8+
sz = new int[N];
9+
for (int i = 0; i < N; i++) {
10+
sz[i] = 1;
11+
}
12+
}
13+
14+
@Override
15+
public void union(int p, int q) {
16+
int pRoot = find(p); //找出q p的根触点
17+
int qRoot = find(q);
18+
if (pRoot == qRoot) { //处于同一分量不做处理
19+
return;
20+
}
21+
//小树合并到大树
22+
if (sz[pRoot] < sz[qRoot]) {
23+
sz[qRoot] += sz[pRoot];
24+
id[pRoot] = qRoot;
25+
} else {
26+
sz[pRoot] += sz[qRoot];
27+
id[qRoot] = pRoot;
28+
}
29+
count--;
30+
}
31+
32+
@Override
33+
public int find(int p) {
34+
//找出p所在分量的根触点
35+
while (p != id[p]) {
36+
p = id[p];
37+
}
38+
return p;
39+
}
40+
}

Netty/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cn.silently9527</groupId>
8+
<artifactId>Netty</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>io.netty</groupId>
14+
<artifactId>netty-all</artifactId>
15+
<version>4.1.51.Final</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>1.8</source>
26+
<target>1.8</target>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.silently9527.netty;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.channel.socket.nio.NioSocketChannel;
10+
11+
public class EchoClient {
12+
private final String host;
13+
private final int port;
14+
15+
public EchoClient(String host, int port) {
16+
this.host = host;
17+
this.port = port;
18+
}
19+
20+
public void start() throws InterruptedException {
21+
EventLoopGroup group = new NioEventLoopGroup();
22+
try {
23+
Bootstrap bootstrap = new Bootstrap();
24+
bootstrap.group(group)
25+
.channel(NioSocketChannel.class)
26+
.remoteAddress(host, port)
27+
.handler(new ChannelInitializer<SocketChannel>() {
28+
@Override
29+
protected void initChannel(SocketChannel ch) throws Exception {
30+
ch.pipeline().addLast(new EchoClientHandler());
31+
}
32+
});
33+
ChannelFuture future = bootstrap.connect().sync();
34+
future.channel().closeFuture().sync();
35+
} finally {
36+
group.shutdownGracefully().sync();
37+
}
38+
}
39+
40+
public static void main(String[] args) throws InterruptedException {
41+
new EchoClient("localhost", 8080).start();
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.silently9527.netty;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.Unpooled;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.SimpleChannelInboundHandler;
7+
import io.netty.util.CharsetUtil;
8+
9+
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
10+
11+
@Override
12+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
13+
ctx.writeAndFlush(Unpooled.copiedBuffer("Hello, I am a client", CharsetUtil.UTF_8));
14+
}
15+
16+
@Override
17+
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
18+
System.out.println("Client received:" + msg.toString(CharsetUtil.UTF_8));
19+
}
20+
21+
@Override
22+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
23+
cause.printStackTrace();
24+
ctx.close();
25+
}
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.silently9527.netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.channel.socket.nio.NioServerSocketChannel;
10+
11+
public class EchoServer {
12+
private final int port;
13+
14+
public EchoServer(int port) {
15+
this.port = port;
16+
}
17+
18+
public void start() throws InterruptedException {
19+
EchoServerHandler echoServerHandler = new EchoServerHandler();
20+
EventLoopGroup group = new NioEventLoopGroup();
21+
22+
try {
23+
ServerBootstrap bootstrap = new ServerBootstrap();
24+
bootstrap.group(group)
25+
.channel(NioServerSocketChannel.class)
26+
.localAddress(port)
27+
.childHandler(new ChannelInitializer<SocketChannel>() {
28+
@Override
29+
protected void initChannel(SocketChannel ch) throws Exception {
30+
ch.pipeline().addLast(echoServerHandler);
31+
}
32+
});
33+
ChannelFuture channelFuture = bootstrap.bind().sync();
34+
channelFuture.channel().closeFuture().sync();
35+
} finally {
36+
group.shutdownGracefully().sync();
37+
}
38+
39+
}
40+
41+
public static void main(String[] args) throws InterruptedException {
42+
new EchoServer(8080).start();
43+
}
44+
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.silently9527.netty;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.Unpooled;
5+
import io.netty.channel.ChannelFutureListener;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.channel.ChannelInboundHandlerAdapter;
8+
import io.netty.util.CharsetUtil;
9+
10+
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
11+
12+
@Override
13+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
14+
ByteBuf byteBuf = (ByteBuf) msg;
15+
System.out.println("Server received:" + byteBuf.toString(CharsetUtil.UTF_8));
16+
ctx.write(byteBuf);
17+
}
18+
19+
@Override
20+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
21+
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
22+
.addListener(ChannelFutureListener.CLOSE);
23+
}
24+
25+
@Override
26+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
27+
cause.printStackTrace();
28+
ctx.close();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)