Special Arrays
#1

I was just looking though some of my old code and I found my code for iterators (aka foreach), bit arrays (storing 32 values in one variable) and binary trees (a very fast searchable structure). These all use arrays underneath but you don't ever want to directly access the arrays as the layout is special.

Anyway, to declare a bit array to store 10 elements you do:

pawn Code:
new Bit:arr[Bit_Bits(10)];
To declare a binary tree with 10 elements you do:

pawn Code:
new Bintree:arr[10][E_BINTREE_TREE];
And to delare an iterator with 10 elements you do:

pawn Code:
Iter_Create(arr, 10);
Given how far apart these were developed that's not really a surprise. However I decided to design a new syntax for special arrays, so I have, and I'm presenting it here in case other people ever want to use or learn from it. Basically the syntaxes are now:


pawn Code:
new BitArray:arr<10>;
new BinaryTree:arr<10>;
new Iterator:arr<10>;
All nice and the same. The code to do this is very simple:

pawn Code:
#define BinaryTree:%1<%2> Bintree:%1[%2][E_BINTREE_TREE]
#define BitArray:%1<%2> Bit:%1[Bit_Bits(%2)]
I've excluded the code for the iterator version as it uses the internals of the iterator library which should not be seen. The beauty of this method is that (for the most part) you can use this syntax in function declarations too:

pawn Code:
Bintree_Function(BinaryTree:a<>, b)
{
    // Do something using your private internal binary tree structure, but give users a common interface.
}
I say not always as you can't use the bit array example like this as without a size between the <>s, you will get code with "[Bit_Bits()]" in, which in this case is invalid as it's missing a required parameter.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)