Author Topic: using hashtables with xdata  (Read 2716 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3647
  • (x-in)->[process]->(y-out) ... simples!
using hashtables with xdata
« on: June 06, 2010, 08:51:36 PM »
Hi All,

What's your opinion/s on using hastables for retrieving xdata, so far I've had no issues and it works quite well, it just saves iterating over the table and assigning var's one at a time, I can just dump the resbuf then work on it like an array, just another way to skin the cat I guess.

Here's a sample
Code: [Select]
private void GetXdata()
{
ResultBuffer rb = new ResultBuffer();
rb = (ResultBuffer)_solid.GetXDataForApplication(Utils.AppName);
if(rb != null)
{
_hasData = true;
Hashtable ht = new Hashtable();
IEnumerator iter = rb.GetEnumerator();
int i = 0; //count for ht
while (iter.MoveNext())
{
TypedValue tv = (TypedValue)iter.Current;
ht.Add(i,tv.Value);
i++;
}
//set the property values
Point3d x = new Point3d();
Point3d y = new Point3d();
Point3d z = new Point3d();
Point3d L = new Point3d();
Point3d R = new Point3d();
L = (Point3d)ht[1];
R = (Point3d)ht[2];
x = (Point3d)ht[3];
y = (Point3d)ht[4];
z = (Point3d)ht[5];
_start = L;
_end = R;
_vecX = new Vector3d(x.X,x.Y,x.Z);
_vecY = new Vector3d(y.X,y.Y,y.Z);
_vecZ = new Vector3d(z.X,z.Y,z.Z);
_sectionType = (string)ht[6];
_sectionName = (string)ht[7];
_itemNo = (string)ht[8];
}

}
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: using hashtables with xdata
« Reply #1 on: June 07, 2010, 07:02:16 PM »

Can't make time to play Mick, but how did this turn out for you ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3647
  • (x-in)->[process]->(y-out) ... simples!
Re: using hashtables with xdata
« Reply #2 on: June 07, 2010, 07:35:15 PM »
It works quite well, just thought I'd throw it out there for any potential downfalls to this method, I just find it neater I guess and I can then throw the hashtable around and use it like an array to get at the var's.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Glenn R

  • Guest
Re: using hashtables with xdata
« Reply #3 on: June 08, 2010, 06:01:41 AM »
Mick,

Any reason you're explicitly acquiring an enumerator and not using a 'foreach' construct?

There was a nice class written by Tony called TypedValueList around here somewhere...I believe Dan made some more based on the principles shown.

sinc

  • Guest
Re: using hashtables with xdata
« Reply #4 on: June 08, 2010, 08:20:09 AM »
You're also doing this:

Code: [Select]
Point3d x = new Point3d();
Point3d y = new Point3d();
Point3d z = new Point3d();
Point3d L = new Point3d();
Point3d R = new Point3d();
L = (Point3d)ht[1];
R = (Point3d)ht[2];
x = (Point3d)ht[3];
y = (Point3d)ht[4];
z = (Point3d)ht[5];

Your code is creating and destroying some unnecessary Point3d objects.  You could just do this instead:

Code: [Select]
Point3d L = (Point3d)ht[1];
Point3d R = (Point3d)ht[2];
Point3d x = (Point3d)ht[3];
Point3d y = (Point3d)ht[4];
Point3d z = (Point3d)ht[5];

MickD

  • King Gator
  • Posts: 3647
  • (x-in)->[process]->(y-out) ... simples!
Re: using hashtables with xdata
« Reply #5 on: June 08, 2010, 06:41:28 PM »
Glen,
This is actually pretty old code so I think either I didn't know about foreach back then or it wasn't implemented at the time :P, I would definitely use it now though. I'll have a dig around for that class too, thanks.

Sinc,
being old code I can't remember why I did it like that, perhaps a cast didn't work as resbuff may have stored the point data diferently than was need to do a quick cast, I'll have to have a deeper look, cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

sinc

  • Guest
Re: using hashtables with xdata
« Reply #6 on: June 08, 2010, 09:34:32 PM »
To get back to your initial question, is there a reason you're using a Hashtable?  In the code you've posted so far, you could use a simple array or list or dictionary, and it would work equally well.  Possibly even better, since it doesn't look like you're doing anything that takes advantage of the "bucket" system in hashes.

Also, you may want to look into the generics, such as Dictionary<TKey, TValue> and HashSet<T>.  The generics generally yield better performance than the older collection classes.  I'm not positive, but I think the only reason the older collection classes are still around is for backward-compatibility.  I don't think there's any other reason to use them over the generics.

MickD

  • King Gator
  • Posts: 3647
  • (x-in)->[process]->(y-out) ... simples!
Re: using hashtables with xdata
« Reply #7 on: June 09, 2010, 01:17:31 AM »
I think it was because I didn't need to declare a type with the HT, just a index number for a key and store the value what ever the resbuf type was. Does an array take different value types?? (been a while, been stuck in Inventor land :()

Being that there's a lot of mojo going on behind the scenes of any 'T' type containers I really wonder if they are quicker at all, but that's another discussion altogether and .net has some very good optimisations going on for those sorts of things.
If writing it again I would look into them for sure, these days I use Boo as much as I can and it only has lists, arrays and hashtables which keep things simple and easy to work with and can be 'sliced' like with Python. Sure, a slight perf hit maybe on large sets but nothing that I'd notice :).

cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

sinc

  • Guest
Re: using hashtables with xdata
« Reply #8 on: June 09, 2010, 09:36:35 AM »
The generics are part of .NET, so you should be able to use them in Boo, too.

MickD

  • King Gator
  • Posts: 3647
  • (x-in)->[process]->(y-out) ... simples!
Re: using hashtables with xdata
« Reply #9 on: June 10, 2010, 05:31:46 PM »
true, I'm not sure they're fully implemented into Boo though, I'll have to check, cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien