Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Storing large number of values in 2D array (http://www.velocityreviews.com/forums/t444816-storing-large-number-of-values-in-2d-array.html)

rajus 10-20-2006 06:15 PM

Storing large number of values in 2D array
 
I want to store the (x,y) coordinates of about 10,000 points in a 2D
array.How can I store them and retrieve them later?


Lew Pitcher 10-20-2006 06:30 PM

Re: Storing large number of values in 2D array
 

rajus wrote:
> I want to store the (x,y) coordinates of about 10,000 points in a 2D
> array.How can I store them and retrieve them later?


I'm afraid that your question doesn't relate specifically to the use of
the C langage, and is off topic in this forum. You might try one of the
comp.programming groups where they discuss algorithms and data
structures.

But, before you go, why don't you think about a sparse matrix
implementation?
Something like
struct { long x; long y; } coord[10000];

HTH
--
Lew Pitcher


jacob navia 10-20-2006 06:46 PM

Re: Storing large number of values in 2D array
 
rajus wrote:
> I want to store the (x,y) coordinates of about 10,000 points in a 2D
> array.How can I store them and retrieve them later?
>

You have 10 000 x 2 points i.e. 20 000 points to store.
To minimize space you could use 12 bits (coordinates up
to 4096x4096) and store 5 points per 64 bits, making it
around 32 000 bytes for the 2D array.

Using only 10 bits (1024x1024 coordinates) you would store
6 points per 64 bits unit, around 26 666 bytes for the array.

You would need to develop a small package that reads any sequence of
10 or 12 bits in the array, making a function like

uint32 GetPointsCoords(int x,int y)
{
// Here you calculate the offset in the array
// of your point, and return a 32 bit result
// containing in the upper 16 bits the y coordinate
// and in the lower 16 bits the x coordinate
// for instance.
}

You can save yourself development time of course if you
just make

unsigned short array[10000][2];

This would take 40 000 bytes, an increase of 14 000
but no development effort, you just index the array:

x = array[2357][0];
y = array[2357][1];

With short in 16 bits, you would get enough coordinate space to go
to 65535 x 65535 screens. The largest screens today go to
2500 x 2000 so your software will last (for a while...)

jacob

Al Balmer 10-20-2006 06:57 PM

Re: Storing large number of values in 2D array
 
On Fri, 20 Oct 2006 20:46:28 +0200, jacob navia
<jacob@jacob.remcomp.fr> wrote:

>rajus wrote:
>> I want to store the (x,y) coordinates of about 10,000 points in a 2D
>> array.How can I store them and retrieve them later?
>>

>You have 10 000 x 2 points i.e. 20 000 points to store.
>To minimize space you could use 12 bits (coordinates up
>to 4096x4096) and store 5 points per 64 bits, making it
>around 32 000 bytes for the 2D array.


Of course, the extra code required to do this would probably make up
for the saved space. I like Lew's suggestion, using whatever size
integer is appropriate.

--
Al Balmer
Sun City, AZ

dcorbit@connx.com 10-20-2006 07:17 PM

Re: Storing large number of values in 2D array
 
rajus wrote:
> I want to store the (x,y) coordinates of about 10,000 points in a 2D
> array.How can I store them and retrieve them later?


#include <stdlib.h>
#include <stdio.h>

typedef struct point {
double x;
double y;
} point, *pointAddr;

static size_t dim = 10000u;

int main(void)
{
size_t i;

/* The use of calloc to zero array of floats is not portable. */
/* Allocate array with malloc() */
pointAddr array = malloc(dim * sizeof( point) );
/* Check for success */
if (array == NULL) {
puts("FATAL ERROR: Out of memory.");
exit(EXIT_FAILURE);
}
/* Initialize array to a known state. */
for (i = 0; i < dim; i++) {
array[i].x = 0;
array[i].y = 0;
}

/* do whatever you like with the array here... */

return 0;
}


dcorbit@connx.com 10-20-2006 07:24 PM

Re: Storing large number of values in 2D array
 

Lew Pitcher wrote:
> rajus wrote:
> > I want to store the (x,y) coordinates of about 10,000 points in a 2D
> > array.How can I store them and retrieve them later?

>
> I'm afraid that your question doesn't relate specifically to the use of
> the C langage, and is off topic in this forum. You might try one of the
> comp.programming groups where they discuss algorithms and data
> structures.


If this isn't a C question, then I don't know what one is.

> But, before you go, why don't you think about a sparse matrix
> implementation?
> Something like
> struct { long x; long y; } coord[10000];


With 10,000 points, a sparse array implementation is a total waste of
time and energy.

Even with doubles that's {typically} 16*10,000 = 160,000 bytes.

Considering 1 GB of PC3200 DDR2 ECC ram at $112:
http://www.ramseeker.com/pc/index.php
that's about 1/2 cent worth of memory. Hardly worth worrying about
sparse implementations.

IMO-YMMV.

> HTH
> --
> Lew Pitcher



dcorbit@connx.com 10-20-2006 08:25 PM

Re: Storing large number of values in 2D array
 

rajus wrote:
> I want to store the (x,y) coordinates of about 10,000 points in a 2D
> array.How can I store them and retrieve them later?


Something a bit ambiguous about your post is:
"How can I store them and retrieve them later?"

You can obviously poke them into an array and pull them out when you
need them. The code I posted earlier is an example of how you might
prepare an array of 2d points for use.

But you might be talking about permanent storage. In such a case, the
C answer is to use fwrite() to save them to disk and fread() to read
them back into memory. This answer is not portable across different
systems because different systems have different binary formats. In
other words, if you write out binary data using fwrite() then you
should only read the data back into memory using fread() on machines
with similar architecture. So if you wrote the data out on a Windows
machine, it probably won't read in correctly on a SPARC machine.

Also, if you plan to have many sets of data (or need the binary
compatibility referred to above), you might want to store them in a
database.

We do not have enough information for a very clear answer if you want
to know about long term storage and retrieval.


dcorbit@connx.com 10-20-2006 08:30 PM

Re: Storing large number of values in 2D array
 
dcorbit@connx.com wrote:
[snip]
> With 10,000 points, a sparse array implementation is a total waste of
> time and energy.
>
> Even with doubles that's {typically} 16*10,000 = 160,000 bytes.
>
> Considering 1 GB of PC3200 DDR2 ECC ram at $112:
> http://www.ramseeker.com/pc/index.php
> that's about 1/2 cent worth of memory. Hardly worth worrying about
> sparse implementations.


Time to temper my remark. I keep forgetting that much of (most?) C
programming is actually embedded work. On an embedded system, you
might have very limited resources. In such a circumstance, a skiplist
might be a good way to store a sparse vector.

There are advanced codes available for sparse matrices and vectors, but
generally they are designed for huge data sets.


William Hughes 10-20-2006 08:42 PM

Re: Storing large number of values in 2D array
 

dcorbit@connx.com wrote:
> Lew Pitcher wrote:
> > rajus wrote:
> > > I want to store the (x,y) coordinates of about 10,000 points in a 2D
> > > array.How can I store them and retrieve them later?

> >
> > I'm afraid that your question doesn't relate specifically to the use of
> > the C langage, and is off topic in this forum. You might try one of the
> > comp.programming groups where they discuss algorithms and data
> > structures.

>
> If this isn't a C question, then I don't know what one is.


You got that one right!

- William Hughes


Keith Thompson 10-20-2006 09:02 PM

Re: Storing large number of values in 2D array
 
dcorbit@connx.com writes:
> Lew Pitcher wrote:
>> rajus wrote:
>> > I want to store the (x,y) coordinates of about 10,000 points in a 2D
>> > array.How can I store them and retrieve them later?

>>
>> I'm afraid that your question doesn't relate specifically to the use of
>> the C langage, and is off topic in this forum. You might try one of the
>> comp.programming groups where they discuss algorithms and data
>> structures.

>
> If this isn't a C question, then I don't know what one is.
>
>> But, before you go, why don't you think about a sparse matrix
>> implementation?
>> Something like
>> struct { long x; long y; } coord[10000];

>
> With 10,000 points, a sparse array implementation is a total waste of
> time and energy.
>
> Even with doubles that's {typically} 16*10,000 = 160,000 bytes.


Suppose each (x,y) coordinate consists of a pair of integers, each of
which can be in the range 0..999999. If the OP wants to store
information about each point in the corresponding element of a 2D
array, the array would have to have one trillion (10**12) elements.
Some sort of sparse array representation is just about mandatory.

I think the OP wants to store information about a point, and use the
coordinates of the point to retrieve it later. I might consider using
a hash table, hashing the (x,y) coordinate pair to obtain the
retrieval key.

But we need more information from the OP. How are the (x,y)
coordinates represented (int, double, whatever)? What are the
possible ranges? What information to you need for each point? How
important is fast access to each point?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


All times are GMT. The time now is 12:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.