﻿<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=Dev%3ASource%2FData_Structures%2FGHash_Tutorial</id>
	<title>Dev:Source/Data Structures/GHash Tutorial - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=Dev%3ASource%2FData_Structures%2FGHash_Tutorial"/>
	<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/GHash_Tutorial&amp;action=history"/>
	<updated>2026-06-02T04:04:11Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/GHash_Tutorial&amp;diff=43148&amp;oldid=prev</id>
		<title>Yamyam: 1版 をインポートしました</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/GHash_Tutorial&amp;diff=43148&amp;oldid=prev"/>
		<updated>2018-06-28T17:45:48Z</updated>

		<summary type="html">&lt;p&gt;1版 をインポートしました&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;2018年6月28日 (木) 17:45時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ja&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(相違点なし)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Yamyam</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/GHash_Tutorial&amp;diff=43147&amp;oldid=prev</id>
		<title>wiki&gt;Jby601: /* What is a GHash? */ minor grammatical edit</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=Dev:Source/Data_Structures/GHash_Tutorial&amp;diff=43147&amp;oldid=prev"/>
		<updated>2012-01-15T14:36:00Z</updated>

		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;What is a GHash?: &lt;/span&gt; minor grammatical edit&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= What is a GHash?=&lt;br /&gt;
&lt;br /&gt;
There are times that you need to make an easily searchable list, where the size is unknown, and it needs to be quickly accessed (no linear searching) One great way to get this job done is with a hash table. Setting up your own hash, when you haven't got a clue can be a daunting task. But why reinvent the wheel! There is already a generic hash data structure built into the Blender codebase! ZR wrote ghash which lets you set up and use a hash with very little code. This can give you several options...&lt;br /&gt;
*A Keyed Hash Table&lt;br /&gt;
*An Item Set&lt;br /&gt;
&lt;br /&gt;
==Hash Table==&lt;br /&gt;
&lt;br /&gt;
A hash table is basically these key and value pairs that have been stored in such a way that passing the key to the hash will return you the value. ghash uses pointers as the key and value. Since it is set up with void pointers, they can point to just about anything. One application would be the case that you had a data structure that held temporary meta data about an EditEdge. For each edit edge, you could use the EdgeEdge* as your key and then dynamically allocate some memory to your meta data struct and then assign that as the value. Then whenever you needed the metadata for that edge, you pass the key to the hash and it returns the pointer to your struct.&lt;br /&gt;
&lt;br /&gt;
==An Item Set==&lt;br /&gt;
&lt;br /&gt;
Suppose you just want to make a dynamic list of items. Just feed the keys to the hash with NULL as the value. Then you can check later if the key is in the hash or set up an iterator to step through the keys of your hash.&lt;br /&gt;
&lt;br /&gt;
= Using GHash=&lt;br /&gt;
&lt;br /&gt;
== The Basic GHash==&lt;br /&gt;
&lt;br /&gt;
We get there from this .h file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 #include &amp;quot;BLI_ghash.h&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we need to declare our ghash: Then we need to create our new data structure. We pass this init function pointers to the hash and compare functions&lt;br /&gt;
that this hash will be using. In this case a pointer hash. The function arguments determine how key values are compared or hashed, and can be used to create more specialized hash tables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
GHash *gh;&lt;br /&gt;
&lt;br /&gt;
gh = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When it comes time to add an entry into our hash we call BLI_ghash_insert and pass it our hash, the key and value.&lt;br /&gt;
Where key and value are both pointers.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghash_insert(gh, key, value);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we can pass the key to BLI_ghash_lookup and retrieve that value back&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 value = BLI_ghash_lookup(gh, key);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One way to create a dynamic set of objects is to enter keys into the hash with NULL as the value. Then, you can later&lt;br /&gt;
use BLI_ghash_haskey to see if that item is in the hash. It returns 1 for yes and 0 for no.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghash_haskey(gh,key)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Of course we can check the size with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghash_size(gh)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When we are done, be sure to clean up after yourself with this little beauty!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghash_free(gh, NULL, NULL);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using An Iterator==&lt;br /&gt;
&lt;br /&gt;
After we create our hash, we need to create an iterator if we want to&lt;br /&gt;
step though it... Note that it is not safe to insert elements or free&lt;br /&gt;
the hash while we are itereating through it.&lt;br /&gt;
&lt;br /&gt;
We will need to declare our iterator&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 GHashIterator* ghi;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then later we initialize it with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 ghi = BLI_ghashIterator_new(gh);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The iterator starts on the first key and we step through the keys using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghashIterator_step(ghi);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On a given key we can get the key or value using these, they return the key or value pointers&lt;br /&gt;
if the iterator has completly gone through the hash, you will get NULL from either.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
BLI_ghashIterator_getKey(ghi);&lt;br /&gt;
&lt;br /&gt;
BLI_ghashIterator_getValue(ghi);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also test the doneness of the iterator with this. Returning 1 if done 0 if not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghashIterator_isDone(ghi);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and eventaully free it with this&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
 BLI_ghashIterator_free(ghi);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Here is some sample code to print the items in a hash:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void print_hash(GHash *gh) {&lt;br /&gt;
    GHashIterator *ghi = BLI_ghashIterator_new(gh);&lt;br /&gt;
    for (; !BLI_ghashIterator_isDone(ghi); BLI_ghashIterator_step(ghi)) {&lt;br /&gt;
        printf(&amp;quot;Key(%p) : Value(%p)\n&amp;quot;, BLI_ghashIterator_getKey(ghi),&lt;br /&gt;
                                        BLI_ghashIterator_getValue(ghi));&lt;br /&gt;
    }&lt;br /&gt;
    BLI_ghashIterator_free(ghi);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Script]]&lt;/div&gt;</summary>
		<author><name>wiki&gt;Jby601</name></author>
		
	</entry>
</feed>