root/lang/csharp/TypableMap/TypableMap/TypableMap.cs @ 18271

Revision 18271, 4.6 kB (checked in by mayuki, 5 years ago)

cho45さんのTypableMapをC#に移植した。

  • Property svn:keywords set to Id
Line 
1//
2// TypableMap - typo しにくい id を自動生成
3// $Id$
4//
5// Original code by cho45
6// http://subtech.g.hatena.ne.jp/cho45/20080603/1212504034
7//
8// Copyright © 2008 Mayuki Sawatari <mayuki@misuzilla.org>
9// License: MIT License
10//
11using System;
12using System.Collections;
13using System.Collections.Generic;
14using System.Text;
15
16namespace TypableMap
17{
18    public class TypableMap<T> : IDictionary<String, T>
19    {
20        private static readonly String[] Roma;
21       
22        private Dictionary<String, T> _map;
23        private Int32 _size;
24        private Int64 _num;
25       
26        static TypableMap()
27        {
28            List<String> roma = new List<string>();
29            foreach (var c in "a i u e o k g s z t d n h b p m y r w j v l q".Split(' '))
30                foreach (var d in "a i u e o".Split(' '))
31                    roma.Add(String.Concat(c.ToString(), d.ToString()));
32            Roma = roma.ToArray();
33        }
34       
35        /// <summary>
36        /// TypableMap のインスタンスを初期化します。
37        /// </summary>
38        public TypableMap() : this(2)
39        {
40        }
41       
42        /// <summary>
43        /// TypableMap のインスタンスをサイズを指定して初期化します。
44        /// </summary>
45        /// <param name="size">保持するサイズの元となる値。ローマ字の組み合わせ数に指定した値を累乗します。</param>
46        public TypableMap(Int32 size)
47        {
48            _size = 2;
49            _num = 0;
50            _map = new Dictionary<String, T>();
51        }
52       
53        private String Generate(Int64 num)
54        {
55            StringBuilder sb = new StringBuilder();
56            do
57            {
58                Int32 r = (Int32)(num % Roma.Length);
59                num /= Roma.Length;
60                sb.Insert(0, Roma[r]);
61            } while (num > 0);
62
63            return sb.ToString();
64        }
65       
66        public String Add(T value)
67        {
68            String id = Generate(_num);
69            _map[id] = value;
70            _num++;
71            _num %= (Int64)(Math.Pow(Roma.Length, _size));
72            return id;
73        }
74
75        #region IDictionary<string,T> メンバ
76
77        void IDictionary<String, T>.Add(string key, T value)
78        {
79            throw new NotSupportedException();
80        }
81
82        public bool ContainsKey(string key)
83        {
84            return _map.ContainsKey(key);
85        }
86
87        public ICollection<string> Keys
88        {
89            get { return _map.Keys; }
90        }
91
92        public bool Remove(string key)
93        {
94            return _map.Remove(key);
95        }
96
97        public bool TryGetValue(string key, out T value)
98        {
99            return _map.TryGetValue(key, out value);
100        }
101
102        public ICollection<T> Values
103        {
104            get { return _map.Values; }
105        }
106
107        public T this[string key]
108        {
109            get
110            {
111                return _map[key];
112            }
113            set
114            {
115                throw new NotSupportedException();
116            }
117        }
118
119        #endregion
120
121        #region ICollection<KeyValuePair<string,T>> メンバ
122
123        void ICollection<KeyValuePair<string, T>>.Add(KeyValuePair<string, T> item)
124        {
125            throw new NotSupportedException();
126        }
127
128        public void Clear()
129        {
130            _map.Clear();
131            _num = 0;
132        }
133
134        bool ICollection<KeyValuePair<string, T>>.Contains(KeyValuePair<string, T> item)
135        {
136            return ((IDictionary<String, T>) _map).Contains(item);
137        }
138
139        void ICollection<KeyValuePair<string, T>>.CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
140        {
141            ((IDictionary<String, T>)_map).CopyTo(array, arrayIndex);
142        }
143
144        public int Count
145        {
146            get { return _map.Count; }
147        }
148
149        public bool IsReadOnly
150        {
151            get { return false; }
152        }
153
154        bool ICollection<KeyValuePair<string, T>>.Remove(KeyValuePair<string, T> item)
155        {
156            return Remove(item.Key);
157        }
158
159        #endregion
160
161        #region IEnumerable<KeyValuePair<string,T>> メンバ
162
163        public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
164        {
165            return _map.GetEnumerator();
166        }
167
168        #endregion
169
170        #region IEnumerable メンバ
171
172        IEnumerator IEnumerable.GetEnumerator()
173        {
174            return _map.GetEnumerator();
175        }
176
177        #endregion
178    }
179}
Note: See TracBrowser for help on using the browser.