同調実験室 - ARGIUS.net

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

14:配列(3) 連想配列

機能

  1. 連想配列に要素(文字列)を追加
  2. 配列の要素数を表示
  3. 連想配列からキー"bean"にマッピングされた値を取得して表示
  4. 連想配列からキー"egg"にマッピングされた値(要素なし)を取得して表示

マップの実装だが詳細はそれぞれ別(Cは二分木構造)

実行結果

$ ./test
bean = mame
egg = (not found)
$

ソース

C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* 二分木によるマップ実装 */
typedef struct st_map {
  struct st_map * lnode;
  struct st_map * rnode;
  char * key;
  char * value;
} * Map;

/* ノードを追加 */
Map create_node()
{
  Map mp;
  mp = (Map)malloc(sizeof(struct st_map));
  mp->lnode = NULL;
  mp->rnode = NULL;
  mp->key   = NULL;
  mp->value = NULL;
  return mp;
}

/* 指定のノードにセット */
void map_write(Map m, char * key, char * value)
{
  if (m == NULL) {
    m = create_node();
  }
  m->key   = (char *)malloc(strlen(key)+1);
  m->value = (char *)malloc(strlen(value)+1);
  sprintf(m->key,   "%s", key);
  sprintf(m->value, "%s", value);
  return;
}

/* マッピング */
void map_put(Map m, char * key, char * value)
{
  if (m->key == NULL) {
    map_write(m, key, value);
    return;
  }
  
  int cmp = strcmp(m->key, key);
  if (cmp == 0) {
    /* 登録済みは上書き */
    map_write(m, key, value);
  } else if (cmp < 0) {
    if (m->lnode == NULL) {
      m->lnode = create_node();
    }
    map_put(m->lnode, key, value);
  } else if (cmp > 0) {
    if (m->rnode == NULL) {
      m->rnode = create_node();
    }
    map_put(m->rnode, key, value);
  }
  
}

/* マッピングされた値を取得 */
char * map_get(Map m, char * key)
{
  Map mp = m;
  int cmp;
  
  for (;;) {
    
    if (mp == NULL || mp->key == NULL) break;
    
    cmp = strcmp(mp->key, key);
    
    if (cmp < 0) {
      mp = mp->lnode;
    } else if (cmp > 0) {
      mp = mp->rnode;
    } else {
      return mp->value;
    }
    
  }
  
  return "(not found)";
}

/* 実行 */
int main()
{
  Map  dict;
  char key[32];
  
  dict = create_node();
  
  map_put(dict, "apple", "ringo");
  map_put(dict, "bean",  "mame");
  map_put(dict, "candy", "ame");
  map_put(dict, "drink", "nomimono");
  
  sprintf(key, "bean");
  printf("%s = %s\n", key, map_get(dict, key));

  sprintf(key, "egg");
  printf("%s = %s\n", key, map_get(dict, key));
  
  return 0;
}

C++

#include <iostream>
#include <string>
#include <map>

using namespace std; // 読みやすさのため

string map_get(map<string, string> & m, string key)
{
  string s = m[key];
  if (s.length() == 0) {
    return string("(not found)");
  }
  return s;
}

int main()
{
  map<string, string> dict;
  string key;
  
  dict["apple"]  = "ringo";
  dict["bean"]   = "mame";
  dict["candy"]  = "ame";
  dict["drink"]  = "nomimono";
  
  key = "bean";
  cout << key << " = " << map_get(dict, key) << endl;
  
  key = "egg";
  cout << key << " = " << map_get(dict, key) << endl;
  
  return 0;
}

Java

import java.util.*;

// Stringだけのマップ
class StringMap {
    Map map;
    StringMap() {
        map = new HashMap();
    }
    void put(String key, String value) {
        map.put(key, value);
    }
    String get(String key) {
        String s = (String)map.get(key);
        if (s == null) {
            return "(not found)";
        }
        return s;
    }
}

class Main {
    
    public static void main(String[] args) {
        
        StringMap dict = new StringMap();
        String key = "";
        
        dict.put("apple", "ringo");
        dict.put("bean",  "mame");
        dict.put("candy", "ame");
        dict.put("drink", "nomimono");
        
        key = "bean";
        System.out.println(key + " = " + dict.get(key)); 
        
        key = "egg";
        System.out.println(key + " = " + dict.get(key)); 
        
    }
    
}

Perl

# 冗長になるのでクラスにしない
sub map_get {
    my $map = shift;
    my $key = shift;
    my $value = $map->{$key};
    if ($value eq "") {
        return "(not found)";
    }
    return $value;
}

my %dict = ();
my $key  = "";

$dict{'apple'} = "ringo";
$dict{'bean'}  = "mame";
$dict{'candy'} = "ame";
$dict{'drink'} = "nomimono";

$key = "bean";
printf"%s = %s\n", $key, map_get(\%dict, $key);

$key = "egg";
printf"%s = %s\n", $key, map_get(\%dict, $key);

Ruby

# 定義追加
class Hash
  def get(key)
    if key?(key)
      return fetch(key)
    else
      return "(not found)"
    end
  end
end

dict = Hash.new

dict["apple"] = "ringo"
dict["bean"]  = "mame"
dict["candy"] = "ame"
dict["drink"] = "nomimono"

key = "bean"
puts key + " = " + dict.get(key)

key = "egg"
puts key + " = " + dict.get(key)

最終更新日 : 2004.08.23

TOP PAGEINDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18