官术网_书友最值得收藏!

Extending ipairs for use in sparse arrays

The ipairs function in the Lua language is used to iterate over entries in a sequence. This means every entry must be defined by the pair of key and value, where the key is the integer value. The main limitation of the ipairs function is that the keys must be consecutive numbers.

You can modify the ipairs function so that you can successfully iterate over entries with integer keys that are not consecutive. This is commonly seen in sparse arrays.

Getting ready

In this recipe, you'll need to define our own iterator function, which will return every entry of a sparse array in deterministic order. In this case, the iterator function can be included in your code as a global function to accompany pairs and ipairs functions; or you can put it in a Lua module file not to pollute the global environment space.

How to do it…

This code shows a very simple sparse array iterator without any caching:

function ipairs_sparse(t)
  -- tmpIndex will hold sorted indices, otherwise
  -- this iterator would be no different from pairs iterator
  local tmpIndex = {}
  local index, _ = next(t)
  while index do
    tmpIndex[#tmpIndex+1] = index
    index, _ = next(t, index)
  end
  -- sort table indices
  table.sort(tmpIndex)
  local j = 1

  return function()
    -- get index value
    local i = tmpIndex[j]
    j = j + 1
    if i then
      return i, t[i]
    end
  end
end

The following lines of code show the usage example for iteration over a sparse array;

-- table below contains unsorted sparse array
local t = {
  [10] = 'a', [2] = 'b', [5] = 'c', [100] = 'd', [99] = 'e',
}
-- iterate over entries
for i, v in ipairs_sparse(t) do
  print(i,v)
end

How it works…

The Lua language uses iterator functions in the control structure called the generic for. The generic for calls the iterator function for each new iteration and stops when the iterator function returns nil. The ipairs_sparse function works in the following steps:

  1. It builds a new index of keys from the table.
  2. It sorts the index table.
  3. It returns a closure where each call of the closure returns a consecutive index and a value from the sparse array.

Each call to ipairs_sparse prepares a new index table called index. The index consists of (integer, entry) pairs.

主站蜘蛛池模板: 江永县| 大埔县| 德化县| 东宁县| 朝阳县| 庆城县| 海宁市| 海城市| 和田市| 云阳县| 濮阳县| 屯门区| 安图县| 万全县| 永春县| 聂荣县| 时尚| 孝昌县| 大同市| 湖南省| 民勤县| 阿勒泰市| 隆安县| 西乌| 两当县| 拉孜县| 北海市| 邵东县| 屯昌县| 汶川县| 怀化市| 苏尼特左旗| 云龙县| 沙河市| 惠州市| 安泽县| 思茅市| 奉节县| 沂水县| 察隅县| 乌兰县|