Array and Table Extensions

配列拡張機能とテーブル拡張機能

These extensions add two new data structures to NetLogo, arrays and hash tables.

これらの拡張機能によってNetLogoでは配列とハッシュテーブルという2種類のデータ構造を扱うことができます。

When to use

いつ利用するのか

In general, anything you can do with an array or table, you could also just use a list for. But you may want to consider using an array or table instead for speed reasons. All three data structures (list, array, and table) have different performance characteristics, so you may be able to make your model run faster by selecting the appropriate data structure.

一般に、配列やテーブルを使ってできるあらゆることは、単にリストを使うだけでもできます。しかし、計算速度を優先するよりも配列やテーブルを使いたいと考えることもあるかもしれません。リスト、配列、テーブルの3種類のデータ構造は異なるパフォーマンス特性を持っており、適切なデータ構造を選択することでモデルをより速く動かすことができます。

Arrays are useful when you need a collection of values whose size is fixed. You can quickly access or alter any item in an array if you know its position.

配列は、大きさが固定された数値の集まりが必要なときに有用です。配列の要素の位置がわかっていれば、それに素早くアクセスしたり値を変化させたりすることができます。

Tables are useful when you need to do associate values with other values. For example, you might make a table of words and their definitions. Then you can look up the definition of any word. Here, the words are the "keys". You can easily retrieve the value for any key in the table, but not vice versa.

テーブルはある値と他の値とを関連付けたい場合に有用です。例えば、用語とそれらの定義とのテーブルを作ることができます。そうすればどんな用語の意味も探すことができます。このとき用語は”キー”となります。どんな用語についても値を取り出すことができますが、逆は成り立ちません。

Unlike NetLogo's lists and strings, arrays and tables are "mutable". That means that you can actually modify them directly, rather than constructing an altered copy as with lists. If the array or table is used in more than one place in your code, any changes you make will show up everywhere. It's tricky to write code involving mutable structures and it's easy to make subtle errors or get surprising results, so we suggest sticking with lists and strings unless you're certain you want and need mutability.

NetLogoのリストや文字列とは異なり、配列とテーブルはミュータブルです。すなわちリストのように変更されたコピーを作成するのではなく、直接に修正することができます。もし配列やテーブルがコードの1か所以上の場所で使用されているなら、どのような変更もあらゆる場所で反映されます。ミュータブルな構造を含むコードは扱いにくく、またわかりにくいエラーやびっくりするような結果が生じやすくなります。そのため、ミュータブルであることが確実に必要でない限りは、リストや文字列を使い続けることを推奨します。

Lists and arrays in NetLogo 5.0

NetLogo 5.0におけるリストと配列

In NetLogo 4.1 and earlier, common list operations such as last, lput, item, and replace-item took linear time (proportional to the size of the list). As a result, some users chose to use arrays instead of lists in order to get good performance. But in the current NetLogo, these operations now run in nearly constant time. So arrays are now less often needed.

NetLogo 4.1以前のバージョンでは、last, lput, item, replace-itemなどのリストに関する操作にはリストの大きさに比例する処理時間が必要でした。そのため、パフォーマンス向上のためにリストの代わりに配列を使うユーザーもいました。しかしNetLogoの現在のバージョンでは、これらの処理はほぼ一定の時間で動作します。そのため配列は現在では必要性が低くなっています。

How to use

使用方法

Both extensions come preinstalled.

両方の拡張機能がプリインストールされています。

To use the array extension in your model, add a line to the top of your Code tab:

モデルで配列機能を使うには、コードタブの冒頭に次の行を追加します:

extensions [array]

To use the table extension in your model, add a line to the top of your Code tab:

モデルでテーブル機能を使うには、コードタブの冒頭に次の行を追加します:

extensions [table]

You can use both extensions in the same model if you want, as follows:

両方の拡張機能を使いたいときは次のように記載します:

extensions [array table]

If your model already uses other extensions, then it already has an extensions line in it, so just add array and/or table to the list.

もしモデルで既に他の拡張機能を使っているのであれば、extensions行が既にあるので、その行にarraytableを追記します。

For more information on using NetLogo extensions, see the Extensions Guide.

NetLogo拡張機能についてもっと知りたい場合は拡張機能ガイドをご覧ください。

Limitation on table keys

テーブルキーの制限

Table keys may only be strings, numbers, booleans, or lists. (Lists may be arbitrarily nested lists as long as all the items inside are strings, numbers, or booleans.)

テーブルキーとして用いることができるのは文字列、数値、論理型、リストのみです。(リストは、全ての要素が文字列、数値、論理型である限り、任意のネストとすることができます。)

Array example

配列の例

let a array:from-list n-values 5 [0]
print a
=> {{array: 0 0 0 0 0}}
print array:length a
=> 5
foreach n-values 5 [?] [ array:set a ? ? * ? ]
print a
=> {{array: 0 1 4 9 16}}
print array:item a 0
=> 0
print array:item a 3
=> 9
array:set a 3 50
print a
=> {{array: 0 1 4 50 16}}

Table example

テーブルの例

let dict table:make
table:put dict "turtle" "cute"
table:put dict "bunny" "cutest"
print dict
=> {{table: "turtle" -> "cute", "bunny" -> "cutest" }}
print table:length dict
=> 2
print table:get dict "turtle"
=> "cute"
print table:get dict "leopard"
=> (error)
print table:keys dict
=> ["turtle" "bunny"]

Code Example: Table Example

コード例: Table Example

Array primitives

配列プリミティブ

array:from-list array:item array:set array:length array:to-list

array:from-list

array:from-list list

array:from-list リスト

Reports a new array containing the same items in the same order as the input list.

インプットしたリストと同じ要素を同じ順序で含む新しい配列を返します。

array:item

array:item array index

array:item 配列 インデックス

Reports the item in the given array with the given index (ranging from zero to the length of the array minus one).

インデックス番目の配列の要素を返します(インデックスはゼロから始まり配列の長さマイナス1までの範囲)。

array:set

array:set array index value

array:set 配列 インデックス

Sets the item in the given array with the given index (ranging from zero to the length of the array minus one) to the given value.

指定した配列の指定したインデックス(ゼロから始まり配列の長さマイナス1までの範囲)の要素に指定した値を設定します。

Note that unlike the replace-item primitive for lists, a new array is not created. The given array is actually modified.

リストに対するreplace-itemプリミティブとは異なり、新しい配列は生成されません。指定した配列を実際に変更します。

array:length

array:length array

array:length 配列

Reports the length of the given array, that is, the number of items in the array.

指定した配列の長さ、すなわち配列の要素の数を返します。

array:to-list

array:to-list array

array:to-list 配列

Reports a new list containing the same items in the same order as the given array.

指定した配列と同じ要素を同じ順序で含む新しいリストを返します。

Table Primitives

テーブルプリミティブ

table:clear table:from-list table:get table:has-key? table:keys table:length table:make table:put table:remove table:to-list

table:clear

table:clear table

table:clear テーブル

Removes all key-value pairs from table.

テーブルからすべてのキーと値のペアを取り除きます。

table:from-list

table:from-list list

table:from-list リスト

Reports a new table with the contents of list. list must be a list of two element lists, or pairs. The first element in the pair is the key and the second element is the value.

リストの内容をもとにして新しいテーブルを作成します。リストは2要素のリスト、すなわちペアでなくてはなりません。ペアの1番目の要素がキーとなり、2番目の要素が値となります。

table:get

table:get table key

table:get テーブル キー

Reports the value that key is mapped to in the table. Causes an error if there is no entry for the key.

テーブルにおけるキーの写像先の値を返します。キーに対応する値がないときはエラーとなります。

table:has-key?

table:has-key? table key

table:has-key? テーブル キー

Reports true if key has an entry in table.

テーブルにキーが存在するときはtrueを返します。

table:keys

table:keys table

table:keys テーブル

Reports a list of all the keys in table, in the same order the keys were inserted.

テーブルのすべてのキーのリストを、キーが挿入されたのと同じ順序で返します。

table:length

table:length table

table:length テーブル

Reports the number of entries in table.

テーブルのエントリーの数を返します。

table:make

table:make

table:make

Reports a new, empty table.

新しい空のテーブルを返します。

table:put

table:put table key value

table:put テーブル キー

Maps key to value in table. If an entry already exists in the table for the given key, it is replaced.

テーブルでキーから値への写像を定義します。すでにテーブルにおいてキーからの写像が存在する場合は置き換えます。

table:remove

table:remove table key

table:remove テーブル キー

Removes the mapping in table for key.

テーブルにおいてキーからの写像を削除します。

table:to-list

table:to-list table

table:to-list テーブル

Reports a list with the content of table. The list will be a list of two element lists, or pairs. The first element in the pair is the key and the second element is the value. The keys appear in the same order they were inserted.

テーブルの内容を持つリストを返します。リストは2要素のリスト、すなわちペアとなります。ペアの第1の要素はキーとなり、第2の要素は値となります。キーは挿入されていたのと同じ順序となります。