Holder Class
Author |
Message |
copthesaint
|
Posted: Wed Sep 30, 2009 4:12 pm Post subject: Holder Class |
|
|
I made this holder class that stores type of type integer and double.
I thought it would be interesting to use, anyways Please comment!
Java: | /*********************************************
*Holder Class by: Christian Olienick Plourde*
*********************************************
*Designed to be used to hold values, *
*without having to declare variables. *
*********************************************
*/
public class Holder {
int[] intValueHolder = new int[10000];
int[] intValueHolderID = new int [10000];
String [] intValueName = new String [10000];
int intLength = 0;
double[] doubleValueHolder = new double [10000];
int[] doubleValueHolderID = new int [10000];
String [] doubleValueName = new String [10000];
int doubleLength = 0;
/*******************************************************
*Creates several integer holders with the same name.
*This also can be accomplished with createIntHolder,
*but it may cause undesired effects to arrays.
*To use, set the name of the holder and number of
*holders to add to the name.
*/
public void addToIntHolder (String name, int numValues ){
int preIntLength = intLength+ 1+numValues;
for (int i = intLength ; i < preIntLength;i++ ){
if (i < 10000){
intValueHolderID [i ] = 10000+i;
intValueName [i ] = name;
intLength = i;
}
else if (i == 10000){
System. out. println ("Cannot create holder for "+ name+ ".");
}
}
}
/********************************************************
*Creates several double holders with the same name.
*This also can be accomplished with createDoubleHolder,
*but it may cause undesired effects to arrays.
*To use, set the name of the holder and number of
*holders to add to the name.
*/
public void addToDoubleHolder (String name, int numValues ){
int preDoubleLength = doubleLength+ 1+numValues;
for (int i = doubleLength ; i < preDoubleLength;i++ ){
if (i < 10000){
doubleValueHolderID [i ] = 10000+i;
doubleValueName [i ] = name;
doubleLength = i;
}
else if (i == 10000){
System. out. println ("Cannot create holder for "+ name+ ".");
}
}
}
/********************************************************
*Creates an integer holder for a spesified name, and
*sets the initial value of the holder.
*To use set the name of the holder, then the initial
*value of the holder.
*/
public void createIntHolder (String name, int value ){
for (int i = 0;i < intLength+ 2; i++ ){
if (intValueHolderID [i ] == 10000+i ){
if (i == 10000){
System. out. println ("Cannot create a referrence for "+ name+ ". Exceeds array Int holder length.");
i = intLength+ 2;
}
}
else if (intValueHolderID [i ] == 0){
intValueHolderID [i ] = 10000+i;
intValueName [i ] = name;
intValueHolder [i ] = value;
if (intLength > i ) {
}
else if (intLength < i ){
intLength = i;
}
i = intLength+ 2;
}
}
}
/********************************************************
*Creates an double holder for a spesified name, and
*sets the initial value of the holder.
*To use set the name of the holder, then the initial
*value of the holder.
*/
public void createDoubleHolder (String name, double value ){
for (int i = 0;i < doubleLength+ 2; i++ ){
if (doubleValueHolderID [i ] == 10000+i ){
if (i == 10000){
System. out. println ("Cannot create a referrence for "+ name+ ". Exceeds array Int holder length.");
i = doubleLength+ 2;
}
}
else if (doubleValueHolderID [i ] == 0){
doubleValueHolderID [i ] = 10000+i;
doubleValueName [i ] = name;
doubleValueHolder [i ] = value;
if (doubleLength > i ) {
}
else if (doubleLength < i ){
doubleLength = i;
}
i = doubleLength+ 2;
}
}
}
/********************************************************
*Sets a holder's value to what is specified.
*To use, set the name of the holder then the value to
*set the integer value of the holder, and then the
*array value of the holder. If the holder isn't an
*array set the value to 0.
*/
public void setIntHolder (String name, int value, int arrayValue ){
int count = 0;
for (int i = 0; i < intLength+ 1;i++ ){
if (intValueHolderID [i ] == 10000+i & intValueName [i ] == name ){
if (count == arrayValue ){
intValueHolder [i ] = value;
i = intLength+ 1;
}
else if (count != arrayValue & i == intLength ){
System. out. println ("Cannot find holder "+ name + "of array index "+ value+ ".");
}
}
else if (intValueHolderID [i ] != 10000+i || intValueName [i ] != name ){
if (i == intLength ){
System. out. println ("Cannot find holder "+ name + ". The holder has not been created.");
}
}
}
}
/********************************************************
*Sets a holder's value to what is specified.
*To use, set the name of the holder then the value to
*set the double value of the holder, and then the
*array value of the holder. If the holder isn't an
*array set the value to 0.
*/
public void setDoubleHolder (String name, double value, int arrayValue ){
int count = 0;
for (int i = 0; i < doubleLength+ 1;i++ ){
if (doubleValueHolderID [i ] == 10000+i & doubleValueName [i ] == name ){
if (count == arrayValue ){
doubleValueHolder [i ] = value;
i = doubleLength+ 1;
}
else if (count != arrayValue & i == doubleLength ){
System. out. println ("Cannot find holder "+ name + "of array index "+ value+ ".");
}
}
else if (doubleValueHolderID [i ] != 10000+i || doubleValueName [i ] != name ){
if (i == doubleLength ){
System. out. println ("Cannot find holder "+ name + ". The holder has not been created.");
}
}
}
}
/********************************************************
*Gathers values from a holder and returns them as an
*array. The number of values returned must be specified.
*If there are fewer values avalible then the number of
*values to be returned the values will be set to 0.
*To use set the name of the holder, then the value for
*the length of the array.
*/
public int[] returnArrayIntHolder (String name, int scope ){
int[] returnValue = new int[scope ];
int count = 0;
for (int i = 0; i < intLength+ 1; i++ ){
if (name == intValueName [i ]){
returnValue [count ] = intValueHolder [i ];
count = count + 1;
if (count > scope ){
i = intLength+ 1;
}
}
}
return returnValue;
}
/********************************************************
*Gathers values from a holder and returns them as an
*array. The number of values returned must be specified.
*If there are fewer values avalible then the number of
*values to be returned the values will be set to 0.
*To use set the name of the holder, then the value for
*the length of the array.
*/
public double[] returnArrayDoubleHolder (String name, int scope ){
double[] returnValue = new double[scope ];
int count = 0;
for (int i = 0; i < doubleLength+ 1; i++ ){
if (name == doubleValueName [i ]){
returnValue [count ] = doubleValueHolder [i ];
count = count + 1;
if (count > scope ){
i = doubleLength+ 1;
}
}
}
return returnValue;
}
/********************************************************
*Returns a single integer value from a holder name.
*To use set the name of the holder and the array number.
*If the holder isn't an array, set the array number as 0.
*/
public int returnIntHolder (String name, int arrayNum ) {
int returnValue = 0;
int countArrayNum = 0;
boolean returnIsTrue = false;
for (int i = 0;i < intLength+ 1;i++ ){
if (name == intValueName [i ]){
if (countArrayNum == arrayNum ) {
returnValue = intValueHolder [i ];
returnIsTrue = true;
}
else if (countArrayNum != arrayNum ) {
countArrayNum = countArrayNum+ 1;
}
}
else if (name != intValueName [i ]){
}
}
if (returnIsTrue == false){
System. out. println ("Unable to find "+ name + "of array value "+ arrayNum+ ".");
}
return returnValue;
}
/********************************************************
*Returns a single double value from a holder name.
*To use set the name of the holder and the array number.
*If the holder isn't an array, set the array number as 0.
*/
public double returnDoubleHolder (String name, int arrayNum ) {
double returnValue = 0;
int countArrayNum = 0;
boolean returnIsTrue = false;
for (int i = 0;i < doubleLength+ 1;i++ ){
if (name == doubleValueName [i ]){
if (countArrayNum == arrayNum ) {
returnValue = doubleValueHolder [i ];
returnIsTrue = true;
}
else if (countArrayNum != arrayNum ) {
countArrayNum = countArrayNum+ 1;
}
}
else if (name != doubleValueName [i ]){
}
}
if (returnIsTrue == false){
System. out. println ("Unable to find "+ name + "of array value "+ arrayNum+ ".");
}
return returnValue;
}
/********************************************************
*Removes an integer holder.
*To use, set the name of the holder to remove.
*This removes array variables and normal.
*/
public void removeIntHolder (String name ){
for (int i = 0; i < intLength+ 1; i++ ){
if (name == intValueName [i ]){
intValueName [i ] = "";
intValueHolderID [i ] = 0;
intValueHolder [i ] = 0;
}
}
}
/********************************************************
*Removes an real holder.
*To use, set the name of the holder to remove.
*This removes array variables and normal.
*/
public void removeDoubleHolder (String name ){
for (int i = 0; i < doubleLength+ 1; i++ ){
if (name == doubleValueName [i ]){
doubleValueName [i ] = "";
doubleValueHolderID [i ] = 0;
doubleValueHolder [i ] = 0;
}
}
}
public static void main (String[] args ){
}
} | [/syntax] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Wed Sep 30, 2009 4:23 pm Post subject: RE:Holder Class |
|
|
While that is certainly an impressive effort, why not just use Map<String, int>, specifically HashMap<String, int> ? It'd be way faster for pretty well every operation, uses less memory, and is built-in (it's part of the core Java API).
You also have a series of bugs: you cannot compare one string to another using == in Java. That comparison compares the values of their references, which is not what you want to do. You need to use string1.equals ( string2 ); or string1.equalsIgnoreCase ( string2 ); .
Asymptotic Comparison:
Insert: HashMap is O(1), yours is O(N)
Remove: HashMap is O(1), yours is O(N)
Set: HashMap is O(1), yours is O(N)
Iterate: HashMap is O(N), yours is O(N), but probably faster...if you had implemented this operation.
Max Capacity: HashMap is (whatever you can fit into the heap), yours is 10000.
You also don't need a main() method on a class that isn't going to be run as a program. |
|
|
|
|
|
copthesaint
|
Posted: Thu Oct 01, 2009 1:34 pm Post subject: RE:Holder Class |
|
|
Thanks, i didnt realise I needed to use .equals() all ways...So what I made is simular to the Hashmap Class? If so, what am I doing making this lol. Lastly what did you mean by O(N) and O(1). |
|
|
|
|
|
DemonWasp
|
Posted: Thu Oct 01, 2009 2:04 pm Post subject: RE:Holder Class |
|
|
What you've made implements some of the operations expected of a Map class (in particular, a map from String->integer and String->double), but what you have isn't remotely similar to a HashMap.
The O(N), O(1) and O( some expression on N ) are statements of the asymptotic time required by a given algorithm. Put more simply, they are the "Order" of the operation. So if I say something takes O(1) time, that means "constant time" - no matter how large N is, it should always take the same amount of time. Something which is O(N) will take an amount of time roughly proportional to the value of N. Something which is O ( N^2 ) takes an amount of time roughly proportional to N squared.
As you may imagine, lower orders will scale better (they will handle an increase in N more efficiently than a higher order algorithm). This doesn't necessarily imply that they run faster in all cases, but it does guarantee that for sufficiently large N, they will perform faster.
So when I say that your algorithm is O ( N ), I mean it gets slower in proportion to the number of objects you have in your arrays. If N is large, say N=8000, it will take 800 times longer than if N=10.
HashMap, on the other hand, is O ( 1 )* for all the operations you have implemented - get, remove and set. The algorithm it uses isn't particularly complex, but analyzing it is somewhat more difficult.
* This is a tiny bit of a lie. It's amortized O(1), which is a bit complex and best left for another day. |
|
|
|
|
|
|
|