The directions in this section of code are giving me trouble and I don't know how to follow through with them properly. (We have to write code where there are dollar signs.)
Instructions: write a code that repetitively invokes the HashPerformanceClass mainDriver. Then the repetitive code starts the number of insertions at statrInsertions, increase the insert count by the deltaInsertions until end insertions value.
Problem
I am having trouble repetitively invoking the HashPerformanceClass mainDriver, and I don't know what to do. If anyone has any advise I would be grateful.
public class MainClass {
static int displayResultsNo = 0;
void displayHashResults() {
displayResultsNo++;
System.out.println("\nDisplay No. - "+displayResultsNo);
System.out.println("\n\nHash Instrumented Performance:\n-----------------------------");
System.out.println("Trials: " + HashPerformanceClass.trials + ", " +
"Insert Count: " + HashPerformanceClass.insertCount + ", " +
"Load: " + HashPerformanceClass.payload + ", " +
"Table Start Size: " + HashPerformanceClass.startingSize);
System.out.println("\nLinear Hashing:");
System.out.println("Insertion Total Probes: " + HashPerformanceClass.insertionLinearProbes) ;
System.out.println("Insertion Average Probes: " + HashPerformanceClass.insertionLinearProbesAvg);
System.out.println("\nDouble Hashing:");
System.out.println("Insertion Total Probes: " + HashPerformanceClass.insertionDoubleProbes) ;
System.out.println("Insertion Average Probes: " + HashPerformanceClass.insertionDoubleProbesAvg);
System.out.println("\nPerfect Hashing:");
System.out.println("Insertion Total Probes: " + HashPerformanceClass.insertionPerfectProbes) ;
System.out.println("Insertion Average Probes: " + HashPerformanceClass.insertionPerfectProbesAvg);
}//displayHashResults()
static int displayNo = 0;
void displayHashAvgProbesTable(int trials, int startInsertions, int endInsertions, int deltaInsertions, double load, int startSize) {
displayNo++;
System.out.println("\n\nDisplay No. - "+displayNo);
System.out.println("\nAverage Number of Probes Table \n------------------------------\ntrials: "+trials+" Load: "+load+" Start Size: "+startSize+"\n");
System.out.println(String.format("%-15s %-15s %-15s %-15s", "No Of Items","Linear Hash" ,"Double Hash" ,"Perfect Hash ") );
System.out.println(String.format("%-15s %-15s %-15s %-15s", "Inserted" ,"Probe Average","Probe Average","Probe Average") );
System.out.println(String.format("%-15s %-15s %-15s %-15s", "-----------","-------------","-------------","-------------") );
//$ write a code that repetitively invokes the HashPerformanceClass mainDriver
// The the repetitive code starts the number of insertions at startInsertions, increases the insert count by the deltaInsertions
//until end insertions value
}//displayHashAvgProbesTable()
MainClass () {
//$Invoke HashPerformanceClass mainDriver using arguments of insertCount=1, trials =10, payLoad=100%, Starting Size =7, display results
HashPerformanceClass.mainDriver( 1, 1, 1.0, 7); displayHashResults();
HashPerformanceClass.mainDriver(100, 80, 0.5, 101); displayHashResults(); //third column 1.0 = 100%
HashPerformanceClass.mainDriver(100, 80, 0.9, 101); displayHashResults();
HashPerformanceClass.mainDriver( 20, 1000, 0.5, 503); displayHashResults();
HashPerformanceClass.mainDriver( 20, 1000, 1.0, 1009); displayHashResults();
HashPerformanceClass.mainDriver( 20, 1000, 1.0, 4999); displayHashResults();
HashPerformanceClass.mainDriver(100, 2400, 0.5, 4999); displayHashResults();
displayHashAvgProbesTable ( 10, 100, 2000, 100, 0.5, 1009);
displayHashAvgProbesTable ( 10, 100, 2500, 1000, 0.5, 4999);
displayHashAvgProbesTable ( 10, 100, 2500, 1000, 0.5, 4999);
}//MainClass
public static void main(String[] args) {
new MainClass();
}//main()
}//MainClass
___________________________________________________________
import java.util.*;
/**
* A class for generating statistical information hash table insertion.
*/
public class HashPerformanceClass {
static int insertionLinearProbes = 0, insertionDoubleProbes = 0, insertionPerfectProbes = 0;
static float insertionLinearProbesAvg = 0, insertionDoubleProbesAvg = 0, insertionPerfectProbesAvg = 0;
static int trials;
static int insertCount;
static double payload;
static int startingSize;
public static void mainDriver(int trials, int insertCount, double payLoad, int startingSize) {
HashPerformanceClass.trials = trials;
HashPerformanceClass.insertCount = insertCount;
HashPerformanceClass.payload = payLoad;
HashPerformanceClass.startingSize= startingSize;
insertionLinearProbes = insertionDoubleProbes = insertionPerfectProbes = 0;
insertionLinearProbesAvg = insertionDoubleProbesAvg = insertionPerfectProbesAvg = 0;
//$ Declare all 3 hash table objects (linear, double, perfect) using the generic parameterized types as String and setting Starting Size from the mainDriver input parameter
DictionaryInstrumentedLinearImplementation <String, String> linearHashTableObj;
DictionaryInstrumentedDoubleImplementation <String, String> doubleHashTableObj;
DictionaryInstrumentedPerfectImplementation<String, String> perfectHashTableObj;
//Array used to hold random data inserted
String dataArray[];
//$
//For each trial set the inputs for the hash table Implementation
// Generate the Random Data array using insert count, do only once for each trial so that all hash table types have same source data set
// For all 3 hash table types
// Instantiate new hash table object
// Set the pay load factor hashtableobject.set payload
// Insert all the data from the array into the table insertalldata( , );
// Reset the probe counters for the hash table insertionlinear probes +=
// Sum up the probe statistics
//do this for all insertionlinearprobes double and perfect . . .
//$ Calculate all the insertion probes averages into the class variables for each hash type
}//mainDriver()
/* Generate an array of random of pseudo words. Each word will be composed of three randomly chosen syllables.
* #param arraySize The number of strings to generate.
* #return The array of strings.
*/
private static String[] generateRandomData(int arraySize) {
String uniqueWordStringArray[] = new String[arraySize];
DictionaryInstrumentedLinearImplementation<String,String> checkTable = new DictionaryInstrumentedLinearImplementation<String,String>();
String firstSylStringArray [] = {"ther", "fal", "sol", "cal", "com", "don", "gan", "tel", "fren", "ras", "tar", "men", "tri", "cap", "har"};
String secondSylStringArray[] = {"mo", "ta", "ra", "te", "bo", "bi", "du", "ca", "dan", "sen", "di", "no", "fe", "mi", "so" };
String thirdSylStringArray [] = {"tion", "ral", "tal", "ly", "nance", "tor", "ing", "ger", "ten", "ful", "son", "dar", "der", "den", "ton"};
Random generator = new Random();
int i=0;
while (i < arraySize) {
String valueString;
valueString = firstSylStringArray [generator.nextInt( firstSylStringArray.length) ];
valueString += secondSylStringArray[generator.nextInt(secondSylStringArray.length) ];
valueString += thirdSylStringArray [generator.nextInt( thirdSylStringArray.length) ];
if (!checkTable.contains(valueString)) {
// Have not seen pseudo word string before, so add it to the list array list
uniqueWordStringArray[i] = valueString;
checkTable.add(valueString,valueString);
i++;
}//end if
}//while
return uniqueWordStringArray;
}
/* Insert all of the values in the array into the hash table.
* #param dict The dictionary to insert all the words into.
*/
private static void insertAllData(DictionaryInterface<String,String> dict, String[] dataArray) {
for (String wordString : dataArray) {
dict.add(wordString, wordString);
}
}//insertAllData()
}//HashPerformanceClass
Related
I have a table with String columns "key1", "col1", "col2", "col3". I have a snippet below to test the bulk insert. When I run it, I don't get any error, yet I don't see any changes to the "test" table.
Did I miss something?
Object[][] data = new Object[4][];
ArrayList<String>[] rec = new ArrayList[4];
rec[0] = new ArrayList<String>();
rec[1] = new ArrayList<String>();
rec[2] = new ArrayList<String>();
rec[3] = new ArrayList<String>();
for (Integer i = 0; i < 10; i++) {
rec[0].add(i.toString() + i.toString() + i.toString());
rec[1].add(i.toString() + i.toString() + i.toString());
rec[2].add(i.toString() + i.toString() + i.toString());
rec[3].add(i.toString() + i.toString() + i.toString());
}
for (int i = 0; i < 4; i++) {
data[i] = rec[i].toArray(new Object[rec[i].size()]);
}
c.Dict dict = new c.Dict(Arrays.asList("key1", "col1", "col2", "col3").toArray(new String[4]), data);
c.Flip flip = new c.Flip(dict);
Object[] updStatement = new Object[] { ".u.upd", "test", flip };
conn.ks(updStatement);
Adding onto what you said, you want to be using the ks() method with 2 arguments.
From the javadoc given in the c.java class:
Use this to
* invoke a function in kdb+ which takes 2 arguments and does not return a value. e.g. to invoke f[x;y] use ks("f",x,y);
* to invoke a lambda, use ks("{x+y}",x,y);
The function .u.upd takes in 2 arguments and, by default, has the same signature as insert:
First argument is a symbol for the table name, which has the type String in Java
Second argument is the records which has the type Object[] in Java
The length of the Object[] in the second argument should be equal to the number of columns. Each Object in the Object[] should itself be an array that has a length equal to the number of records. The order of the inner arrays should be the same order as the columns, and the values of each inner array should have the matching type to the column type in kdb and have the same order as the records.
Your Object[] should look like:
new Object[]{
new Object[]{row1col1, row2col1, /*..., */ rowNcol1},
new Object[]{row1col2, row2col2, /*..., */ rowNcol2},
/* column 3 to column N-1 values */
new Object[]{row1colN, row2colN, /*..., */ rowNcolN}
}
And your ks() method call should look like:
conn.ks(".u.upd", "test", new Object[]{ /*.... */});
I searched a bit and not finding any - wrote a StratifiedKFold method which can be used with Scala, Spark (MLlib). I am posting the answer below
a java version of stratifiedKFold of JavaRDD.
/*
stritified KFolds. this will promise each category will be fairly splited into training and testing samples.
for example 3 folds, category '0' has 3 samples, then all the 3 output folds will be like: training with 2 samples, testing with 1 samples.
the split will be as fair as possible, like: to split 11 samples into 3, result will be [4, 4, 3].
if the count of one category is less than k, then some folds will not contain samples of this category, this bad split will cause inaccurate result. but this is not mandatory.
*/
private static Tuple2<JavaRDD<LabeledPoint>, JavaRDD<LabeledPoint>>[] stritifyKFolds(JavaRDD<LabeledPoint> data, Broadcast<Integer> kBC){
JavaRDD<List<List<LabeledPoint>>> foldedLP = data.mapToPair( //map to (key,list) pair.
lp -> {
List<LabeledPoint> list = new ArrayList<>();
list.add(lp);
return new Tuple2<>(lp.label(), list);
}
).reduceByKey( //aggregate
(List<LabeledPoint> list1, List<LabeledPoint> list2) -> {
list1.addAll(list2);
return list1; //put the LabeledPoint with the same key into one list.
}
).values() //get list only after aggregate.
.map( //split each list into K folds.
list -> {
//shuffule and then put into different folds.
Collections.shuffle(list);
int total = list.size();
if(total < kBC.value()){
log.warn("category size {} is less than folds number {}, this will break stratification and leads to bad folds splitting", total, kBC.value());
}
//assign each element into folds.
List<List<LabeledPoint>> keyFolds = new ArrayList<>();
int avg = total/kBC.value(); //averge number of elements.
int remain = total - kBC.value(); //remain number of elements, which will be assign to folds from left to right fairly.
for(int i=0, index = 0, count=0; i<kBC.value(); i++, index += count){
//get current folds count
count = (i<remain) ? avg+1 : avg;
keyFolds.add(list.subList(index, index+count));
}
return keyFolds;
}
);
foldedLP.persist(StorageLevel.MEMORY_AND_DISK_SER());
Tuple2<JavaRDD<LabeledPoint>, JavaRDD<LabeledPoint>>[] result = new Tuple2[kBC.value()];
//initialize folds, each fold is a tuple of (training RDD, testing RDD).
for(int i=0; i<kBC.value(); i++){
final int ii = i; //must be final variable if used in lambda.
JavaRDD<LabeledPoint> training_i = foldedLP.flatMap(
list -> {
List<LabeledPoint> noII = new ArrayList<>();
for(int j=0; j<kBC.value(); j++){
if(j != ii){
noII.addAll(list.get(j)); //get the all except ii_th list iterator.
}
}
return noII.iterator();
}
);
JavaRDD<LabeledPoint> testing_i = foldedLP.flatMap(
list -> list.get(ii).iterator() //get the ii_th list iterator.
);
result[i] = new Tuple2<>(training_i, testing_i);
}
foldedLP.unpersist();
return result;
}
def StratifiedKFold(nSamples: Int, k: Int, labels: List[Int],shuffle: Boolean = false): (Map[Int,List[List[Int]]],Int)= {
var idxs = (0 until nSamples).toArray
val unqLabels = labels.distinct
val noOfLabels = unqLabels.length
val idxsbylabel = idxs.groupBy { x => labels(x) }
var stratifiedidxs: Map[Int,List[List[Int]]] = Map(1 -> List(List(1)))
for ( i <- 0 to noOfLabels-1){
val labelsgroup_i_arr = if(shuffle) bshuffle(idxsbylabel(i).toArray) else idxsbylabel(i).toArray
val noOfParts = if(labelsgroup_i_arr.length%k==0) labelsgroup_i_arr.length/k else (labelsgroup_i_arr.length/k)+1
val labelsgroup_i_lst = List.concat(labelsgroup_i_arr)
stratifiedidxs = stratifiedidxs + (i -> labelsgroup_i_lst.grouped(noOfParts).toList)
}
(stratifiedidxs,noOfLabels)
}
I am fairly new to java and am currently debugging a program. So I have a class that has a simple method with a few calculations. Basically when I call this method from another class to use a variable (that happens to be part of an array where some of it is being calculated) I get the calculation I need. Then I call it once more in another class, it calculates again and my numbers become compromised. Is there a way to have this method take in some value, calculate and then stay put?
This is the array class. I need to instantiate this once and then have the values for the arrays be static when I call it again. Is there a way to make the method stay put while I just call the arrays?
In another class I use PlanMat pm = new PlanMat()
then something like pm.materials(fSi, fS, fO); and to call an array pm.rho0[48];
public class PlanMat {
Constants con = new Constants();
double GPa = con.GPascals;
Main m = new Main();
public int i ;
public int eos [ ];
public double rho0 [ ];
public double c [ ];
public double nn [ ];
public double Ks0 [ ];
public double Ksp [ ];
public void materials(double fSi, double fS, double fO)
{
i = 0;
eos = new int [ 51 ];
rho0 = new double [ 51 ];
c = new double [ 51 ];
nn = new double [ 51 ];
Ks0 = new double [ 51 ];
Ksp =new double [ 51 ];
double fFeS = ( ((con.M_Fe / con.M_S) + 1) * fS );
double fFeSi = ( ((con.M_Fe / con.M_Si) + 1) * fSi);
double fFeO = ( ((0.950 * con.M_Fe) + con.M_O) / (1.95 * con.M_O) * fO);
double fFe = 1.0 - fFeS - fFeSi - fFeO;
i = 48;
eos [ i ] = 1;
rho0 [ i ] = 1.0 / ( (fFe / rho0[3]) + (fFeSi / rho0[21])
+ (fFeO / rho0[22]) + (fFeS / rho0[13]) );
Ks0 [ i ] = fFe * Ks0[3] + fFeSi * Ks0[21] + fFeO * Ks0[22]
+ fFeS * Ks0[13];
Ksp [ i ] = fFe * Ks0[3] + fFeSi * Ks0[21] + fFeO * Ks0[22]
+ fFeS * Ks0[13];
c [ i ] = 0.0;
nn [ i ]= 0.0;
}
}
There are two wasys to acheive this:
First:
You should write set and get methods, call to set method to set the values and get method to get the values. You would be setting the values say in class1 you set the values and now if you want the same values in class2 to you would be calling your get method to get the values as an array and pass this array as an argument to the constructor of class2.
Second:
You can pass the whole object to the class2 constructor and use that object in that class your values will be same and data will not be compromised but for this you will have to implement the Serializable on your data class(That you are using in both.)
class a{
......
function()
{
a,b
}
}
class b{
...function(){
here u need your variable value.
call the function and store the variable value in new variable.
}
}
if u need to do same u can do
class c{
...function(){
here u need your variable value.
call the function and store the variable value in new variable.
}
}
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);
count_matched=0;
bson *doc;
while(mongo_cursor_next(cursor)==MONGO_OK)
{
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator_init(&it,doc);
while(bson_iterator_next(&it) != BSON_EOO)
{
fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));
}
}
This code is working perfectly and i can see the matched documents (Key + Value) but now i want to save the matched document's key and value to a string. Can any tell me how i can save the return value of key and value in to a string?
One document includes (all strings)
Total Key=10
Total value=10
and i want to save 10 document's key and value at one time. I am using C driver of mongodb.
The following code shows how you would be doing copy of the key and values from the bson iterator into your key-value arrays temp_key and temp_value. The specific block of code is in between the comments marked START and END.
Additionally, you can find documentation for accessing BSON document contents at http://api.mongodb.org/c/current/bson.html .
mongo_cursor *cursor = mongo_find(&conn, TEST_NS, &query, NULL, 0, 0, 0);
int count_matched = 0;
bson *doc;
// Assuming you are just looking for 100 key / value pair of max length of 99 characters
const unsigned KV_ARRAY_LENGTH = 100;
const unsigned MAX_KV_LENGTH = 105;
char temp_key[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1], temp_value[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1];
int i = 0;
while (mongo_cursor_next(cursor) == MONGO_OK) {
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator it;
bson_iterator_init(&it,doc);
while (bson_iterator_next(&it) != BSON_EOO) {
fprintf(stderr,"%s : %s\n", bson_iterator_key(&it), bson_iterator_string(&it));
/******* START - Code to capture key-value into appropriate array */
if (i < KV_ARRAY_LENGTH) {
/* - Collect key-value pairs only if there is space in the array
* - Key / Value woud be captured only till the max amount of space available for them i.e. MAX_KV_LENGTH in this case
* */
strncpy(temp_key[i], bson_iterator_key(&it), MAX_KV_LENGTH);
strncpy(temp_value[i], bson_iterator_string(&it), MAX_KV_LENGTH);
temp_key[i][MAX_KV_LENGTH] = temp_value[i][MAX_KV_LENGTH] = '\0';
++i;
} else {
/* whatever need to be done if there is no room in the array */
}
/******* END - Code to capture key-value into appropriate array */
}
}
/* Test iterating through the key-value pair constructed in query iteration */
fprintf(stdout, "--- Fields collected ---\n");
int keyIndex = 0;
for ( ; keyIndex < i; ++keyIndex) {
fprintf(stdout, "{key: %s, value: %s}\n", temp_key[keyIndex], temp_value[keyIndex]);
}
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0);
count_matched=0;
bson *doc;
//Answer
const char* temp_key[100][100],temp_value[100][100];
int i=0;
while(mongo_cursor_next(cursor)==MONGO_OK)
{
count_matched++;
doc=(bson *)mongo_cursor_bson(cursor);
bson_iterator_init(&it,doc);
while(bson_iterator_next(&it) != BSON_EOO)
{
fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it));
temp[i][0]=bson_iterator_key[&it]; //Answer
temp_value[i][0]=bson_iterator_key[&it]; //Answer
i++; //Answer
}
}
Just for the record, this is the rough sketch and i know about corruption of the temp variables and their overflow but i will remove it according to my code.
Hi is it possible to set the time range of a Dojo textTimeBox to 09:00 - 18:30.
I can't find anything in either the Zend or Dojo documentation that show how this can be done or if it can be done.
Many thanks in advance.
You can set max and min constraints for widget:
new dijit.form.TimeTextBox({
name: "prog_val",
value: new Date(),
constraints: {
timePattern: 'HH:mm:ss',
clickableIncrement: 'T00:15:00',
visibleIncrement: 'T00:15:00',
visibleRange: 'T01:00:00',
min:'T09:00:00',
max:'T18:30:00'
}
},
"prog_val");
It does not allow the user to enter data beyond the allowed values.
However this still allows user to scroll to the disabled times, user just cannot select them.
For hiding disabled times you should do some hack :)
You should override _getFilteredNodes method of dijit._TimePicker. For example :
dojo.declare("my._TimePicker", dijit._TimePicker, {
// extend the default show() method
_getFilteredNodes: function (/*number*/start, /*number*/maxNum, /*Boolean*/before) {
// summary:
// Returns an array of nodes with the filter applied. At most maxNum nodes
// will be returned - but fewer may be returned as well. If the
// before parameter is set to true, then it will return the elements
// before the given index
// tags:
// private
var nodes = [], n, i = start, max = this._maxIncrement + Math.abs(i),
chk = before ? -1 : 1, dec = before ? 1 : 0, inc = before ? 0 : 1;
do {
i = i - dec;
var date = new Date(this._refDate);
var incrementDate = this._clickableIncrementDate;
date.setHours(date.getHours() + incrementDate.getHours() * i,
date.getMinutes() + incrementDate.getMinutes() * i,
date.getSeconds() + incrementDate.getSeconds() * i);
if (!this.isDisabledDate(date)) {
n = this._createOption(i);
if (n) { nodes.push(n); }
}
i = i + inc;
} while (nodes.length < maxNum && (i * chk) < max);
if (before) { nodes.reverse(); }
return nodes;
}
});
And you need to set this new class ('my._TimePicker') as a popupClass property of your text time box:
dojo.addOnLoad(function () {
dijit.byId("prog_val").popupClass = "my._TimePicker";
});
And you can see : it works!