public class HashMap <V> {
Object[] keys;
Object[] values;
int initialSize = 4;
int currentSize = initialSize;
public HashMap() {
values = new Object[currentSize];
keys = new Object[currentSize];
}
public void put(Object key,V value) {
int pos = findStorageIndice(key.hashCode());
if (pos == -1) {
resizeBucket(currentSize<<1);
put(key,value);
} else {
keys[pos] = key;
values[pos] = value;
}
}
@SuppressWarnings("unchecked")
private void resizeBucket(int newSize) {
Object oldkeys[] = keys;
Object oldvalues[] = values;
currentSize = newSize;
values = new Object[currentSize];
keys = new Object[currentSize];
for (int i = 0; i < oldkeys.length;i++) {
put(oldkeys[i],(V)oldvalues[i]);
}
}
private int findStorageIndice(Object key) {
int startPos = key.hashCode() & (currentSize-1);
if ((keys[startPos] == null)||(keys[startPos].equals(key))) {
return startPos;
}
for (int i = (startPos+1) & (currentSize-1);i != startPos;i=(i+1) & (currentSize-1)) {
if ((keys[i] == null)||(keys[i].equals(key))) {
return i;
}
}
return -1;
}
private int getIndice(Object key) {
int indice = key.hashCode() & (currentSize-1);
if (key.equals(keys[indice])) {
return indice;
}
for(int i = (indice+1) & (currentSize-1);i != indice;i= (i+1) & (currentSize-1)) {
if (key.equals(keys[i])) {
return i;
}
}
return -1;
}
@SuppressWarnings("unchecked")
public V get(Object key) {
int indice = getIndice(key);
if (indice == -1) {
return null;
} else {
return (V)values[indice];
}
}
}
{
public static int getRandInt() {
int lower = 0;
int higher = 100000000;
int random = (int)(Math.random() * (higher-lower)) + lower;
return random;
}
public static void main(String argv[]) {
long millis = System.currentTimeMillis();
System.out.println("Launching custom HashMap insertion");
HashMap<String> map = new HashMap<String>();
for (int i = 0;i < 1000000;i++) {
Integer iobj = new Integer(getRandInt());
map.put(iobj, "Value="+iobj);
map.get(iobj);
}
long currentmillis = System.currentTimeMillis();
System.out.println("Elapsed : " +(currentmillis - millis) + "ms");
millis = System.currentTimeMillis();
System.out.println("Launching reference HashMap insertion");
java.util.HashMap<Integer,String> map2 = new java.util.HashMap<Integer,String>();
for (int i = 0;i < 1000000;i++) {
Integer iobj = new Integer(getRandInt());
map2.put(iobj, "Value="+iobj);
map2.get(iobj);
}
currentmillis = System.currentTimeMillis();
System.out.println("Elapsed : " +(currentmillis - millis) + "ms");
}
}
Launching custom HashMap insertion
Elapsed : 5871ms
Launching reference HashMap insertion
Elapsed : 507ms
Launching custom HashMap insertion
Elapsed : 1127ms
Launching custom HashMap insertion
Elapsed : 970ms
Launching custom HashMap insertion
Elapsed : 685ms
select * from(
SELECT dev.RREC_COD,DEV.TYPE_EVT_CODE ,
ROW_NUMBER() OVER (PARTITION BY dev.RREC_COD ORDER BY dev.DATE_EVT) as rnum
from ZZ dev
where
EXTRACT(YEAR FROM dev.DATE_EVT) = 2012
) t
where t.rnum = 1
and t.RREC_COD = 1234
On Sat, 13 Oct 2012, Waffle wrote:
# Is there a simple camera scrolling example for kivy that is available? I am
trying to make a side scroller however I cannot find a simple example to follow.
I # looked into the kobr landing zone game, but it was too hard to understand and
I spent a few hours trying to implement its mechanisms into my game (no luck)
private static String baseUrl = "http://query.yahooapis.com/v1/public/yql?q=";
private static String buildRequest(String request) {
try {
return baseUrl + URLEncoder.encode(request, "UTF-8")+"&format=json";
} catch (UnsupportedEncodingException e) {
return baseUrl + URLEncoder.encode(request) +"&format=json";
}
}
String request =
buildRequest("select * from geo.places where text=\"san francisco, ca\"");
ObjectMapper sMapper = new ObjectMapper();
sMapper.configure(org.codehaus.jackson.map.
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String request = buildRequest("select * from geo.places where
text=\"san francisco, ca\"");
InputStreamReader is = null;
Request yqlrequest = null;
try {
is = fetchRessource(request);
yqlrequest = sMapper.readValue(is, Request.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ((yqlrequest != null)&&(yqlrequest.query != null)) {
System.out.println("PLACE :"+yqlrequest.query.result.placeList.size());
} else {
System.out.println("Objet null !");
}
public class Request {
@JsonProperty("query")
public Query query;
}
public class Query {
@JsonProperty("results")
Result result;
}
public class Result {
@JsonProperty("place")
public List<Place> placeList;
}
public class Place {
@JsonProperty("name")
public String name;
}
try {
preparedStatement.executeQuery();
}
catch(SQLException e){
throw e;
}
finally {
prepardedStatement.close();
}
try {
preparedStatement.executeQuery();
}
finally {
prepardeStatement.close();
}
public static String getDirectory(String path) {
File directory = new File(path);
Map<String, Object> data = new HashMap<String, Object>();
data.put("path", path);
List<String> files = new ArrayList<String>();
if (!directory.isDirectory())
return null;
if (directory != null) {
String [] fileList;
int i;
fileList = directory.list();
if (fileList != null) {
for(i=0 ;i<fileList.length;i++) {
files.add(fileList[i]);
}
}
}
data.put("files", files);
Configuration cfg = new Configuration();
Template template;
try {
template = cfg.getTemplate("src/directoryhtml.ftl");
StringWriter out = new StringWriter();
template.process(data, out);
return out.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
return null;
<html>
<body>
<#list files as file>
<a href="${path}${file}">${file}</a><br>
</#list>
</body>
</html>
public class EchoServer {
private final int port;
public EchoServer(int port) {
this.port = port;
}
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
// Configure the server.
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(port));
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new EchoServer(port).run();
}
}
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String s = "HELLO.\n";
final ByteBuf bbHello = ctx.alloc().buffer(s.getBytes().length);
bbHello.writeBytes(s.getBytes());
ctx.write(bbHello);
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
ctx.write(msg);
ctx.flush();
}
}
private static int partition(int tab[], int i, int j) {
int p = tab[i];
boolean right = true;
while (i < j) {
if (right) {
if (p > tab[j]) {
int tmp = tab[j];
tab[j] = tab[i];
tab[i] = tmp;
right = false;
i++;
} else {
j--;
}
}
if (!right) {
if (p <= tab[i]) {
int tmp = tab[j];
tab[j] = tab[i];
tab[i] = tmp;
right = true;
j--;
} else {
i++;
}
}
}
return i;
}
private static int partition2(int tab[], int i, int j) {
int pivot_value = tab[i];
int store_index = i;
swap(tab,i,j);
for (int id = i; id < j;id++) {
if (tab[id] < pivot_value){
swap(tab,id,store_index);
store_index++;
}
}
swap(tab,store_index,j);
return store_index;
}
public static void quickSort(int tab[], int i, int j) {
if (i < j) {
int q = partition2 (tab,i,j);
quickSort(tab,i,q);
quickSort(tab,q+1,j);
}
}
x % 512 = x & (512 - 1)