# n lists?

hello everyone.
can anyone help me with a problem im having:
i want to read in data from a file and store it in an n list (say quad addresses)
i know how to read in data and store it in an array but i just want to store it in a list then set cases for each integer in each member.
say i have 0373
i want to store this in a list

1 | 0373

then set cases dependeing on the integer
say
case:
‘0’ x=0, y=0, z=0;
‘1’ etc etc

is there a good way to do this? preferably not too code intensive? any help though is much appreciated. thanks in advance

Have you read any book on programming which explains integer formats and bit operations?
This is extremely simple.

You can encode the tesseral addresses in bits of a machine word. I’m assuming the length of the addresses is identical over the whole list? If not you need to store the number of subdivisions, too.

You can encode the subdivision octree location in three bits each. A 32 integer could hold 10 subdivisions, your example would work with shorts. 0373 would be binary 000 011 111 011.
Create a binary mask with three bits set (here start at 0xE00) and a shift which extracts the 3 bits from the address per subdivision.

A hardcoded version for your case looks like this (you should calculate all magic constants yourself).

``````mask = 0x0E00;
shift = 9;
{
shift -= 3;
switch (subDiv)
{
case 0:
...
break
case 1:
...
}
}
``````

It would be beneficial to store the address starting from the lowest significant bits, then the for-loop would be even simpler.

ok but before i do that how do i take the addresses from a file and store them in a list instead of holding them in an array? or should i just take them straight out of the file and convert them (skip the storing of addresses and just store coordinates?)

Gee, I don’t know, how about looking at:

fopen,
fscanf,
fclose

Or if you want more complexity (and flexibility) use an XML API.

im familiar with these functions and have used them to store into an array but any idea on how they can be used to just store something in a list? an array is too complicated for something that is trivial in my opinion.

Err…?

How is a list simpler than an array?

Let’s say you don’t know how many elements there are going to be in your array, and let’s assume you write your file as:

1 0123
2 2340
3 6789

Simplest way is to loop through the file twice.
The first time you just count the number of ’
's.
Then you allocate your array - because now you now how many elements there are.
Then you loop through the file again.

Surely it can’t get much simpler than that?

(OK a linked list class would be simpler but then you need to implement that first, and it would be slower to address).

ok say i’ve already done that. read in a file. counted the amount of elements, then rewound the file to store each element in an array. how do i then go through each element and access integer by integer in each address?
i’ve done this so far (only the last relevant bit)

``````
rewind(fp);
int index = 0;
for(int i = 0; i < g_NumberOfAdds; i++)
{
fscanf(fp, "%f
index++;
}
fclose(fp);
}
``````

all of the addresses are around 7 integers long. i want to use bit operations. could i get some sample code of another function that basically takes this array as its parameters? would that do it?

1st of all, I wouldn’t load floats if I were you.
Use longs or ints, or:

Are you trying to do something like this:

1. look up a “script” to perform based on an integer (the n in your case)
2. walk through the script, then for every element in the script execute some action.

If so, I propose the following:
store the thing as an array of strings.

Store as:
01234
7653456
654
8768

read in (after looping to determine the number of elements) with something like:

``````char **scripts;
char buffer[256]; // something large
scripts=(char **)malloc(sizeof(char *)*scriptcount);

fp=fopen("scripts.txt","r");
index=0;
while(fscanf(fp,"%[^
]
",buffer)>0) {
scripts[index++]=strdup(buffer);
}
fclose(fp);

``````

To decode, just use:

``````// execute actions for script identified by index
char *currentscript;
currentscript = scripts[index];
for(int i=0;currentscript[i];i++) {
switch(currentscript[i]) {
default: // undefined value - don't do anything
break;
case '0': x=0; y=0; z=0;
break;
case '1':  etc etc
break;
}
}

``````

True, it’s not very space-efficient, but one byte per “command” isn’t that much, and it’s dead-easy and fast to decode.

i am…but would setting up counts for x,y and z
say

`````` xCount = 0;
yCount = 0;
zCount = 0;

``````

the for each case statement increment relevant counters

`````` case '2': ++yCount;
break;

case '3': ++xCount;
++yCount;
break;
``````

then would it be possible to take the xCount (which is then by my deduction the x coordinate of my address) and put these into an array as it recurses through the for loop? or am i a total newb and got it all wrong? please clear this up for me please. kind regards

You should do what you think is best.

In your example, say you have a list of 233. That would result in: yCount=3, xCount=2. If that’s what you want: great.
However, if all you want is to determine x,y,z from a file, you can achieve that in less steps - by just storing the coordinates directly in the file.
Somehow I think what you want is more complex…

It would help if you explained what the goal is of those strings. Are they, for instance, meant to modify vertex coordinates?

the purpose of obtaining the coordinates from the address is to plot the point of a voxel and set its dimensions depending on the level of subdivision. at the moment i have a lot of code but the main drawing one is the Debug one. It takes the 8 points of a cube determined from the centre then stores each line in turn. (this is simply to show my octree subdividing) the data i will be working with will have quite a lot of addresses which, when drawn as i’ve mentioned, will make a 3D volumetric representation of whatever is in the tesseral data set. that help much? how would i go about your method? simply storing them in one step?

hi again. in your scripts example above. shouldnt there be a memory allocation to hold what is read in etc. ive managed to write all of my code and get it to compile and build but im getting a memory error that points to the line

`````` for(int i=0;currentaddress[i];i++)
``````

can anyone help?

Two possible causes:

`````` scripts=(char **)malloc(sizeof(char *)*scriptcount);

``````

Requires that you count the scripts before you load them into memory. I did state that you needed to do that first, but you probably missed it.

`````` currentscript = scripts[index];
``````

Requires that 0 <= index < scriptcount

W.r.t. the voxel stuff: I’m sorry for not responding. I didn’t quite get what you want to do, but I suspect I would have to read up on voxels, and I don’t have time for that I’m afraid.

voxels are just pixels. where voxels are volume rendering. all about volume. i had already counted the number of scripts/addresses being read in from the file using a temporary variable to hold the amount then reading them into an array NumberOfAdds. I don’t think your mentioned cause it whats causing it

just a question…do i realy need a pointer to a pointer to an item? and is there a way of allowing switch statements to take arrays as there arguments?

ok sorry to pester…but i will anyway. can anyone tell me the best way of doing the following (with regards to what i’ve mentioned):>

ill put it in a mixture of code and pseudocode

``````until EOF do
{
string = input
len = length(string)
for i = 1 to len do
{
ch = substr(string, i-1, 1)
switch(ch)
{
case '0' :
break
case '1' : etc etc
}
}
}

``````

this is the gist of what i want to do…any hints or snippets on how to do this would be appreciated

does anyone know how to do this please?

anyone? i’ve figured it out as far as this. i just need to use a string as input. the input mainly being each tesseral address. i’ll no doubt have to store the length of the character for later use. each string being a word and the length being the wordlength. then the strings being used as characters in the switch statement. can anyone shed some light please?