• R/O
  • HTTP
  • SSH
  • HTTPS

CHNOSProject: Commit

CHNOSProjectのすべてのプロジェクトのソースのルート


Commit MetaInfo

Revisiónf1b1537c613260de4c7e0bb7f11bac11ca96a759 (tree)
Tiempo2013-07-03 03:07:59
Autorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

chnlib:CHNLIB_UUID(UUIDv4), メルセンヌ・ツイスターを利用した乱数生成器CHNLIB_RandomGeneratorMTを追加。

Cambiar Resumen

Diferencia incremental

--- a/CHNOSProject/chn/chn.xcodeproj/project.pbxproj
+++ b/CHNOSProject/chn/chn.xcodeproj/project.pbxproj
@@ -7,6 +7,8 @@
77 objects = {
88
99 /* Begin PBXBuildFile section */
10+ 174CE470177FEF23004FE674 /* chnlib05.c in Sources */ = {isa = PBXBuildFile; fileRef = 174CE46F177FEF23004FE674 /* chnlib05.c */; };
11+ 174CE472177FFE16004FE674 /* chnlib06.c in Sources */ = {isa = PBXBuildFile; fileRef = 174CE471177FFE16004FE674 /* chnlib06.c */; };
1012 17D0F40116DA313700C62B79 /* chnlib.c in Sources */ = {isa = PBXBuildFile; fileRef = 17D0F3FA16DA313700C62B79 /* chnlib.c */; };
1113 17D0F40216DA313700C62B79 /* chnlib.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D0F3FB16DA313700C62B79 /* chnlib.h */; settings = {ATTRIBUTES = (Public, ); }; };
1214 17D0F40316DA313700C62B79 /* chnlib00.c in Sources */ = {isa = PBXBuildFile; fileRef = 17D0F3FC16DA313700C62B79 /* chnlib00.c */; };
@@ -17,6 +19,8 @@
1719 /* End PBXBuildFile section */
1820
1921 /* Begin PBXFileReference section */
22+ 174CE46F177FEF23004FE674 /* chnlib05.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib05.c; sourceTree = "<group>"; };
23+ 174CE471177FFE16004FE674 /* chnlib06.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib06.c; sourceTree = "<group>"; };
2024 17D0F3ED16DA305000C62B79 /* libchn.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libchn.a; sourceTree = BUILT_PRODUCTS_DIR; };
2125 17D0F3FA16DA313700C62B79 /* chnlib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib.c; sourceTree = "<group>"; };
2226 17D0F3FB16DA313700C62B79 /* chnlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 2; path = chnlib.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
@@ -48,6 +52,8 @@
4852 17D0F3FE16DA313700C62B79 /* chnlib02.c */,
4953 17D0F3FF16DA313700C62B79 /* chnlib03.c */,
5054 17D0F40016DA313700C62B79 /* chnlib04.c */,
55+ 174CE46F177FEF23004FE674 /* chnlib05.c */,
56+ 174CE471177FFE16004FE674 /* chnlib06.c */,
5157 17D0F3EE16DA305000C62B79 /* Products */,
5258 );
5359 sourceTree = "<group>";
@@ -128,6 +134,8 @@
128134 17D0F40516DA313700C62B79 /* chnlib02.c in Sources */,
129135 17D0F40616DA313700C62B79 /* chnlib03.c in Sources */,
130136 17D0F40716DA313700C62B79 /* chnlib04.c in Sources */,
137+ 174CE470177FEF23004FE674 /* chnlib05.c in Sources */,
138+ 174CE472177FFE16004FE674 /* chnlib06.c in Sources */,
131139 );
132140 runOnlyForDeploymentPostprocessing = 0;
133141 };
--- a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -7,7 +7,7 @@
77 <key>chn.xcscheme</key>
88 <dict>
99 <key>orderHint</key>
10- <integer>2</integer>
10+ <integer>3</integer>
1111 </dict>
1212 </dict>
1313 <key>SuppressBuildableAutocreation</key>
--- a/CHNOSProject/chn/chnlib.h
+++ b/CHNOSProject/chn/chnlib.h
@@ -49,6 +49,15 @@ struct CHNLIB_STRUCTURE_HEADER {
4949 uint (*getHash)(const void *structure);
5050 };
5151
52+//@chnlib00.c
53+typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray;
54+//@chnlib01.c
55+typedef struct CHNLIB_STRING CHNLIB_String;
56+//@chnlib05.c
57+typedef struct CHNLIB_RANDOM_GENERATOR_MT CHNLIB_RandomGeneratorMT;
58+//@chnlib06.c
59+typedef struct CHNLIB_UniversallyUniqueIDentifier CHNLIB_UUID;
60+
5261 //
5362 //Define library values
5463 //
@@ -58,9 +67,11 @@ struct CHNLIB_STRUCTURE_HEADER {
5867 #define False 0
5968 #define True 1
6069
61-#define CHNLIB_STRUCT_ID_Null 0
62-#define CHNLIB_STRUCT_ID_UIPArray 1
63-#define CHNLIB_STRUCT_ID_String 2
70+#define CHNLIB_STRUCT_ID_Null 0
71+#define CHNLIB_STRUCT_ID_UIPArray 1
72+#define CHNLIB_STRUCT_ID_String 2
73+#define CHNLIB_STRUCT_ID_RandomGeneratorMT 3
74+#define CHNLIB_STRUCT_ID_UUID 4
6475
6576 #define CHNLIB_MAX_STRING_LENGTH 4096
6677
@@ -86,8 +97,6 @@ void CHNLIB_Retain(void **structure);
8697 void CHNLIB_Release(void **structure);
8798 void *CHNLIB_AutoRelease(void *structure);
8899 //@chnlib00.c
89-typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray;
90-struct CHNLIB_UIP_ARRAY;
91100 //----配列の初期化
92101 CHNLIB_UIPArray *CHNLIB_UIPArray_Initialize(void);
93102 //----データの追加
@@ -117,8 +126,6 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source);
117126 CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB_UIPArray *array);
118127
119128 //@chnlib01.c
120-typedef struct CHNLIB_STRING CHNLIB_String;
121-struct CHNLIB_STRING;
122129 //----文字列の初期化
123130 CHNLIB_String *CHNLIB_String_Initialize(const char str[]);
124131 CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...);
@@ -159,12 +166,13 @@ int CHNLIB_String_Search_UIPArrayStringLocation(const CHNLIB_String *s, int s_st
159166 int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, const CHNLIB_UIPArray *list, const CHNLIB_String *s);
160167 int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const CHNLIB_String *s);
161168 CHNLIB_String *CHNLIB_ReadLine(FILE *fp);
169+CHNLIB_UUID *CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(CHNLIB_RandomGeneratorMT *mt);
162170
163171 //@chnlib03.c
164172 void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[]);
165173 const char *CHNLIB_Environment_GetFilenameFromPath(const char path[]);
166174
167-//@chnlib05.c
175+//@chnlib04.c
168176 int CHNLIB_UTF8_GetCharacterType(char c);
169177 int CHNLIB_UTF8_GetStringLengthByCharacter(const char s[]);
170178 uint CHNLIB_UTF8_GetNextUnicodeOfCharacter(const char s[], const char **next);
@@ -173,4 +181,19 @@ int CHNLIB_UTF8_CompareString_LeftHand(const char s[], const char search[]);
173181 int CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(const char s[], int start, int end);
174182 uint CHNLIB_UTF8_GetCountOfContain(const char s[], const char search[]);
175183
184+//@chnlib05.c
185+CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Initialise(void);
186+void CHNLIB_RandomGeneratorMT_Free(CHNLIB_RandomGeneratorMT *env);
187+void CHNLIB_RandomGeneratorMT_SetSeed(CHNLIB_RandomGeneratorMT *env, unsigned long s);
188+void CHNLIB_RandomGeneratorMT_SetSeedFromArray(CHNLIB_RandomGeneratorMT *env, unsigned long init_key[], int key_length);
189+uint CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(CHNLIB_RandomGeneratorMT *env);
190+
191+//@chnlib06.c
192+CHNLIB_UUID *CHNLIB_UUID_Initialise(void);
193+void CHNLIB_UUID_Free(CHNLIB_UUID *uuid);
194+void CHNLIB_UUID_Print(const CHNLIB_UUID *uuid);
195+int CHNLIB_UUID_IsEqualToUUID(const CHNLIB_UUID *uuid1, const CHNLIB_UUID *uuid2);
196+void CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(CHNLIB_UUID *uuid, uint random1, uint random2, uint random3, uint random4);
197+void CHNLIB_UUID_SetNullUUID(CHNLIB_UUID *uuid);
198+CHNLIB_UUID *CHNLIB_UUID_Copy(const CHNLIB_UUID *uuidBase);
176199 #endif
--- a/CHNOSProject/chn/chnlib02.c
+++ b/CHNOSProject/chn/chnlib02.c
@@ -139,4 +139,19 @@ CHNLIB_String *CHNLIB_ReadLine(FILE *fp)
139139 return CHNLIB_String_Initialize(s);
140140 }
141141
142+CHNLIB_UUID *CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(CHNLIB_RandomGeneratorMT *mt)
143+{
144+ //mtが有効でない場合、NullUUIDを返す。
145+ CHNLIB_UUID *uuid;
146+
147+ uuid = CHNLIB_UUID_Initialise();
148+
149+ if(CHNLIB_StructureHeader_GetTypeID(mt) != CHNLIB_STRUCT_ID_RandomGeneratorMT){
150+ return uuid;
151+ }
152+
153+ CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(uuid, CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt));
154+
155+ return uuid;
156+}
142157
--- /dev/null
+++ b/CHNOSProject/chn/chnlib05.c
@@ -0,0 +1,289 @@
1+//
2+// chnlib05.c
3+// AI003
4+//
5+// Created by 西田 耀 on 13/06/30.
6+// Copyright (c) 2013年 Hikaru Nishida. All rights reserved.
7+// A C-program for MT19937, modified by hikarupsp for CHNLIB.
8+
9+// MT19937を利用した乱数生成器
10+
11+/*
12+ A C-program for MT19937, with initialization improved 2002/1/26.
13+ Coded by Takuji Nishimura and Makoto Matsumoto.
14+
15+ Before using, initialize the state by using init_genrand(seed)
16+ or init_by_array(init_key, key_length).
17+
18+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
19+ All rights reserved.
20+
21+ Redistribution and use in source and binary forms, with or without
22+ modification, are permitted provided that the following conditions
23+ are met:
24+
25+ 1. Redistributions of source code must retain the above copyright
26+ notice, this list of conditions and the following disclaimer.
27+
28+ 2. Redistributions in binary form must reproduce the above copyright
29+ notice, this list of conditions and the following disclaimer in the
30+ documentation and/or other materials provided with the distribution.
31+
32+ 3. The names of its contributors may not be used to endorse or promote
33+ products derived from this software without specific prior written
34+ permission.
35+
36+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
40+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47+
48+
49+ Any feedback is very welcome.
50+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
51+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
52+*/
53+/*
54+ Modified by hikarupsp on 2013/06/30 for CHNLIB.
55+*/
56+
57+//
58+//Include headers
59+//
60+#include <stdio.h>
61+#include "chnlib.h"
62+
63+//
64+//Define values
65+//
66+/* Period parameters */
67+#define N 624
68+#define M 397
69+#define MATRIX_A 0x9908b0dfUL /* constant vector a */
70+#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
71+#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
72+
73+//
74+//Declare internal functions
75+//
76+CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Internal_Allocate(void);
77+void CHNLIB_RandomGeneratorMT_Internal_Destruct(void **structure);
78+uint CHNLIB_RandomGeneratorMT_Internal_GetHash(const void *structure);
79+
80+//
81+//Define types
82+//
83+struct CHNLIB_RANDOM_GENERATOR_MT {
84+ //MT19937を利用した乱数生成器
85+ CHNLIB_StructureHeader header;
86+ unsigned long mt[N]; /* the array for the state vector */
87+ int mti; /* mti==N+1 means mt[N] is not initialized */
88+};
89+
90+//
91+//Functions
92+//
93+
94+CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Initialise(void)
95+{
96+ CHNLIB_RandomGeneratorMT *env;
97+
98+ env = CHNLIB_RandomGeneratorMT_Internal_Allocate();
99+ env->header.destructor = &CHNLIB_RandomGeneratorMT_Internal_Destruct;
100+ env->header.getHash = &CHNLIB_RandomGeneratorMT_Internal_GetHash;
101+ env->mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
102+ return env;
103+}
104+
105+void CHNLIB_RandomGeneratorMT_Free(CHNLIB_RandomGeneratorMT *env)
106+{
107+ //envを解放する。
108+ if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){
109+ return;
110+ }
111+
112+ env->header.typeid = CHNLIB_STRUCT_ID_Null;
113+ env->header.signature = 0;
114+ CHNLIB_System_FreeMemory(env, CHNLIB_DEBUG_ARGUMENTS);
115+
116+ return;
117+}
118+
119+
120+/* initializes mt[N] with a seed */
121+void CHNLIB_RandomGeneratorMT_SetSeed(CHNLIB_RandomGeneratorMT *env, unsigned long s)
122+{
123+ if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){
124+ return;
125+ }
126+
127+ env->mt[0]= s & 0xffffffffUL;
128+ for (env->mti=1; env->mti<N; env->mti++) {
129+ env->mt[env->mti] =
130+ (1812433253UL * (env->mt[env->mti-1] ^ (env->mt[env->mti-1] >> 30)) + env->mti);
131+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
132+ /* In the previous versions, MSBs of the seed affect */
133+ /* only MSBs of the array mt[]. */
134+ /* 2002/01/09 modified by Makoto Matsumoto */
135+ env->mt[env->mti] &= 0xffffffffUL;
136+ /* for >32 bit machines */
137+ }
138+}
139+
140+/* initialize by an array with array-length */
141+/* init_key is the array for initializing keys */
142+/* key_length is its length */
143+/* slight change for C++, 2004/2/26 */
144+void CHNLIB_RandomGeneratorMT_SetSeedFromArray(CHNLIB_RandomGeneratorMT *env, unsigned long init_key[], int key_length)
145+{
146+ int i, j, k;
147+
148+ if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){
149+ return;
150+ }
151+
152+ CHNLIB_RandomGeneratorMT_SetSeed(env, 19650218UL);
153+ i=1; j=0;
154+ k = (N>key_length ? N : key_length);
155+ for (; k; k--) {
156+ env->mt[i] = (env->mt[i] ^ ((env->mt[i-1] ^ (env->mt[i-1] >> 30)) * 1664525UL))
157+ + init_key[j] + j; /* non linear */
158+ env->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
159+ i++; j++;
160+ if (i>=N) { env->mt[0] = env->mt[N-1]; i=1; }
161+ if (j>=key_length) j=0;
162+ }
163+ for (k=N-1; k; k--) {
164+ env->mt[i] = (env->mt[i] ^ ((env->mt[i-1] ^ (env->mt[i-1] >> 30)) * 1566083941UL))
165+ - i; /* non linear */
166+ env->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
167+ i++;
168+ if (i>=N) { env->mt[0] = env->mt[N-1]; i=1; }
169+ }
170+
171+ env->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
172+}
173+
174+/* generates a random number on [0,0xffffffff]-interval */
175+uint CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(CHNLIB_RandomGeneratorMT *env)
176+{
177+ unsigned long y;
178+ static unsigned long mag01[2]={0x0UL, MATRIX_A};
179+ /* mag01[x] = x * MATRIX_A for x=0,1 */
180+
181+ if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){
182+ return 0;
183+ }
184+
185+ if (env->mti >= N) { /* generate N words at one time */
186+ int kk;
187+
188+ if (env->mti == N+1) /* if init_genrand() has not been called, */
189+ CHNLIB_RandomGeneratorMT_SetSeed(env, 5489UL); /* a default initial seed is used */
190+
191+ for (kk=0;kk<N-M;kk++) {
192+ y = (env->mt[kk]&UPPER_MASK)|(env->mt[kk+1]&LOWER_MASK);
193+ env->mt[kk] = env->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
194+ }
195+ for (;kk<N-1;kk++) {
196+ y = (env->mt[kk]&UPPER_MASK)|(env->mt[kk+1]&LOWER_MASK);
197+ env->mt[kk] = env->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
198+ }
199+ y = (env->mt[N-1]&UPPER_MASK)|(env->mt[0]&LOWER_MASK);
200+ env->mt[N-1] = env->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
201+
202+ env->mti = 0;
203+ }
204+
205+ y = env->mt[env->mti++];
206+
207+ /* Tempering */
208+ y ^= (y >> 11);
209+ y ^= (y << 7) & 0x9d2c5680UL;
210+ y ^= (y << 15) & 0xefc60000UL;
211+ y ^= (y >> 18);
212+
213+ return (uint)y;
214+}
215+
216+/* generates a random number on [0,1]-real-interval */
217+//double genrand_real1(void)
218+//{
219+// return genrand_int32()*(1.0/4294967295.0);
220+// /* divided by 2^32-1 */
221+//}
222+
223+/* generates a random number on [0,1)-real-interval */
224+//double genrand_real2(void)
225+//{
226+// return genrand_int32()*(1.0/4294967296.0);
227+// /* divided by 2^32 */
228+//}
229+
230+/* generates a random number on (0,1)-real-interval */
231+//double genrand_real3(void)
232+//{
233+// return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
234+// /* divided by 2^32 */
235+//}
236+
237+/* generates a random number on [0,1) with 53-bit resolution*/
238+//double genrand_res53(void)
239+//{
240+// unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
241+// return(a*67108864.0+b)*(1.0/9007199254740992.0);
242+//}
243+
244+//
245+//Internal functions
246+//
247+
248+CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Internal_Allocate(void)
249+{
250+ CHNLIB_RandomGeneratorMT *tag;
251+
252+ tag = (CHNLIB_RandomGeneratorMT *)CHNLIB_System_AllocateMemory_Strict(sizeof(CHNLIB_RandomGeneratorMT), CHNLIB_DEBUG_ARGUMENTS);
253+
254+ CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_RandomGeneratorMT);
255+
256+ return tag;
257+}
258+
259+void CHNLIB_RandomGeneratorMT_Internal_Destruct(void **structure)
260+{
261+ //デストラクタ(実際にRelease->freeされる時に呼ばれる)
262+ if(structure == NULL){
263+ return;
264+ }
265+
266+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
267+ CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
268+#endif
269+
270+ CHNLIB_RandomGeneratorMT_Free(*structure);
271+
272+ *structure = NULL;
273+
274+ return;
275+}
276+
277+uint CHNLIB_RandomGeneratorMT_Internal_GetHash(const void *structure)
278+{
279+ //構造体の値が静的な意味を持たない場合は、構造体のアドレスの下位32bit分を返し、Hashの代わりとする。
280+ uint hash;
281+
282+ hash = 0;
283+
284+ if(CHNLIB_StructureHeader_GetTypeID(structure) == CHNLIB_STRUCT_ID_RandomGeneratorMT){
285+ hash = (uint)&structure;
286+ }
287+
288+ return hash;
289+}
--- /dev/null
+++ b/CHNOSProject/chn/chnlib06.c
@@ -0,0 +1,266 @@
1+//
2+// chnlib06.c
3+// chn
4+//
5+// Created by 西田 耀 on 13/06/30.
6+// Copyright (c) 2013年 Hikaru Nishida. All rights reserved.
7+//
8+
9+// UUIDを格納する構造体
10+
11+//
12+//Include headers
13+//
14+#include <stdio.h>
15+#include "chnlib.h"
16+
17+//
18+//Define values
19+//
20+
21+//
22+//Declare internal functions
23+//
24+CHNLIB_UUID *CHNLIB_UUID_Internal_Allocate(void);
25+void CHNLIB_UUID_Internal_Destruct(void **structure);
26+uint CHNLIB_UUID_Internal_GetHash(const void *structure);
27+
28+//
29+//Define types
30+//
31+struct CHNLIB_UniversallyUniqueIDentifier {
32+ //UUID based on RFC4122
33+ //各フィールドはビッグエンディアンとして扱われるべき。
34+ CHNLIB_StructureHeader header;
35+ uchar time_low[4];
36+ uchar time_mid[2];
37+ uchar time_hi_and_version[2];
38+ uchar clock_seq_hi_and_reserved[1];
39+ uchar clock_seq_low[1];
40+ uchar node[6];
41+};
42+
43+//
44+//Functions
45+//
46+
47+CHNLIB_UUID *CHNLIB_UUID_Initialise(void)
48+{
49+ CHNLIB_UUID *uuid;
50+
51+ uuid = CHNLIB_UUID_Internal_Allocate();
52+ uuid->header.destructor = &CHNLIB_UUID_Internal_Destruct;
53+ uuid->header.getHash = &CHNLIB_UUID_Internal_GetHash;
54+
55+ CHNLIB_UUID_SetNullUUID(uuid);
56+
57+ return uuid;
58+}
59+
60+void CHNLIB_UUID_Free(CHNLIB_UUID *uuid)
61+{
62+ //uuidを解放する。
63+ if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){
64+ return;
65+ }
66+
67+ CHNLIB_UUID_SetNullUUID(uuid);
68+
69+ uuid->header.typeid = CHNLIB_STRUCT_ID_Null;
70+ uuid->header.signature = 0;
71+ CHNLIB_System_FreeMemory(uuid, CHNLIB_DEBUG_ARGUMENTS);
72+
73+ return;
74+}
75+
76+void CHNLIB_UUID_Print(const CHNLIB_UUID *uuid)
77+{
78+ if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){
79+ return;
80+ }
81+
82+ printf("%02x%02x%02x%02x-", uuid->time_low[0], uuid->time_low[1], uuid->time_low[2], uuid->time_low[3]);
83+ printf("%02x%02x-", uuid->time_mid[0], uuid->time_mid[1]);
84+ printf("%02x%02x-", uuid->time_hi_and_version[0], uuid->time_hi_and_version[1]);
85+ printf("%02x%02x-", uuid->clock_seq_hi_and_reserved[0], uuid->clock_seq_low[0]);
86+ printf("%02x%02x%02x%02x%02x%02x", uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3], uuid->node[4], uuid->node[5]);
87+}
88+
89+int CHNLIB_UUID_IsEqualToUUID(const CHNLIB_UUID *uuid1, const CHNLIB_UUID *uuid2)
90+{
91+ int i;
92+
93+ if(CHNLIB_StructureHeader_GetTypeID(uuid1) != CHNLIB_STRUCT_ID_UUID){
94+ return False;
95+ }
96+ if(CHNLIB_StructureHeader_GetTypeID(uuid2) != CHNLIB_STRUCT_ID_UUID){
97+ return False;
98+ }
99+
100+ for(i = 0; i < 4; i++){
101+ if(uuid1->time_low[i] != uuid2->time_low[i]){
102+ return False;
103+ }
104+ }
105+ for(i = 0; i < 2; i++){
106+ if(uuid1->time_mid[i] != uuid2->time_mid[i]){
107+ return False;
108+ }
109+ }
110+ for(i = 0; i < 2; i++){
111+ if(uuid1->time_hi_and_version[i] != uuid2->time_hi_and_version[i]){
112+ return False;
113+ }
114+ }
115+ if(uuid1->clock_seq_hi_and_reserved[i] != uuid2->clock_seq_hi_and_reserved[i]){
116+ return False;
117+ }
118+ if(uuid1->clock_seq_low[i] != uuid2->clock_seq_low[i]){
119+ return False;
120+ }
121+ for(i = 0; i < 6; i++){
122+ if(uuid1->node[i] != uuid2->node[i]){
123+ return False;
124+ }
125+ }
126+ return True;
127+}
128+
129+void CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(CHNLIB_UUID *uuid, uint random1, uint random2, uint random3, uint random4)
130+{
131+ int i;
132+
133+ if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){
134+ return;
135+ }
136+
137+ //random1
138+ for(i = 0; i < 4; i++){
139+ uuid->time_low[i] = ((random1 >> (i * 8)) & 0xff);
140+ }
141+ //random2
142+ for(i = 0; i < 2; i++){
143+ uuid->time_mid[i] = ((random2 >> (i * 8)) & 0xff);
144+ }
145+ for(i = 0; i < 2; i++){
146+ uuid->time_hi_and_version[i] = ((random2 >> ((i + 2) * 8)) & 0xff);
147+ }
148+ //random3
149+ uuid->clock_seq_hi_and_reserved[0] = ((random3 >> (1 * 8)) & 0xff);
150+ uuid->clock_seq_low[0] = ((random3 >> (2 * 8)) & 0xff);
151+ for(i = 0; i < 2; i++){
152+ uuid->node[i] = ((random3 >> ((i + 2) * 8)) & 0xff);
153+ }
154+ //random4
155+ for(i = 2; i < 6; i++){
156+ uuid->node[i] = ((random4 >> ((i - 2) * 8)) & 0xff);
157+ }
158+ //Fixed field
159+ uuid->clock_seq_hi_and_reserved[0] &= 0x3f;
160+ uuid->clock_seq_hi_and_reserved[0] |= 0x80;
161+
162+ uuid->time_hi_and_version[0] &= 0x0f;
163+ uuid->time_hi_and_version[0] |= 0x40;
164+
165+ return;
166+}
167+
168+void CHNLIB_UUID_SetNullUUID(CHNLIB_UUID *uuid)
169+{
170+ int i;
171+
172+ if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){
173+ return;
174+ }
175+
176+ for(i = 0; i < 4; i++){
177+ uuid->time_low[i] = 0x00;
178+ }
179+ for(i = 0; i < 2; i++){
180+ uuid->time_mid[i] = 0x00;
181+ }
182+ for(i = 0; i < 2; i++){
183+ uuid->time_hi_and_version[i] = 0x00;
184+ }
185+ uuid->clock_seq_hi_and_reserved[0] = 0x00;
186+ uuid->clock_seq_low[0] = 0x00;
187+ for(i = 0; i < 6; i++){
188+ uuid->node[i] = 0x00;
189+ }
190+ return;
191+}
192+
193+CHNLIB_UUID *CHNLIB_UUID_Copy(const CHNLIB_UUID *uuidBase)
194+{
195+ CHNLIB_UUID *uuidCopy;
196+
197+ uuidCopy = CHNLIB_UUID_Initialise();
198+
199+ int i;
200+
201+ if(CHNLIB_StructureHeader_GetTypeID(uuidBase) == CHNLIB_STRUCT_ID_UUID){
202+ for(i = 0; i < 4; i++){
203+ uuidCopy->time_low[i] = uuidBase->time_low[i];
204+ }
205+ for(i = 0; i < 2; i++){
206+ uuidCopy->time_mid[i] = uuidBase->time_mid[i];
207+ }
208+ for(i = 0; i < 2; i++){
209+ uuidCopy->time_hi_and_version[i] = uuidBase->time_hi_and_version[i];
210+ }
211+ uuidCopy->clock_seq_hi_and_reserved[0] = uuidBase->clock_seq_hi_and_reserved[0];
212+ uuidCopy->clock_seq_low[0] = uuidBase->clock_seq_low[0];
213+ for(i = 0; i < 6; i++){
214+ uuidCopy->node[i] = uuidBase->node[i];
215+ }
216+ }
217+ return uuidCopy;
218+
219+}
220+
221+//
222+//Internal functions
223+//
224+CHNLIB_UUID *CHNLIB_UUID_Internal_Allocate(void)
225+{
226+ CHNLIB_UUID *tag;
227+
228+ tag = (CHNLIB_UUID *)CHNLIB_System_AllocateMemory_Strict(sizeof(CHNLIB_UUID), CHNLIB_DEBUG_ARGUMENTS);
229+
230+ CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_UUID);
231+
232+ return tag;
233+}
234+
235+void CHNLIB_UUID_Internal_Destruct(void **structure)
236+{
237+ //デストラクタ(実際にRelease->freeされる時に呼ばれる)
238+ if(structure == NULL){
239+ return;
240+ }
241+
242+#ifdef DEBUG_MEMORY_REFERENCE_COUNT
243+ CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure);
244+#endif
245+
246+ CHNLIB_UUID_Free(*structure);
247+
248+ *structure = NULL;
249+
250+ return;
251+}
252+
253+uint CHNLIB_UUID_Internal_GetHash(const void *structure)
254+{
255+ uint hash;
256+ CHNLIB_UUID *uuid;
257+
258+ hash = 0;
259+
260+ if(CHNLIB_StructureHeader_GetTypeID(structure) == CHNLIB_STRUCT_ID_UUID){
261+ uuid = (CHNLIB_UUID *)structure;
262+ hash = ((uuid->time_low[3] << 24) | (uuid->time_mid[1] << 16) | (uuid->clock_seq_low[0] << 8) | (uuid->node[5]));
263+ }
264+
265+ return hash;
266+}
--- a/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj
+++ b/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
77 objects = {
88
99 /* Begin PBXBuildFile section */
10+ 174CE46C177FE6A8004FE674 /* libelchnos.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 174CE46B177FE6A8004FE674 /* libelchnos.a */; };
1011 17EE90A216DB67DB005238B2 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 17EE90A116DB67DB005238B2 /* main.c */; };
1112 17EE90A416DB67DB005238B2 /* libtest.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17EE90A316DB67DB005238B2 /* libtest.1 */; };
1213 17EE90B616DB6898005238B2 /* libchn.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 17EE90B516DB6898005238B2 /* libchn.a */; };
@@ -36,6 +37,8 @@
3637 /* End PBXCopyFilesBuildPhase section */
3738
3839 /* Begin PBXFileReference section */
40+ 174CE46B177FE6A8004FE674 /* libelchnos.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libelchnos.a; path = ../DerivedData/CHNOSProject/Build/Products/Debug/libelchnos.a; sourceTree = "<group>"; };
41+ 174CE46E177FE6F5004FE674 /* elctest.elc */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 4; path = elctest.elc; sourceTree = "<group>"; };
3942 17EE909E16DB67DB005238B2 /* libtest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = libtest; sourceTree = BUILT_PRODUCTS_DIR; };
4043 17EE90A116DB67DB005238B2 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
4144 17EE90A316DB67DB005238B2 /* libtest.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = libtest.1; sourceTree = "<group>"; };
@@ -48,6 +51,7 @@
4851 isa = PBXFrameworksBuildPhase;
4952 buildActionMask = 2147483647;
5053 files = (
54+ 174CE46C177FE6A8004FE674 /* libelchnos.a in Frameworks */,
5155 17EE90B616DB6898005238B2 /* libchn.a in Frameworks */,
5256 );
5357 runOnlyForDeploymentPostprocessing = 0;
@@ -66,6 +70,7 @@
6670 17EE909516DB67DB005238B2 = {
6771 isa = PBXGroup;
6872 children = (
73+ 174CE46B177FE6A8004FE674 /* libelchnos.a */,
6974 17EE90B516DB6898005238B2 /* libchn.a */,
7075 17EE90B416DB6890005238B2 /* chn.xcodeproj */,
7176 17EE90A016DB67DB005238B2 /* libtest */,
@@ -86,6 +91,7 @@
8691 children = (
8792 17EE90A116DB67DB005238B2 /* main.c */,
8893 17EE90A316DB67DB005238B2 /* libtest.1 */,
94+ 174CE46E177FE6F5004FE674 /* elctest.elc */,
8995 );
9096 path = libtest;
9197 sourceTree = "<group>";
--- a/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -7,7 +7,7 @@
77 <key>libtest.xcscheme</key>
88 <dict>
99 <key>orderHint</key>
10- <integer>4</integer>
10+ <integer>6</integer>
1111 </dict>
1212 </dict>
1313 <key>SuppressBuildableAutocreation</key>
--- a/CHNOSProject/libtest/libtest/main.c
+++ b/CHNOSProject/libtest/libtest/main.c
@@ -8,32 +8,49 @@
88
99 #include <stdio.h>
1010 #include "chnlib.h"
11+#include <time.h>
1112
1213 int main(int argc, const char * argv[])
1314 {
14- CHNLIB_String *str1, *str2, *str;
15- CHNLIB_UIPArray *array;
15+ int i;
16+ CHNLIB_RandomGeneratorMT *mt;
17+ CHNLIB_UUID *uuid, *uuid2;
1618
17- str = CHNLIB_String_Initialize("あいうえおかきs");
18- printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str));
19+ mt = CHNLIB_RandomGeneratorMT_Initialise();
20+ CHNLIB_RandomGeneratorMT_SetSeed(mt, (int)&mt ^ time(NULL));
1921
20- str1 = CHNLIB_String_Initialize("あい");
21- printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str1), CHNLIB_String_GetReferencePointerOfCString(str1));
22- str2 = CHNLIB_String_Initialize("かき");
23- printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str2), CHNLIB_String_GetReferencePointerOfCString(str2));
24-
25- str = CHNLIB_String_Concatenate(str1, str2);
26- printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str));
22+ for(i = 0; i < 1024; i++){
23+ uuid = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt);
24+ CHNLIB_UUID_Print(uuid);
25+ printf(":0x%08X\n", CHNLIB_StructureHeader_GetHash(uuid));
26+ release(uuid);
27+ }
28+
29+ uuid = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt);
30+ putc('\n', stdout);
31+ CHNLIB_UUID_Print(uuid);
32+
33+ uuid2 = CHNLIB_UUID_Copy(uuid);
34+ putc('\n', stdout);
35+ CHNLIB_UUID_Print(uuid2);
2736
28- str = CHNLIB_String_Concatenate(str, str);
29- printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str));
37+ if(CHNLIB_UUID_IsEqualToUUID(uuid, uuid2)){
38+ printf("\neq\n");
39+ } else{
40+ printf("\nNOTeq\n");
41+ }
42+ release(uuid2);
3043
31- array = CHNLIB_UIPArray_Initialize();
32- CHNLIB_UIPArray_AppendLast(&array, 0, str);
33- printf("0x%X", CHNLIB_StructureHeader_GetHash(array));
44+ uuid2 = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt);
45+ putc('\n', stdout);
46+ CHNLIB_UUID_Print(uuid2);
3447
35- CHNLIB_UIPArray_AppendLast(&array, 0, str);
36- printf("0x%X", CHNLIB_StructureHeader_GetHash(array));
48+ if(CHNLIB_UUID_IsEqualToUUID(uuid, uuid2)){
49+ printf("\neq\n");
50+ } else{
51+ printf("\nNOTeq\n");
52+ }
53+ release(uuid2);
3754
3855 return 0;
3956 }
Show on old repository browser