Indexer (programming): Difference between revisions

Content deleted Content added
Add Rust example
Adding local short description: "Object-oriented programming concept", overriding Wikidata description "overloadable collection indexing operator"
 
(4 intermediate revisions by 3 users not shown)
Line 1:
{{Short description|Object-oriented programming concept}}
{{Confusing|reason=is this a C# or a general OO concept? Surely the interesting thing is the use of the keyword 'this', not what is emphasised here? Why is this whole article only sourced to one C# forum post (the other forum post is a dead link). Maybe an example of use of the example class would demonstrate the purpose of the construct. Wikipedia is not a manual, guidebook or textbook [[WP:NOTHOWTO]].|date=April 2015}}
In [[object-oriented programming]], an '''indexer''' allows instances of a particular class or struct to be indexed just like arrays.<ref>{{cite web|access-date=2011-08-01 |author=jagadish980 |date=2008-01-29 |website=SURESHKUMAR.NET FORUMS |title=C# - What is an indexer in C# |url=http://forums.sureshkumar.net/vb-asp-net-interview-technical-questions/16320-c-what-indexer-c.html |archive-url=https://web.archive.org/web/20090922193214/http://forums.sureshkumar.net/vb-asp-net-interview-technical-questions/16320-c-what-indexer-c.html |archive-date=September 22, 2009 }}</ref> It is a form of [[operator overloading]].
 
Line 12:
double& operator[](int i) { return data[i]; }
};
 
#include <iostream>
 
int main() {
vector v(3);
for (int i = 0; i < v.size; i++) v[i] = i + 1;
for (int i = 0; i < v.size; i++) std::cout << v[i] << "\n";
return 0;
}
</syntaxhighlight>
 
=== C sharp# ===
Indexers are implemented through the get and set [[Mutator method|accessor]]s for the {{C_sharp|operator[]}}. They are similar to [[Property (programming)|properties]], but differ by not being [[Static method|static]], and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit {{C_sharp|value}} parameter.
 
Line 28 ⟶ 30:
public class Vector
{
private double[] data_data;
 
public Vector(int n) { data = new double[n]; }
public Vector(int size => data.Length;n)
{
public double this[int i] { get => data[i]; set => data[i] = value; }
public Vector(int n) { data_data = new double[n]; }
}
 
public int Size => _data.Length;
 
public double this[int i]
{
get => _data[i];
set => _data[i] = value;
}
 
public static void Main()
{
var vvector = new Vector(3);
for (intvar i = 0; i < vvector.sizeSize; i++) v[i] = i + 1;
for (int vector[i] = 0; i < v.size; i++) System.Console.WriteLine(v[i])1;
for (var i = 0; i < vector.Size; i++)
System.Console.WriteLine(vector[i]);
}
}
Line 102 ⟶ 116:
In [[PHP]] indexing can be implemented via the predefined {{code|ArrayAccess}} interface,<ref>{{cite web|url=https://www.php.net/manual/en/class.arrayaccess.php|title=PHP ArrayAccess interface}}</ref>
<syntaxhighlight lang="php">
class Vector implements ArrayAccess {
<?php
{
class Vector implements ArrayAccess {
function __construct(int $n) {
$this->size = $n;
$this->data = array_fill(0, $n, 0);
}
 
public function offsetGet($offset): mixed {
return $this->data[$offset];
}
 
public function offsetSet($offset, $value): void {
$this->data[$offset] = $value;
}
 
public function offsetExists($offset): bool {}
 
public function offsetUnset($offset): void {}
}
 
$vvector = new Vector(3);
for ($i = 0; $i < $v->size; $i++) $v[$i] = $i + 1;
 
for ($i = 0; $i < $v->size; $i++) print "{$v[$i]}\n";
for ($i = 0; $i < $vvector->size; $i++) $vvector[$i] = $i + 1;
?>
for ($i = 0; $i < $vvector->size; $i++) print "{$vvector[$i]}\n";
</syntaxhighlight>
 
Line 127 ⟶ 146:
<syntaxhighlight lang="python">
import array
 
class Vector(object):
def __init__(self, n: int):
self.size = n
self.data = array.array("d", [0.0] * n)
def __getitem__(self, i): return self.data[i]
def __setitem__(self, i, value): self.data[i] = value
 
def __getitem__(self, i): return self.data[i]int):
v = Vector(3)
return self.data[i]
for i in range(v.size): v[i] = i + 1
 
for i in range(v.size): print(v[i])
def __setitem__(self, i: int, value): self.data[i] = value
self.data[i] = value
 
vvector = Vector(3)
for i in range(vvector.size): v[i] = i + 1
vector[i] = i + 1
for i in range(vvector.size): print(v[i])
print(vector[i])
</syntaxhighlight>
 
Line 176 ⟶ 202:
assert_eq!(nucleotide_count[Nucleotide::G], 10);
assert_eq!(nucleotide_count[Nucleotide::T], 12);
</syntaxhighlight>
 
=== Smalltalk ===
In [[Smalltalk]] one can emulate indexing by (e.g.) defining the {{smalltalk|get:}} and {{smalltalk|set:value:}} instance methods. For example, in [[GNU Smalltalk]],
<syntaxhighlight lang="smalltalk">
Object subclass: vector [ |data| ]
vector class extend [ new: n [ |v| v:=super new. v init: n. ^v] ]
vector extend [ init: n [ data:= Array new: n ] ]
vector extend [ size [ ^(data size) ] ]
vector extend [ get: i [ ^(data at: i) ] ]
vector extend [ set: i value: x [ data at: i put: x ] ]
v:=vector new: 3
1 to: (v size) do: [:i| v set: i value: (i+1) ]
1 to: (v size) do: [:i| (v get: i) printNl ]
</syntaxhighlight>
 
== See also ==
{{Portal|Computer programming}}
* [[Mutator method]]