pike数据类型-- mapping



      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.









 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值