Module:Exponential search/doc: Difference between revisions

From HIBIKIFORUM
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
Line 1: Line 1:
This module provides a generic exponential search algorithm. This kind of search can be useful when you want to find a key in some kind of sorted array, and you want to do it by checking as few array elements as possible. This could include situations like:
{{Documentation subpage}}
 
This module provides a generic [[Wikipedia:exponential search]] algorithm. This kind of search can be useful when you want to find a key in some kind of sorted array, and you want to do it by checking as few array elements as possible. This could include situations like:
* Finding the highest archive number in a set of archives without checking whether they all exist.
* Finding the highest archive number in a set of archives without checking whether they all exist.
* Finding the number of positional arguments in frame.args without having to expand the wikitext for each of them.
* Finding the number of positional arguments in frame.args without having to expand the wikitext for each of them.
Line 5: Line 7:
You shouldn't use this module if any of the following apply:
You shouldn't use this module if any of the following apply:
# You can use the [[mw:Extension:Scribunto/Lua reference manual#Length operator|Lua length operator]] to find what you need.
# You can use the [[mw:Extension:Scribunto/Lua reference manual#Length operator|Lua length operator]] to find what you need.
# Your array has any gaps in it. (In other words, any of the items before the final item is <code>nil</code>, e.g. <syntaxhighlight lang="lua" inline>{'foo', 'bar', nil, 'baz'}</syntaxhighlight>.) If you try and use this module on a sparse array, you might get an erroneous value.
# Your array has any gaps in it. (In other words, any of the items before the final item is {{code|nil}}, e.g. {{code|lang=lua|{'foo', 'bar', nil, 'baz'}}}.) If you try and use this module on a sparse array, you might get an erroneous value.
# Your array has less then about 10 items in it. It's possible to use this module for those arrays, but you will access most of the array elements anyway (perhaps some of them twice), and your code will be more complicated than if you just used a for loop.
# Your array has less then about 10 items in it. It's possible to use this module for those arrays, but you will access most of the array elements anyway (perhaps some of them twice), and your code will be more complicated than if you just used a for loop.


Line 18: Line 20:
You can then use the expSearch function with the following syntax:
You can then use the expSearch function with the following syntax:


<syntaxhighlight lang="lua">
{{code|expSearch(testFunc, init)}}
expSearch(testFunc, init)
</syntaxhighlight>


Parameters:
Parameters:
* '''testFunc''' - a test function for your array. This function should take a positive integer '''i''' as its first parameter. If the element corresponding to '''i''' is not in the array, then the function should return false or nil; and if it ''is'' in the array, then the function should return a truthy value (anything other than false or nil). (required)
* {{para|testFunc}} - a test function for your array. This function should take a positive integer <var>i</var> as its first parameter. If the element corresponding to <var>i</var> is not in the array, then the function should return false or nil; and if it ''is'' in the array, then the function should return a truthy value (anything other than false or nil). (required)
* '''init''' - the initial value of '''i''' to check. For advanced users. (optional)
* {{para|init}} - the initial value of <var>i</var> to check. For advanced users. (optional)
 
expSearch will return the highest value of <var>i</var> for which testFunc was truthy. If no values were truthy, the function will return nil.


expSearch will return the highest value of '''i''' for which testFunc was truthy. If no values were truthy, the function will return nil.
<includeonly>{{sandbox other||{{testcases other||


<includeonly>{{#ifeq:{{SUBPAGENAME}}|sandbox | |
<!-- Categories below this line, please -->
<!-- Categories below this line, please; interwikis at Wikidata -->
[[Category:Modules]]
[[Category:Modules]]
}}</includeonly>
}}}}</includeonly>

Latest revision as of 23:48, 11 June 2023

Light bulb.png }}
It contains usage information, categories and other content that is not part of the original module page.

This module provides a generic Wikipedia:exponential search algorithm. This kind of search can be useful when you want to find a key in some kind of sorted array, and you want to do it by checking as few array elements as possible. This could include situations like:

  • Finding the highest archive number in a set of archives without checking whether they all exist.
  • Finding the number of positional arguments in frame.args without having to expand the wikitext for each of them.

You shouldn't use this module if any of the following apply:

  1. You can use the Lua length operator to find what you need.
  2. Your array has any gaps in it. (In other words, any of the items before the final item is nil, e.g. {'foo', 'bar', nil, 'baz'}.) If you try and use this module on a sparse array, you might get an erroneous value.
  3. Your array has less then about 10 items in it. It's possible to use this module for those arrays, but you will access most of the array elements anyway (perhaps some of them twice), and your code will be more complicated than if you just used a for loop.

Usage

First, load the module.

local expSearch = require('Module:Exponential search')

You can then use the expSearch function with the following syntax:

expSearch(testFunc, init)

Parameters:

  • |testFunc= - a test function for your array. This function should take a positive integer i as its first parameter. If the element corresponding to i is not in the array, then the function should return false or nil; and if it is in the array, then the function should return a truthy value (anything other than false or nil). (required)
  • |init= - the initial value of i to check. For advanced users. (optional)

expSearch will return the highest value of i for which testFunc was truthy. If no values were truthy, the function will return nil.