Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Trace;
import android.support.v7.app.AppCompatActivity;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;

/**
Expand All @@ -29,7 +32,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.container_progress).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dumpPopularRandomNumbersByRank();
mTask = new MyTask().execute();
}
});
WebView webView = (WebView) findViewById(R.id.anim_view);
Expand Down Expand Up @@ -64,6 +67,33 @@ public void dumpPopularRandomNumbersByRank() {
Trace.endSection();
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void dumpPopularRandomNumbersByRankOptimised() {
Trace.beginSection("Data Structures");
// First we need a sorted list of the numbers to iterate through.
ArrayMap<Integer, Integer> sortedNumbers = new ArrayMap<>();
sortedNumbers.putAll(coolestRandomNumbersMap);
coolestRandomNumbers.clone();
Integer[] sorted = {};
sorted = sortedNumbers.keySet().toArray(sorted);
Arrays.sort(sorted);

// Great! Now because we have no rank lookup in the population-sorted array,
// take the random number in sorted order, and find its index in the array
// that's sorted by popularity. The index is the rank, so report that. Easy and efficient!
// Except that it's... you know... It's not.
for (int i = 0; i < sorted.length; i++) {
Integer currentNumber = sorted[i];
for (int j = 0; j < coolestRandomNumbers.length; j++) {
if (currentNumber.compareTo(coolestRandomNumbers[j]) == 0) {
Log.i("Popularity Dump", currentNumber + ": #" + j);
}
}
}
Trace.endSection();
}

public static HashMap<Integer, Integer> coolestRandomNumbersMap = new HashMap<>();
public static Integer[] coolestRandomNumbers = new Integer[3000];
static int temp;

Expand All @@ -72,6 +102,28 @@ public void dumpPopularRandomNumbersByRank() {
for (int i = 0; i < 3000; i++) {
temp = randomGenerator.nextInt();
coolestRandomNumbers[i] = temp;
coolestRandomNumbersMap.put(temp, i);
}
}

private AsyncTask<?, ?, ?> mTask;


class MyTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
dumpPopularRandomNumbersByRankOptimised();
return null;
}

}

@Override
protected void onDestroy() {
super.onDestroy();
if (mTask != null &&
mTask.getStatus() == AsyncTask.Status.RUNNING)
mTask.cancel(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
Expand All @@ -22,6 +23,8 @@ public static void start(Context context) {
context.startActivity(new Intent(context, FibActivity.class));
}

private AsyncTask<?, ?, ?> mTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -30,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.fib_progress).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE)));
mTask = new MyTask().execute();
}
});
WebView webView = (WebView) findViewById(R.id.anim_view);
Expand All @@ -49,4 +52,27 @@ public int computeFibonacci(int positionInFibSequence) {
}
}


class MyTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
return String.valueOf(computeFibonacci(POSITION_IN_FIB_SEQUENCE));
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
textView.setText(s);
}
}


@Override
protected void onDestroy() {
super.onDestroy();
if (mTask != null &&
mTask.getStatus() == AsyncTask.Status.RUNNING)
mTask.cancel(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ public void imPrettySureSortingIsFree() {
}
}

// Now go through and dump the sorted version of each row to output!
for (int i = 0; i < lotsOfInts.length; i++) {
String rowAsStr = "";
StringBuilder sb = new StringBuilder();
for (int j = 0; j < lotsOfInts[i].length; j++) {
// Clearly, the only reasonable way to construct a string is one character at a
// time, with lots and lots of convenient concatenation.
rowAsStr += getSorted(lotsOfInts[i])[j];
sb.append(getSorted(lotsOfInts[i])[j]);
if (j < (lotsOfInts[i].length - 1)) {
rowAsStr += ", ";
sb.append(", ");
}
}
Log.i("MemoryChurnActivity", "Row " + i + ": " + rowAsStr);
Log.i("MemoryChurnActivity", "Row " + i + ": " + sb.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ static class ListenerCollector {
public void setListener(View view, MyCustomView.MyListener listener) {
sListeners.put(view, listener);
}

private static void clearListeners() {
sListeners.clear();
}
}

@Override
protected void onStop() {
super.onStop();
ListenerCollector.clearListeners();
}


}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.0.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
6 changes: 0 additions & 6 deletions gradle/wrapper/gradle-wrapper.properties

This file was deleted.