forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dracome/master
update from dracome 20170302
- Loading branch information
Showing
21 changed files
with
776 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
现代计算机基本都是冯·诺依曼结构,而冯·诺依曼结构中,最核心的思想便是“存储程序思想”,即:把运算程序存在机器的存储器中。 | ||
|
||
在这种结构下,计算机被分成了五个部分:运算器、控制器、存储器、输入设备和输出设备。其中运算器和控制器构成了CPU的最重要部分。早期的冯·诺依曼结构型计算机是以运算器为核心的,譬如穿孔纸带机,运算核心通过从穿孔纸带中获取控制信息与运算信息完成指定的任务。在这种结构下,任何操作都要先与运算核心打交道。直到后来发明了内存,将大量的控制信息与运算信息存储到集成电路上,让计算机有了飞跃式的发展。内存拥有着大大超过于穿孔纸带的存储效率,于是,计算机的中心慢慢地从运算器转移到了存储器。在这种模式下,运算器、控制器、以至于输入输出设备,都是通过与内存交换信息得到很好的协作。 | ||
|
||
存储器的读写效率确实高,但是它也有着单位造价高和相对容量较小的缺点,虽然现在SSD已经也很便宜了,但是对于以前的工程师来说,这都是不可想象的。于是乎,经过伟大的科学家们与工程师们的探索,他们发现了计算机程序运行过程中的“局部性原理”。“局部性原理”包括时间局部性和空间局部性,时间局部性是指:一段被访问过的程序在不久的时间内很有可能会被再次访问,空间局部性是指:一段被访问过的程序的临近的程序很可能马上被访问到。在有的资料中还提到了顺序局部性,它的意思是指:大部分程序是顺序执行的。在“局部性原理”的指导下,存储分级结构便出现了。存储器被分为若干个级别,越靠近CPU计算速度越快但存储容量小、造价高,如寄存器、Cache,越远离CPU,从内存到硬盘,计算速度越慢,但容量大,造价也越低。采用这种结构,最终使工程师们在性能与造价上得到了很好的平衡。 | ||
|
||
再回到冯·诺依曼结构来,它的“把程序存储起来”到底是什么意思呢?好比我们照着菜谱学做菜,菜谱就是存储好的程序。我们要按照菜谱上的步骤,从选材、买菜、炒菜、出锅、上桌等一系列步骤来达到最终的目标。“存储程序”的思想就是要求把这些基本的步骤存到存储器中,然后当我们要实现某一运算的时候,就将实现这一运算所需要的操作调取出来。菜谱上的一条条步骤,就相当于计算机中的指令,程序指令指挥着计算机各个部件的运行。 | ||
|
||
一个计算机指令包含操作码和操作数两个部分,顾名思义,操作码指示的是计算机需要做的操作,操作数则标识了某一次操作需要用到的数据。由于指令长度有限,操作数的最大寻址范围又远小于内存的寻址范围,于是操作数的获取方式又分为很多种,包括立即数寻址、直接寻址、间接寻址、相对寻址、基址寻址、变址寻址等。一条指令的执行主要包括如下几个步骤:取指令、分析指令、执行指令,有的指令还包括内存写回。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>coding2017</artifactId> | ||
<groupId>me.sidzh</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>Week01</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.stefanbirkner</groupId> | ||
<artifactId>system-rules</artifactId> | ||
<version>1.16.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
public class ArrayList implements List{ | ||
|
||
private Object[] elements; | ||
|
||
private static final int INITIAL_SIZE = 16; | ||
|
||
public static final int MAX_LIST_SIZE = 48; | ||
|
||
private int size = 0; | ||
|
||
private int capacity = 0; | ||
|
||
public ArrayList() { | ||
elements = new Object[INITIAL_SIZE]; | ||
capacity = INITIAL_SIZE; | ||
} | ||
|
||
public void add(int index, Object obj) { | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
ensureSpace(); | ||
if (index == size) { | ||
add(obj); | ||
} else { | ||
System.arraycopy(elements, index, elements, index + 1, size - index); | ||
elements[index] = obj; | ||
size++; | ||
} | ||
} | ||
|
||
public void add(Object obj) { | ||
ensureSpace(); | ||
elements[size++] = obj; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder("List: [ "); | ||
for (int i = 0; i < size; ++ i) { | ||
builder.append(elements[i]).append(" "); | ||
} | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
|
||
private void ensureSpace() { | ||
if (size == capacity) { | ||
if (size == MAX_LIST_SIZE) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
int newCapacity = capacity*2 > MAX_LIST_SIZE ? MAX_LIST_SIZE : capacity*2; | ||
grow(newCapacity); | ||
} | ||
} | ||
|
||
private void grow(int newLength) { | ||
Object[] newElements = new Object[newLength]; | ||
System.arraycopy(elements, 0, newElements, 0, elements.length); | ||
elements = newElements; | ||
capacity = newLength; | ||
} | ||
|
||
public Object get(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return elements[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new IllegalArgumentException(); | ||
} | ||
Object toRemove = elements[index]; | ||
System.arraycopy(elements, index + 1, elements, index, size - index -1); | ||
--size; | ||
return toRemove; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator{ | ||
|
||
private int pos = 0; | ||
|
||
public boolean hasNext() { | ||
return pos < size(); | ||
} | ||
|
||
public Object next() { | ||
return elements[pos++]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public class BinaryTree { | ||
|
||
private BinaryTreeNode root; | ||
|
||
private static class BinaryTreeNode { | ||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
} | ||
|
||
public BinaryTree getLeft() { | ||
|
||
|
||
return null; | ||
} | ||
|
||
public void setLeft() { | ||
|
||
} | ||
|
||
public BinaryTree getRight() { | ||
|
||
return null; | ||
} | ||
|
||
public void setRight() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Created by spike on 2/19/17. | ||
*/ | ||
public interface Iterator { | ||
boolean hasNext(); | ||
Object next(); | ||
} |
Oops, something went wrong.