06.08.2010, 01:08
(
Last edited by Y_Less; 15/08/2010 at 01:42 PM.
)
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:
To declare a binary tree with 10 elements you do:
And to delare an iterator with 10 elements you do:
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:
All nice and the same. The code to do this is very simple:
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:
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.
Anyway, to declare a bit array to store 10 elements you do:
pawn Code:
new Bit:arr[Bit_Bits(10)];
pawn Code:
new Bintree:arr[10][E_BINTREE_TREE];
pawn Code:
Iter_Create(arr, 10);
pawn Code:
new BitArray:arr<10>;
new BinaryTree:arr<10>;
new Iterator:arr<10>;
pawn Code:
#define BinaryTree:%1<%2> Bintree:%1[%2][E_BINTREE_TREE]
#define BitArray:%1<%2> Bit:%1[Bit_Bits(%2)]
pawn Code:
Bintree_Function(BinaryTree:a<>, b)
{
// Do something using your private internal binary tree structure, but give users a common interface.
}