Re: 2D lookup table



On Jan 31, 9:16 am, "Mize-ze" <zahy.bn...@xxxxxxxxx> wrote:
If I want to store and fetch information from a matrix like structure
where the indexes of the matrix are not integers but other data
structures, how such thing can be done without a translation table?

Example:

If I have a list of doubles and I want to store a value per each pairs
of values
What is the best way to do this?

Thanks
Well, it kind of depends on the situation.
You can't reliably use doubles as a key to anything. A pair of doubles
is, *ahem*, doublely unreliable.
But, if you have a String as an x coordinate, and a String as the y
coordinate, then you have a few options.

If there is a finite set of x coordinates and a finite set of y
coordinates, and the matrix is "mostly" full, then you would be best
to use a translation table.
Map<String, Integer> xCoordinates;
Map<String, Integer> yCoordinates;

Object[][] matrix = new Object[yCoordinates.size()]
[xCoordinates.size()];

Alternatively, if your matrix is "mostly" empty, you may wish to use
nested maps:

Map<String, Map<String, Object>> matrix;

As another alternative, you may want to use a different "structure"
altogether:

final class Coordinate<X, Y> {
final X x;
final Y y;
public Coordinate(X x, Y y) {
this.x = x;
this.y = y;
}

public boolean equals(Object o) {
if (o == null || !(o instanceof Coordinate)) {
return false;
}
Coordinate c = (Coordinate)o;
return c.x.equals(x) && c.y.equals(y);
}

public int hashCode() {
return x.hashCode() * 39 + y.hashCode();
}
}


Map<Coordinate, Object> matrix;


Hope this helps,
Daniel.

.



Relevant Pages

  • Re: conversion of types.
    ... I am reading the whole row as a string using fgets. ... I might write doubles to a binary file where each double is eight bytes long. ... You need to delimit the values with an 'out-of-bound' character like '|' or something. ... You have to know the format of the file you are reading. ...
    (comp.lang.c)
  • Double.ToString round-trip: "R" vs. "G17"
    ... and therefore the doubles have to be represented as strings. ... We also need to be able to take the XML back from the browser and parse the ... decimal string representation, but at this point it would be a major ... as the original double, it's done, otherwise output it with 17 decimals. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: socket communication: send & receive doesnt work right
    ... So I don't want to send a string as bytes. ... I did a test of sending two doubles: ... public void send_doubles(double vals, int len) throws ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: [PHP] Re: optimilize web page loading
    ... doubles for the email. ... Philip Thompson wrote: ... the PHP parser doesn't have to evaluate ... this string to determine if anything needs to be translated (e.g., ...
    (php.general)
  • Re: [PHP] Re: optimilize web page loading
    ... doubles for the email. ... Philip Thompson wrote: ... the PHP parser doesn't have to evaluate ... this string to determine if anything needs to be translated (e.g., ...
    (php.general)