PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/typeinfo/ti_array_object.d

http://github.com/wilkie/djehuty
D | 96 lines | 74 code | 14 blank | 8 comment | 18 complexity | b565be09812962397c3c3ff86695ef2b MD5 | raw file
 1/*
 2 * ti_array_object.d
 3 *
 4 * This module implements the TypeInfo for Object[]
 5 *
 6 */
 7
 8module runtime.typeinfo.ti_array_object;
 9
10class TypeInfo_AC : TypeInfo {
11	hash_t getHash(void *p) {
12		Object[] s = *cast(Object[]*)p;
13
14		hash_t hash = 0;
15
16		foreach (Object o; s) {
17			if (o) {
18				hash += o.toHash();
19			}
20		}
21		return hash;
22	}
23
24	int equals(void *p1, void *p2) {
25		Object[] s1 = *cast(Object[]*)p1;
26		Object[] s2 = *cast(Object[]*)p2;
27
28		if (s1.length == s2.length) {
29			for (size_t u = 0; u < s1.length; u++) {
30				Object o1 = s1[u];
31				Object o2 = s2[u];
32
33				// Do not pass null's to Object.opEquals()
34				if (o1 is o2 ||	(!(o1 is null) && !(o2 is null) && o1.opEquals(o2))) {
35					continue;
36				}
37				return 0;
38			}
39			return 1;
40		}
41		return 0;
42	}
43
44	int compare(void *p1, void *p2) {
45		Object[] s1 = *cast(Object[]*)p1;
46		Object[] s2 = *cast(Object[]*)p2;
47		ptrdiff_t c;
48
49		c = cast(ptrdiff_t)s1.length - cast(ptrdiff_t)s2.length;
50		if (c == 0) {
51			for (size_t u = 0; u < s1.length; u++) {
52				Object o1 = s1[u];
53				Object o2 = s2[u];
54
55				if (o1 is o2) {
56					continue;
57				}
58
59				// Regard null references as always being "less than"
60				if (o1) {
61					if (!o2) {
62						c = 1;
63						break;
64					}
65					c = o1.opCmp(o2);
66					if (c) {
67						break;
68					}
69				}
70				else {
71					c = -1;
72					break;
73				}
74			}
75		}
76		if (c < 0) {
77			c = -1;
78		}
79		else if (c > 0) {
80			c = 1;
81		}
82		return c;
83	}
84
85	size_t tsize() {
86		return (Object[]).sizeof;
87	}
88
89	uint flags() {
90		return 1;
91	}
92
93	TypeInfo next() {
94		return typeid(Object);
95	}
96}