mappings只是特殊的数组(array).mapping比较慢,并且消耗内存也比较大,因此,mappiing不能完全取代数组.特殊的是,不止整数,所有类型都可以做索引.我们可以想象mapping是这样:
mapping中每一个元素都是key-value对.具有很方便的查找功能,这种功能可以很快的找到任何一个key所对应的value.现在,假如我们把mapping叫做m,m[i]会很快的找到key为i的value.假如key没有找到,会返回0.假如我们对已有的key赋值,他会覆盖原来的值.如果被赋值的key不存在,则一对新的key-value会添加到mapping中, 定义一个常量的mapping是很简单的:
([]) //空mapping
([1:2]) //
(["one":1,"two",2]) //
([1:({2.0})],"":([])) //
和数组相比,mapping可以可以是任意类型的值.最大的不同是key也可以是任意类型.另外需要注意的是key-value对没有一定的顺序.你不能指定第十四对key-value,因为没法区别那对是第十四.因此,不能对mapping做排序操作.下面是mapping的一些重要操作:
m[index]
可以重新赋值,增加值.
+,-,|,&,^
和数组一样,这些操作符,也可以使用,不同的是,mapping作用于key.在一些特殊情况,当value从两个mapping值取得时候,它会选择操作符右边的值.这样它使用起来比+=方便多了.看一些例子:
([1:3,3:1]) + ([2:5, 3:7]) => ([1:3,2:5,3:7])
([1:3,3:1]) - ([2:5, 3:7]) => ([1:3])
([1:3,3:1]) | ([2:5, 3:7]) => ([1:3,2:5,3:7])
([1:3,3:1]) & ([2:5, 3:7]) => ([3:7])
([1:3,3:1]) ^ ([2:5, 3:7]) => ([1:3,2:5])
a==b
相同是返回1,否则返回0
a!=b
不等时返回1,否则返回1
array indices(mapping m)
返回m的所有包含有所有key的数组
mixed m_delete(mapping m,mixed ind)
移除key为ind的key-value对,返回ind对应的value
int mappingp(mixed m)
判断m是否为mapping类型,是返回1,否则返回0
mapping mkmapping(array ind,array val)
把数组ind和val构造一个mapping, ([ind[0]:val[0],ind[1]:val[1],...,ind[i],val[i]])
mapping replace(mapping m,mixed from, mixed to)
把m中所有value为from的全部替换成to,并且生成新的mapping
mixed search(mapping m,mixed val)
返回第一对值为val的key
int sizeof(mapping m)
返回mapping的key-value的对数
array values(mapping m)
同indices,不同的是放回的是所有value的数组.如果同一个mapping连续操作indices和values,两者之间没有任何操作,得到的数组顺序是相同的.它们可以作为mkmapping的两个参数,重新组成mapping m.
int zefo_type(mixed t)
当在mapping中找不到某个key的时候,会返回0.但是,问题是假如0在mapping中也是value的时候怎么办.这个方法就是告诉你两者间的区别.zero_type(m[ind])返回1,说明这个ind不存在与mapping m中,如果存在,则返回1以外的其他数字.
下面是原文:
引用
Mappings are are really just more generic arrays. However, they are slower and use more memory than arrays, so they cannot replace arrays completely. What makes mappings special is that they can be indexed on other things than integers. We can imagine that a mapping looks like this:
Each index-value pair is floating around freely inside the mapping. There is exactly one value for each index. We also have a (magical) lookup function. This lookup function can find any index in the mapping very quickly. Now, if the mapping is called m and we index it like this: m [ i ] the lookup function will quickly find the index i in the mapping and return the corresponding value. If the index is not found, zero is returned instead. If we on the other hand assign an index in the mapping the value will instead be overwritten with the new value. If the index is not found when assigning, a new index-value pair will be added to the mapping. Writing a constant mapping is easy:
([ ]) // Empty mapping
([ 1:2 ]) // Mapping with one index-value pair, the 1 is the index
([ "one":1, "two":2 ]) // Mapping which maps words to numbers
([ 1:({2.0}), "":([]), ]) // Mapping with lots of different types
As with arrays, mappings can contain any type. The main difference is that the index can be any type too. Also note that the index-value pairs in a mapping are not stored in a specific order. You can not refer to the fourteenth key-index pair, since there is no way of telling which one is the fourteenth. Because of this, you cannot use the range operator on mappings.
The following operators and functions are important:
indexing ( m [ ind ] )
As discussed above, indexing is used to retrieve, store and add values to the mapping.
addition, subtraction, union, intersection and xor
All these operators works exactly as on arrays, with the difference that they operate on the indices. In those cases when the value can come from either mapping, it will be taken from the right side of the operator. This makes it easier to add new values to a mapping with +=. Some examples:
([1:3, 3:1]) + ([2:5, 3:7]) returns ([1:3, 2:5, 3:7 ])
([1:3, 3:1]) - ([2:5, 3:7]) returns ([1:3])
([1:3, 3:1]) | ([2:5, 3:7]) returns ([1:3, 2:5, 3:7 ])
([1:3, 3:1]) & ([2:5, 3:7]) returns ([3:7])
([1:3, 3:1]) ^ ([2:5, 3:7]) returns ([1:3, 2:5])
same ( a == b )
Returns 1 if a is the same mapping as b, 0 otherwise.
not same ( a != b )
Returns 0 if a is the same mapping as b, 1 otherwise.
array indices(mapping m)
Indices returns an array containing all the indices in the mapping m.
mixed m_delete(mapping m, mixed ind)
This function removes the index-value pair with the index ind from the mapping m. It will return the value that was removed.
int mappingp(mixed m)
This function returns 1 if m is a mapping, 0 otherwise.
mapping mkmapping(array ind, array val)
This function constructs a mapping from the two arrays ind and val. Element 0 in ind and element 0 in val becomes one index-value pair. Element 1 in ind and element 1 in val becomes another index-value pair, and so on..
mapping replace(mapping m, mixed from, mixed to)
This function creates a copy of the mapping m with all values equal to from replaced by to.
mixed search(mapping m, mixed val)
This function returns the index of the 'first' index-value pair which has the value val.
int sizeof(mapping m)
Sizeof returns how many index-value pairs there are in the mapping.
array values(mapping m)
This function does the same as indices, but returns an array with all the values instead. If indices and values are called on the same mapping after each other, without any other mapping operations in between, the returned arrays will be in the same order. They can in turn be used as arguments to mkmapping to rebuild the mapping m again.
int zero_type(mixed t)
When indexing a mapping and the index is not found, zero is returned. However, problems can arise if you have also stored zeroes in the mapping. This function allows you to see the difference between the two cases. If zero_type(m [ ind ]) returns 1, it means that the value was not present in the mapping. If the value was present in the mapping, zero_type will return something else than 1.
分享到:
相关推荐
- Go语言的起源与特点:Go语言由Robert Griesemer、Rob Pike和Ken Thompson三位开发者设计,强调简洁、并发性和效率。 - 语法结构:包括变量声明、函数定义、控制流程语句(如if、for、switch)以及Go语言特有的...
2. **ORM概念**:对象关系映射(Object-Relational Mapping,ORM)是一种编程技术,用于将关系数据库的数据模型映射到面向对象的软件中。ORM简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据库,而...
内容概要:本文详细介绍了环形振荡器(Ring VCO)和锁相环(PLL)的设计、仿真与测试方法。针对初学者,提供了从基础电路理论到具体实操步骤的全面指导,涵盖了Cadence软件的使用、PSS/PNOISE仿真、调谐曲线绘制、相位噪声优化以及眼图调试等方面的内容。文中不仅讲解了基本概念和技术要点,还分享了许多实用的操作技巧和常见问题解决方案,如如何正确设置仿真参数、优化相位噪声、处理电源纹波等问题。此外,还附赠了一份详细的ADE_XL用户指南,帮助读者深入理解和掌握相关技术。 适合人群:对模拟IC设计感兴趣的初学者及有一定基础的研发人员。 使用场景及目标:①掌握环形振荡器的基本原理及其在Cadence中的仿真方法;②学会如何进行调谐曲线、相位噪声等关键性能指标的仿真与优化;③提高解决实际工程问题的能力,如电源纹波抑制、眼图调试等。 其他说明:本文特别强调了实践经验的重要性,鼓励读者动手实践并在实践中不断积累经验。同时提醒读者注意一些容易忽视但至关重要的细节,如仿真参数的选择和特殊条件下可能出现的问题。
【java】基于Java+Springboot+Vue的社区医院管理系统(源代码+数据库+万字论文).zip
scratch少儿编程逻辑思维游戏源码-大盗之魂.zip
scratch少儿编程逻辑思维游戏源码-弹跳猫.zip
scratch少儿编程逻辑思维游戏源码-城堡逃脱.zip
内容概要:本文探讨了马里兰电池数据集及其在电池剩余寿命(RUL)预测中的应用,重点介绍了RNN(循环神经网络)和LSTM(长短期记忆网络)这两种深度学习模型的应用。文章首先概述了马里兰电池数据集的特点,它记录了电池在不同环境和使用条件下的关键指标变化,为电池寿命预测提供了宝贵的数据支持。接着,文章详细解释了RNN和LSTM模型的工作原理以及它们在处理序列数据方面的优势,特别是LSTM在处理长时间依赖关系时表现出色。随后,通过一个简单的Python代码示例,展示了如何使用Keras库构建LSTM模型来进行RUL预测,包括数据预处理、模型构建、编译、训练和预测的具体步骤。最后,文章总结了RNN和LSTM模型在电池RUL预测中的重要性和潜力,并展望了未来的研究方向。 适合人群:对电池技术和机器学习感兴趣的科研人员、工程师及学生。 使用场景及目标:适用于希望利用深度学习技术提升电池管理系统的准确性和效率的人群。主要目标是通过学习历史数据,预测电池未来的状态,从而为新电池设计和现有电池维护提供科学依据。 其他说明:文中提供的代码示例仅作为入门指南,实际应用中需要根据具体情况调整模型结构和参数设置,并可能需要高性能计算资源来加速训练过程。
scratch少儿编程逻辑思维游戏源码-道场战场:战斗模拟器.zip
内容概要:本文详细介绍了基于STM32的低压无感BLDC(直流无刷电机)方波方案的全功能版本。该方案采用未封装库的源码,支持脉冲注入用于识别电机转子初始位置,并兼容国产芯片。文中提供了详细的硬件设计(包括原理图、丝印图)、软件实现(特别是脉冲注入和换相逻辑),以及调试方法和技巧。此外,还讨论了霍尔接口的兼容性和自动校准流程,确保系统能够适应不同类型的电机负载。 适合人群:具有一定嵌入式开发经验的研发人员和技术爱好者,尤其是对无感BLDC电机控制系统感兴趣的工程师。 使用场景及目标:①深入理解无感BLDC电机控制的底层逻辑;②掌握脉冲注入和换相逻辑的具体实现;③学习如何优化硬件设计和调试技巧,提高系统的可靠性和性能。 其他说明:该方案不仅适用于学术研究,也可应用于实际产品开发,帮助开发者快速搭建稳定的无感BLDC电机控制系统。
内容概要:本文档是2025年R语言数据分析综合教程,详细介绍了从环境配置到实战案例的完整流程。首先,涵盖环境配置与基础操作,包括安装R语言及RStudio IDE、常用数据分析包的安装与加载、数据导入及基础操作如读取CSV/Excel文件、数据查看与清洗等。接着,深入数据探索与可视化,讲解单变量统计、多变量关系分析,并通过`ggplot2`包进行基础图表和高级图表绘制。然后,进入统计建模与高级分析部分,涉及线性回归模型的构建与评估、主成分分析的数据降维与可视化以及分类资料分析中的卡方检验等内容。最后,通过Palmer企鹅数据集分析和医疗数据分类分析两个实战案例,巩固所学知识。此外,还推荐了中文教程和实战拓展资源,如知乎专栏、CSDN文章、GitHub开源项目和Kaggle数据集等; 适合人群:对R语言数据分析感兴趣的初学者及有一定编程基础的数据分析师; 使用场景及目标:①掌握R语言环境搭建与基础操作技能;②学会利用R语言进行数据探索、可视化及统计建模;③通过实战案例提升解决实际问题的能力; 其他说明:文档内容循序渐进,理论与实践相结合,适合自学或教学使用,读者可根据自身需求选择重点学习内容。
少儿编程scratch项目源代码文件案例素材-日本牛奶广告动画.zip
少儿编程scratch项目源代码文件案例素材-黏糊糊的圣诞节.zip
内容概要:本文详细介绍了基于MATLAB/Simulink平台构建的模块化多电平(MMC)统一潮流控制器(UPFC)仿真模型。首先探讨了MMC子模块的基本结构和电容电压均衡算法,接着讨论了环流抑制方法以及线路侧控制策略。文中还提供了具体的参数配置建议,如子模块数量、电容值、IGBT开关频率等,并展示了仿真的典型效果,包括电压提升和传输功率增加。此外,文章强调了该模型在新能源并网场景中的重要性和实用性。 适合人群:电力系统工程师、科研人员、高校师生等对高压输电线路和潮流控制感兴趣的读者。 使用场景及目标:适用于需要理解和掌握UPFC工作原理及其在MATLAB中的具体实现的研究人员和技术人员。目标是帮助读者搭建能够正常运行的仿真模型,理解UPFC在提高电力系统稳定性和灵活性方面的作用。 其他说明:文中提供的代码片段和参数设置有助于读者快速上手进行相关实验。同时,文章提到的谐波分析和性能评估方法也为进一步优化模型提供了指导。
内容概要:本文详细介绍了如何利用Simulink搭建电力系统稳态仿真模型。首先从同步发电机的选择和参数设置入手,强调了惯性常数H和基底电压的重要性和具体配置方法。接着讨论了负荷模型的选择,推荐使用更贴近实际的ZIP负荷模型而非简单的恒定阻抗模型。然后深入探讨了潮流计算的关键步骤,特别是参考节点的设定及其对后续分析的影响。对于线路建模部分,则提倡采用分布参数线路模块并将其分割为多段以提高仿真的准确性。此外,还提到了一些高级应用,如启用相量仿真模式加速仿真速度以及应对可能出现的暂态不稳定情况的方法。最后鼓励尝试加入风电场元素,进一步研究新能源接入后的系统行为。 适合人群:从事电力系统研究、设计或维护的技术人员,尤其是那些希望深入了解Simulink工具箱在电力工程领域应用的专业人士。 使用场景及目标:适用于需要构建电力系统稳态仿真环境的研究项目或教学课程;旨在帮助用户掌握Simulink平台的基本操作技能,同时培养解决复杂电力网络问题的能力。 其他说明:文中提供了大量MATLAB/Simulink代码片段作为辅助材料,便于读者理解和实践相关概念和技术要点。
scratch少儿编程逻辑思维游戏源码-地牢爬行者.zip
少儿编程scratch项目源代码文件案例素材-南瓜小子.zip
少儿编程scratch项目源代码文件案例素材-日落之旅.zip
scratch少儿编程逻辑思维游戏源码-弹回的球.zip
scratch少儿编程逻辑思维游戏源码-电源竞技场.zip