Created
March 28, 2011 12:27
-
-
Save siritori/890380 to your computer and use it in GitHub Desktop.
とみせかけてぜんぜん仮想機械じゃないデータ型管理まで書いたところ
This file contains hidden or 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
import java.io.*; | |
import java.lang.*; | |
import java.util.*; | |
import java.util.concurrent.atomic.*; | |
enum DataType { | |
// primitive | |
BOOLEAN(true), | |
CHARACTER(true), | |
INTEGER(true), | |
DECIMAL(true), | |
STRING(true), | |
// non-primitive | |
HASH(false), | |
LIST(false); | |
private boolean isPrimitive; | |
DataType(boolean isPrimitive) { | |
this.isPrimitive = isPrimitive; | |
} | |
public boolean isPrimitive() { | |
return isPrimitive; | |
} | |
}; | |
class RecrioObject { | |
DataType type; | |
public String typeToString() { | |
return this.type.toString(); | |
} | |
public DataType ofType() { | |
return this.type; | |
} | |
boolean isPrimitive() { | |
return this.type.isPrimitive(); | |
} | |
} | |
class PrimitiveObject extends RecrioObject { | |
public boolean boolData; | |
public char charData; | |
public int intData; | |
public double decimalData; | |
public String strData; | |
public PrimitiveObject(DataType type) { | |
assert type.isPrimitive(); | |
this.type = type; | |
} | |
} | |
class HashObject extends RecrioObject { | |
HashMap<String, Integer> hashData; | |
public HashObject() { | |
this.type = DataType.HASH; | |
this.hashData = new HashMap<String, Integer>(); | |
} | |
public void put(String key, int val) { | |
this.hashData.put(key, val); | |
} | |
public int get(String key) { | |
return hashData.get(key); | |
} | |
public void remove(String key) { | |
this.hashData.remove(key); | |
} | |
} | |
class ListObject extends RecrioObject { | |
LinkedList<Integer> listData; | |
public ListObject() { | |
this.type = DataType.LIST; | |
this.listData = new LinkedList<Integer>(); | |
} | |
public int getFirst() { | |
return this.listData.getFirst(); | |
} | |
public int getLast() { | |
return this.listData.getLast(); | |
} | |
public int removeFirst() { | |
return this.listData.removeFirst(); | |
} | |
public int removeLast() { | |
return this.listData.removeLast(); | |
} | |
public void addFirst(int idxDataUnit) { | |
this.listData.addFirst(idxDataUnit); | |
} | |
public void addLast(int idxDataUnit) { | |
this.listData.addLast(idxDataUnit); | |
} | |
} | |
class DataTable { | |
static AtomicInteger storeCount;// for id allocation; | |
static HashMap<Integer, RecrioObject> dataStore; // all objects; | |
public DataTable() { | |
storeCount = new AtomicInteger(0); | |
dataStore = new HashMap<Integer, RecrioObject>(); | |
} | |
public static int newRecrioObject(DataType type) { | |
int id = storeCount.getAndIncrement(); | |
RecrioObject obj; | |
switch(type) { | |
case HASH: | |
obj = new HashObject(); | |
break; | |
case LIST: | |
obj = new ListObject(); | |
break; | |
default: | |
obj = new PrimitiveObject(type); | |
} | |
System.err.println("new object[" + obj.typeToString() + "] at " + id); | |
dataStore.put(id, obj); | |
return id; | |
} | |
public static RecrioObject get(int id) { | |
return dataStore.get(id); | |
} | |
public static void remove(int id) { | |
System.err.println("deleted object[" + DataTable.get(id).typeToString() +"] at " + id); | |
dataStore.remove(id); | |
} | |
} | |
class RecrioVirtualMachine { | |
public static void main(String args[]) { | |
new DataTable(); | |
// primitive object | |
int primitives[] = { | |
DataTable.newRecrioObject(DataType.BOOLEAN), | |
DataTable.newRecrioObject(DataType.CHARACTER), | |
DataTable.newRecrioObject(DataType.INTEGER), | |
DataTable.newRecrioObject(DataType.DECIMAL), | |
DataTable.newRecrioObject(DataType.STRING) | |
}; | |
// hash object | |
int hash = DataTable.newRecrioObject(DataType.HASH); | |
HashObject hashObj = (HashObject)DataTable.get(hash); | |
hashObj.put("boolean", primitives[0]); | |
hashObj.put("character", primitives[1]); | |
hashObj.put("integer", primitives[2]); | |
// list object | |
int list = DataTable.newRecrioObject(DataType.LIST); | |
ListObject listObj = (ListObject)DataTable.get(list); | |
listObj.addFirst(primitives[0]); | |
listObj.addFirst(primitives[1]); | |
listObj.addFirst(primitives[3]); | |
listObj.addFirst(primitives[4]); | |
DataTable.remove(hash); | |
DataTable.remove(list); | |
for(int i: primitives) { | |
DataTable.remove(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment