PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/typeinfo/ti_staticarray.d

http://github.com/wilkie/djehuty
D | 96 lines | 71 code | 19 blank | 6 comment | 10 complexity | 9335e4c51e5888bbec16bf8b8d361ea1 MD5 | raw file
 1/*
 2 * ti_staticarray.d
 3 *
 4 * This module implements the TypeInfo for static arrays.
 5 *
 6 */
 7
 8module runtime.typeinfo.ti_staticarray;
 9
10class TypeInfo_StaticArray : TypeInfo {
11	char[] toString() {
12		char[20] buf;
13		return value.toString() ~ "[" ~ itoa(buf, 'd', len) ~ "]";
14	}
15
16	int opEquals(Object o) {
17		TypeInfo_StaticArray c;
18
19		return cast(int)
20			   (this is o ||
21				((c = cast(TypeInfo_StaticArray)o) !is null &&
22				 this.len == c.len &&
23				 this.value == c.value));
24	}
25
26	hash_t getHash(void *p) {	
27		size_t sz = value.tsize();
28		hash_t hash = 0;
29		for (size_t i = 0; i < len; i++)
30			hash += value.getHash(p + i * sz);
31		return hash;
32	}
33
34	int equals(void *p1, void *p2) {
35		size_t sz = value.tsize();
36
37		for (size_t u = 0; u < len; u++) {
38			if (!value.equals(p1 + u * sz, p2 + u * sz))
39				return 0;
40		}
41		return 1;
42	}
43
44	int compare(void *p1, void *p2) {
45		size_t sz = value.tsize();
46
47		for (size_t u = 0; u < len; u++) {
48			int result = value.compare(p1 + u * sz, p2 + u * sz);
49			if (result)
50				return result;
51		}
52		return 0;
53	}
54
55	size_t tsize() {
56		return len * value.tsize();
57	}
58
59	void swap(void *p1, void *p2) {
60		void* tmp;
61		size_t sz = value.tsize();
62		ubyte[16] buffer;
63		void* pbuffer;
64
65		if (sz < buffer.sizeof)
66			tmp = buffer.ptr;
67		else
68			tmp = pbuffer = (new void[sz]).ptr;
69
70		for (size_t u = 0; u < len; u += sz) {
71			size_t o = u * sz;
72			tmp[0 .. sz] = (p1 + o)[0 .. sz];
73			(p1 + o)[0 .. sz] = (p2 + o)[0 .. sz];
74			(p2 + o)[0 .. sz] = tmp[0 .. sz];
75		}
76		if (pbuffer)
77			delete pbuffer;
78	}
79
80	void[] init() {
81		return value.init();
82	}
83
84	TypeInfo next() {
85		return value;
86	}
87
88	uint flags() {
89		return value.flags(); 
90	}
91
92	TypeInfo value;
93	size_t len;
94}
95
96