如何使用 Critcl ByteArray? [英] How to use Critcl ByteArray?
问题描述
我想试用 Critcl 以使用 Z 阶曲线 用于二维网格.我需要从 Critcl 获得的是分配、setter、getter 和一些大小信息.阅读 Critcl ByteArray 和 examples 并没有让我对如何做这件事充满信心.
I want to try out Critcl to enhance memory performance using a Z-order curve for a 2d-grid. What I need from Critcl is allocation, setter, getter and some size info. Reading about the Critcl ByteArray and examples does not make me confident on how to do it.
如何创建和返回一个 ByteArray(即 Z 阶曲线)?
How do I create and return a ByteArray (i.e. Z-order curve)?
在使用 ByteArray 时我应该知道哪些注意事项?
Any caveats I should know about when using ByteArray?
推荐答案
根据文档,您应该改用 bytes
类型(当您获得指向具有 len
字段,其中包含字节数,以及一个 s
字段,它是指向实际 只读 字节块的指针.(作为char *
而不是 unsigned char *
原因我不知道.为什么它不是 const
对我来说是另一个谜;在某些情况下确实如此,但您需要查看 o
字段才能弄清楚.)
According to the documentation, you should be using the bytes
type instead (when you get a pointer to a structure that has a len
field with the number of bytes in it, and an s
field that is the pointer to the actual read only block of bytes. (As a char *
and not an unsigned char *
for reasons I don't know. And why it isn't const
is another mystery to me; there are cases where that's indeed true, but you need to look at the o
field to figure that out.)
要返回字节数组,请使用 object
(或 object0
)结果类型,并使用例如 Tcl_NewByteArrayObj()
,或 Tcl_NewObj()
和 Tcl_SetByteArrayLength()
.
To return a byte array, you use the object
(or object0
) result type, and make the object with, for example, Tcl_NewByteArrayObj()
, or Tcl_NewObj()
and Tcl_SetByteArrayLength()
.
这是一个执行简单字节反转的示例(只是命令定义)(因为我根本不了解 Z 阶曲线):
Here's an example (just the command definition) that does trivial byte reversing (since I don't understand Z-order curves at all):
critcl::cproc example {bytes dataPtr} object0 {
Tcl_Obj *result = Tcl_NewObj();
unsigned char *targetBytes = Tcl_SetByteArrayLength(result, dataPtr->len);
for (int i = 0, j = dataPtr->len - 1; j >= 0; i++, j--) {
targetBytes[i] = (unsigned byte) dataPtr->s[j];
}
return result;
}
当然,您需要阅读 Critcl 使用指南 当让它工作时,如果你要产生错误(通过返回 NULL
),记得在解释器中设置一条错误消息.您可以通过使用 Tcl_Interp* interp
作为您使用 critcl::cproc
创建的命令的第一个伪参数来访问它(它已记录在案,但很容易错过).
Naturally, you'll want to read the Critcl usage guide when getting this to work, and if you're going to produce errors (by returning NULL
), remember to set an error message in the interpreter. You can get access to that by using Tcl_Interp* interp
as your first pseudo-argument to the command you create with critcl::cproc
(it's documented, but easily missed).
这篇关于如何使用 Critcl ByteArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!