📄 wzrangetest.html
字号:
<TITLE>WZ 2.0 - %</TITLE>
<PRE>
#include "<A HREF="wzrange.hxx">wzrange.hxx</A>"
</PRE>
<H1>Range</H1>
<P>Here we show how the class <B>wzRange</B> has to be used. This
class allows to handle index ranges. For example, we use it to handle
the node and cell indices in our grids. A wzRange contains an array
manager for the template <A HREF="wzarraytest.html">wzArray</A>.
Thus, together with a wzRange we use different wzArray's.
<P>First, for every item we need some application-dependent data,
something like this:
<PRE>
typedef struct{
wzIndex a;
wzFloat b;
}structure,*pointer;
const wzIndex marker = 0xffffffff;
void test()
{
wzRange range(sizeof(structure),marker);
</PRE>
<P>This marker will be used to mark items which are not used. But
once the item is used, you can use the related field for other
information. By coincidence, you may write the marker into this
field. In this case, some calls may not work correctly.
<P>Thus, use a value for the marker you will never write yourself
into the related field (the first field of your structure).
<PRE>
wzArray<structure> s(range.base);
wzArray<wzIndex> a(range.base,wzOffsetOf(structure,a));
wzArray<wzFloat> b(range.base,wzOffset(pointer,b));
</PRE>
<P>Here you see how you can access the fields which are used
internally. They are used only for invalid items, thus, it would be a
waste of memory not to use them, but you have to be a little bit
careful: not to write the marker value into the field a[i] and not to
modify the fields a[i] and b[i] for invalid items i.
<P>A possibility to detect related errors may be not to use this
declaration during debugging, but:
<PRE>
wzArray<wzIndex> array(range);
</PRE>
<P>Now, let's see how to create new items:
<PRE>
wzIndex i1 = range(), i2=range.create(), i3=range(50);
now(i1==1); now(i2==2); now(i3==3);
s[i2].a=5; now(a[2]==5);
b[i2]=6; now(s[i2].b==6);
</PRE>
<P>Let's look how to organize a loop over the whole range:
<PRE>
now(range.first()==1); now(range.last()==52);
int i=65,j=0;
wzRangeLoop(range,i){ // i is now the loop parameter;
a[i] = 5; b[i] = 10;
j++; now(i==j);
}
now(j==52);
now(i==53); now(range.invalid(i)); // be careful!!!
</PRE>
<P>Note that the macro <B>wzRangeLoop</B> works also if some of the items
have been destroyed:
<PRE>
now(range.valid(i2)); now(!range.free(i2));
range.destroy(i2);
now(range.invalid(i2)); now(range.free(i2)); now(a[i2]==marker);
j=0;
wzRangeLoop(range,i){
j++; now(i != i2);
}
now(j==51); // one item less than before.
</PRE>
<P>Now, let's open a block and allocate a local field:
<PRE>
{wzArray<wzIndex> field(range);
wzAddress adr1,adr2;
field[50]= 8; // initial memory is allocated because of the range
adr1 = &(field[50]); range.create(5000); adr2 = &(field[50]);
now(adr1 != adr2); // Be careful! The field has been reallocated!
now(field[50] == 8); // But the value hasn't changed.
}
i2 = range.create(); // previously destroyed item 2 will be returned
now(i2==2); now(a[i2] == 0);
</PRE>
<P>
<A NAME="errors"> <hr></A>
<H2><A HREF="errors.html">Errors</A></H2>
<P>This package does not throw specific errors.
<A NAME="end"> <hr></A>
<PRE>
}
</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -