基本操作之——tuple类型

* 此示例演示如何使用运算符tuple_type,
* tuple_is_int、tuple_is_real、tuple_is_string、
* tuple_is_mixed和元素运算符
* tuple_type_elem、tuple_is_int_elem、tuple_is_real_elem、
* 和 tuple_is_string_elem。
* 此外,此示例还演示了如何使用强制转换(例如tuple_int)优化元组以加快处理速度。
*
* 如果要使用
* HDevelop 函数
UseTupleFunctions := false
*
* 生成一个tuple
create_matrix (3, 3, 0, MatrixID)
MyTuple := [42,43 - 1,'forty-two',42.0,42,HNULL,MatrixID]

 

*
* 获取元组 MyTuple 的元素类型,并将常量转换为人类可读的文本表示形式
if (UseTupleFunctions)
  tuple_type (MyTuple, TupleType)
  tuple_type_elem (MyTuple, ElemType)
else
  TupleType := type(MyTuple)

   ElemType := type_elem(MyTuple)

 endif

tuple_type_to_string (TupleType, TupleTypeString)

 tuple_type_to_string (ElemType, ElemTypeString)

 *

* 此外,元组及其元素的语义类型可以以字符串形式获得
tuple_sem_type (MyTuple, SemType)
tuple_sem_type_elem (MyTuple, SemTypeElem)

 

*
* 某些数据类型的测试也可用:
if (UseTupleFunctions)
  tuple_is_int (MyTuple, IsInt)
  tuple_is_real (MyTuple, IsReal)
  tuple_is_string (MyTuple, IsString)
  tuple_is_handle (MyTuple, IsHandle)
  tuple_is_mixed (MyTuple, IsMixed)
  tuple_is_int_elem (MyTuple, IsIntElem)
  tuple_is_real_elem (MyTuple, IsRealElem)
  tuple_is_string_elem (MyTuple, IsStringElem)
  tuple_is_handle_elem (MyTuple, IsHandleElem)
else
  IsInt := is_int(MyTuple)
  IsReal := is_real(MyTuple)
  IsString := is_string(MyTuple)
  IsHandle := is_handle(MyTuple)
  IsMixed := is_mixed(MyTuple)
  IsIntElem := is_int_elem(MyTuple)
  IsRealElem := is_real_elem(MyTuple)
  IsStringElem := is_string_elem(MyTuple)
  IsHandleElem := is_handle_elem(MyTuple)

 endif

*
* 作为类型运算符的示例,您可以计算元组 MyTuple 的整数类型的元素数
NumInt := sum(IsIntElem)

 

*
* 显示元组元素的类型
dev_inspect_ctrl ([TupleTypeString,MyTuple,ElemTypeString,SemTypeElem,IsIntElem,IsRealElem,IsStringElem,IsHandleElem])

 

*
*
* HALCON 优化了元组的处理,其中每个元素从一开始就属于同一类型或没有中断。
* 如果元组在成为混合元组后成为元组,其元素都属于同一类型的元组,则在运行时不会自动优化。
IntTuple := gen_tuple_const(20000,1)           *创建大小为20000,值均为1的整型tuple
MixedTuple := [gen_tuple_const(20000 - 1,1),1.0]
count_seconds (S1)
SortTuple1 := sort(IntTuple)
count_seconds (S2)
SortTuple2 := sort(MixedTuple)
count_seconds (S3)
TimeSortInt := (S2 - S1) * 1000
TimeSortMixed := (S3 - S2) * 1000
* 如果你有一个 H_TYPE_MIXED 类型的元组,你可以将其转换为纯元组,例如使用 int() 或 tuple_int 来提高使用该元组的后续运算符的速度。
if (type(MixedTuple) == H_TYPE_MIXED)
  CastTuple := int(MixedTuple)
endif
count_seconds (S4)
SortTuple2_Fast := sort(CastTuple)
count_seconds (S5)
TimeSortCast := (S5 - S4) * 1000
* Sorting of Tuple2 is now faster by a factor of about 3.
dev_inspect_ctrl ([TimeSortInt,TimeSortMixed,TimeSortCast])

 

posted @   echo-efun  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示