Skip to content

Commit ae3190c

Browse files
authored
Merge pull request onlyliuxin#4 from amorvos/amorvos
作业提交
2 parents 54fe271 + 17c650a commit ae3190c

File tree

18 files changed

+1178
-0
lines changed

18 files changed

+1178
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#指令集
2+
----
3+
通俗的理解,指令集就是CPU能认识的语言,指令集运行于一定的微架构之上.指令集有很多,比如复杂指令集AMD,INTEL用的,精简指令集ARM用的
4+
5+
# CPU
6+
---
7+
CPU使整个计算机的核心,进行整体的逻辑操作,他的运算速度很快,我们在主机上的操作最终都会变成原子指令由CPU来执行
8+
9+
历史上的CPU对程序的执行存在很多的执行方案,比如说时间片法,令牌环之类的的,每个程序依次交换执行,这样就存在一个问题,程序的换入换出需要保留当前执行情况的现场,还要加载历史程序的历史现场
10+
11+
这些数据存储在哪里?由于CPU的速度过快,而我们是用的大规模的数据存储一般是价格较低速度较慢的磁盘.如果说CPU直接从磁盘加载,CPU的资源利用率会变的很低,会严重浪费了CPU的运算能力.因此我们需要缓存
12+
13+
# 缓存
14+
---
15+
缓存的出是为加载相关的数据提供CPU路基运算的存储,CPU中的寄存器就是一种缓存,这是CPU本身对操作的优化.但是说这样高速的缓存本身的制造成本是非常高的,完全使用这样高性能的存储设备并不是一个具有性价比的解决方案
16+
17+
因此出现了多级缓存,常见的如INTEL的L1,L2,L3,这是多级缓存的一种实现,随着层级的升高,存储空间在升高,速度在下将,通过诸如LFU、LRU的缓存置换算法优化相关的缓存命中率提高CPU利用率
18+
19+
理解 Cache --> 传送门 `http://www.mouseos.com/arch/cache.html`
20+
21+
即便是L3的缓存也以就是十分昂贵的,依旧是很难大规模使用的存储设备,因此需要速度相对较快,但是价格又是普通用户可以接收的中间设备,比如内存
22+
23+
# 内存
24+
---
25+
内存的存在实际上解决了很多问题,他的价格相对高速缓存来说相对较低,能够提供较大的存储,也可以看作缓存的一种,将磁盘的数据预加载,供高速缓存加载.
26+
27+
内存有很多的工作频率,内存主频越高在一定程度上代表着内存所能达到的速度越快,计算机系统的时钟速度是以频率来衡量的.晶体振荡器控制着时钟速度,在石英晶片上加上电压,其就以正弦波的形式震动起来,这一震动可以通过晶片的形变和大小记录下来。晶体的震动以正弦调和变化的电流的形式表现出来,这一变化的电流就是时钟信号.
28+
29+
内存是一种断电后存储洗洗就会丢失的存储设备,这就尴尬了,对于宕机我们并无法预测,我们需要将众多的资源数据进行存储,这个时候我们需要硬盘.
30+
31+
# 硬盘
32+
---
33+
硬盘对于我们而言他的速度反而是其次的,他最大的意义是数据的安全性.在数据安全的基础之上我们再去追求硬盘的速度.
34+
35+
硬盘有很多比如说机械硬盘,固态硬盘,PCI硬盘.机械硬盘.机械硬盘的大体结构如下图,数据的读取需要磁头的移动读取扇区上的信息,这时候磁盘的转速就很重要了
36+
![yingpan.jpg-19.4kB][1]
37+
38+
固态硬盘可以看作式闪存的堆积,闪存的制造价格的降低带来了他的春天,但是闪存的数据擦除次数有限,固态硬盘的寿命要注意
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
[1]: http://static.zybuluo.com/Haipop/finuq0bs9p1d90q38bccqjea/yingpan.jpg
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.pop.practice.homework.first.collection;
2+
3+
/**
4+
* @author haipop Date: 17-2-19 Time: 下午3:40
5+
*/
6+
public abstract class AbstractCollection<T> implements Collection<T> {
7+
8+
@Override
9+
@SuppressWarnings("unchecked")
10+
public void addAll(Collection<T> collection) throws IllegalAccessException {
11+
Iterator iterator = collection.iterator();
12+
while (iterator.hasNext()) {
13+
add((T) iterator.next());
14+
}
15+
}
16+
17+
@Override
18+
@SuppressWarnings("unchecked")
19+
public void removeAll(Collection<T> collection) {
20+
Iterator iterator = collection.iterator();
21+
while (iterator.hasNext()) {
22+
remove((T) iterator.next());
23+
}
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.pop.practice.homework.first.collection;
2+
3+
/**
4+
* @author haipop Date: 17-2-16 Time: 下午6:35
5+
*/
6+
public interface Collection<T> extends Iterator<T> {
7+
8+
/**
9+
* 是否为空
10+
*/
11+
boolean isEmpty();
12+
13+
/**
14+
* 获取大小
15+
*/
16+
int size();
17+
18+
/**
19+
* 添加元素
20+
*/
21+
void add(T element) throws IllegalAccessException;
22+
23+
/**
24+
* 批量添加元素
25+
*/
26+
void addAll(Collection<T> collection) throws IllegalAccessException;
27+
28+
/**
29+
* 删除元素
30+
*/
31+
void remove(T element);
32+
33+
/**
34+
* 批量删除元素
35+
*/
36+
void removeAll(Collection<T> collection);
37+
38+
/**
39+
* 元素查找,返回索引,找不到返回-1
40+
*/
41+
int contain(T element);
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.pop.practice.homework.first.collection;
2+
3+
/**
4+
* @author haipop Date: 17-2-16 Time: 下午6:34
5+
*/
6+
public interface Iterator<T> {
7+
8+
/**
9+
* 实例化
10+
*/
11+
Iterator iterator();
12+
13+
/**
14+
* 是否存在下一个
15+
*/
16+
boolean hasNext();
17+
18+
/**
19+
* 获取下一个元素
20+
*/
21+
T next();
22+
23+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package com.pop.practice.homework.first.collection.list;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
import com.pop.practice.homework.first.collection.AbstractCollection;
7+
import com.pop.practice.homework.first.collection.Iterator;
8+
import com.pop.practice.homework.utils.Math;
9+
10+
/**
11+
* @author haipop Date: 17-2-16 Time: 下午6:33
12+
*/
13+
public class ArrayList<T> extends AbstractCollection<T> implements List<T>, Serializable {
14+
15+
private static final long serialVersionUID = -3408657766857424074L;
16+
17+
/**
18+
* 阈值
19+
*/
20+
private static final double THRESHOLD = 0.75F;
21+
22+
/**
23+
* 大小
24+
*/
25+
private int size;
26+
27+
/**
28+
* 当前的存储位置
29+
*/
30+
private int flag;
31+
32+
/**
33+
* 元素集合
34+
*/
35+
private Object[] store;
36+
37+
/**
38+
* 是否容量自增
39+
*/
40+
private boolean autoIncrement;
41+
42+
/**
43+
* 遍历器
44+
*/
45+
private Iterator iterator;
46+
47+
/**
48+
* 默认无参
49+
*/
50+
public ArrayList() {
51+
this.size = 8;
52+
this.flag = 0;
53+
this.autoIncrement = true;
54+
this.store = new Object[this.size];
55+
}
56+
57+
/**
58+
* 指定大小,不可自增
59+
*/
60+
public ArrayList(int size) {
61+
this.size = size;
62+
this.flag = 0;
63+
this.autoIncrement = false;
64+
this.store = new Object[size];
65+
}
66+
67+
@Override
68+
public Iterator iterator() {
69+
this.iterator = new ArrayListIterator();
70+
return iterator;
71+
}
72+
73+
@Override
74+
public boolean hasNext() {
75+
return iterator.hasNext();
76+
}
77+
78+
@Override
79+
@SuppressWarnings("unchecked")
80+
public T next() {
81+
return (T) iterator.next();
82+
}
83+
84+
@Override
85+
public boolean isEmpty() {
86+
return size == 0;
87+
}
88+
89+
@Override
90+
public int size() {
91+
return flag;
92+
}
93+
94+
@Override
95+
public void add(T element) throws IllegalAccessException {
96+
assessStore(flag + 1, size, store, flag);
97+
flag++;
98+
this.store[flag] = element;
99+
}
100+
101+
private void assessStore(int left, int size, Object[] store, int flag) throws IllegalAccessException {
102+
if (!autoIncrement) {
103+
return;
104+
}
105+
double coefficient = Math.div(left, size);
106+
if (coefficient > THRESHOLD) {
107+
// 达到阈值,拓展
108+
Object[] newStore = new Object[this.size * 2];
109+
System.arraycopy(store, 0, newStore, 0, flag);
110+
this.store = newStore;
111+
this.size = size * 2;
112+
}
113+
}
114+
115+
@Override
116+
public void remove(T element) {
117+
for (int i = 0; i < flag; i++) {
118+
if (Objects.equals(this.store[i], element)) {
119+
System.arraycopy(store, i + 1, store, i, flag - i);
120+
flag--;
121+
break;
122+
}
123+
}
124+
}
125+
126+
@Override
127+
public int contain(T element) {
128+
int result = -1;
129+
for (int i = 0; i < flag; i++) {
130+
if (Objects.equals(element, store[i])) {
131+
return i;
132+
}
133+
}
134+
return result;
135+
}
136+
137+
@Override
138+
@SuppressWarnings("uncheckd")
139+
public T get(int index) throws IndexOutOfBoundsException {
140+
return (T) this.store[index];
141+
}
142+
143+
private class ArrayListIterator implements Iterator {
144+
145+
private int index;
146+
147+
ArrayListIterator() {
148+
index = 0;
149+
}
150+
151+
@Override
152+
public Iterator iterator() {
153+
return new ArrayListIterator();
154+
}
155+
156+
@Override
157+
public boolean hasNext() {
158+
return index < size;
159+
}
160+
161+
@Override
162+
public Object next() {
163+
return store[index++];
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)