ObjFW  Check-in [ac51f5f0cf]

Overview
Comment:Improve -[hash] of OFNumber and OFObject.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ac51f5f0cf17e97a41e9d3ffee02225dadf92ef50ece4623de3fac8cd5e32698
User & Date: js on 2012-12-06 00:14:47
Other Links: manifest | tags
Context
2012-12-06
00:51
Clean up a few -[hash] methods. check-in: 10f221ad30 user: js tags: trunk
00:14
Improve -[hash] of OFNumber and OFObject. check-in: ac51f5f0cf user: js tags: trunk
2012-12-05
21:28
OFMapTable: Reseed on resize. check-in: f7018aedf9 user: js tags: trunk
Changes

Modified src/OFDate.m from [6b0927552c] to [2efbaabb9c].

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
- (uint32_t)hash
{
	uint32_t hash;
	union {
		double d;
		uint8_t b[sizeof(double)];
	} d;
	uint8_t i;

	d.d = OF_BSWAP_DOUBLE_IF_LE(seconds);

	OF_HASH_INIT(hash);

	for (i = 0; i < sizeof(double); i++)
		OF_HASH_ADD(hash, d.b[i]);

	OF_HASH_FINALIZE(hash);







|

|







364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
- (uint32_t)hash
{
	uint32_t hash;
	union {
		double d;
		uint8_t b[sizeof(double)];
	} d;
	uint_fast8_t i;

	d.d = OF_BSWAP_DOUBLE_IF_BE(seconds);

	OF_HASH_INIT(hash);

	for (i = 0; i < sizeof(double); i++)
		OF_HASH_ADD(hash, d.b[i]);

	OF_HASH_FINALIZE(hash);

Modified src/OFNumber.m from [2866f4968c] to [a189f6857c].

1036
1037
1038
1039
1040
1041
1042

1043
1044
1045

1046
1047
1048
1049


1050
1051



1052


1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067

1068
1069
1070
1071
1072
1073
1074


1075

1076

1077
1078



1079





1080

1081

1082
1083
1084
1085
1086
1087
1088

		return OF_ORDERED_SAME;
	}
}

- (uint32_t)hash
{

	uint32_t hash;
	uint8_t i;


	switch (type) {
	case OF_NUMBER_FLOAT:;
		union {
			float f;


			uint8_t b[sizeof(float)];
		} f;






		f.f = OF_BSWAP_FLOAT_IF_LE(value.float_);

		OF_HASH_INIT(hash);

		for (i = 0; i < sizeof(float); i++)
			OF_HASH_ADD(hash, f.b[i]);

		OF_HASH_FINALIZE(hash);

		return hash;
	case OF_NUMBER_DOUBLE:;
		union {
			double d;
			uint8_t b[sizeof(double)];
		} d;


		d.d = OF_BSWAP_DOUBLE_IF_LE(value.double_);

		OF_HASH_INIT(hash);

		for (i = 0; i < sizeof(double); i++)
			OF_HASH_ADD(hash, d.b[i]);




		OF_HASH_FINALIZE(hash);


		return hash;



	default:





		return [self uInt32Value];

	}

}

- (OFNumber*)numberByAddingNumber: (OFNumber*)num
{
	CALCULATE(+, num)
}








>

<

>
|
|
|
|
>
>
|
|
>
>
>
|
>
>
|
|
<
|
<
<

|

<
|




>

|
<
<



>
>

>
|
>
|
|
>
>
>
|
>
>
>
>
>
|
>
|
>







1036
1037
1038
1039
1040
1041
1042
1043
1044

1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062

1063


1064
1065
1066

1067
1068
1069
1070
1071
1072
1073
1074


1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105

		return OF_ORDERED_SAME;
	}
}

- (uint32_t)hash
{
	of_number_type_t type_ = type;
	uint32_t hash;


	/* Do we really need signed to represent this number? */
	if (type_ & OF_NUMBER_SIGNED && [self intMaxValue] >= 0)
		type_ &= ~OF_NUMBER_SIGNED;

	/* Do we really need floating point to represent this number? */
	if (type_ & OF_NUMBER_FLOAT) {
		double v = [self doubleValue];

		if (v < 0) {
			if (v == [self intMaxValue]) {
				type_ &= ~OF_NUMBER_FLOAT;
				type_ |= OF_NUMBER_SIGNED;
			}
		} else {
			if (v == [self uIntMaxValue])
				type_ &= ~OF_NUMBER_FLOAT;
		}

	}



	OF_HASH_INIT(hash);


	if (type_ & OF_NUMBER_FLOAT) {
		union {
			double d;
			uint8_t b[sizeof(double)];
		} d;
		uint_fast8_t i;

		d.d = OF_BSWAP_DOUBLE_IF_BE([self doubleValue]);



		for (i = 0; i < sizeof(double); i++)
			OF_HASH_ADD(hash, d.b[i]);
	} else if (type_ & OF_NUMBER_SIGNED) {
		intmax_t v = [self intMaxValue] * -1;

		while (v != 0) {
			OF_HASH_ADD(hash, v & 0xFF);
			v >>= 8;
		}

		OF_HASH_ADD(hash, 1);
	} else {
		uintmax_t v = [self uIntMaxValue];

		while (v != 0) {
			OF_HASH_ADD(hash, v & 0xFF);
			v >>= 8;
		}
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}

- (OFNumber*)numberByAddingNumber: (OFNumber*)num
{
	CALCULATE(+, num)
}

Modified src/OFObject.m from [df05949c71] to [e989a30df6].

840
841
842
843
844
845
846

847












848
849
850
851
852

853
854
855
856
857
858
859
	/* Classes containing data should reimplement this! */
	return (self == object);
}

- (uint32_t)hash
{
	/* Classes containing data should reimplement this! */

	return (uint32_t)(uintptr_t)self;












}

- (OFString*)description
{
	/* Classes containing data should reimplement this! */

	return [OFString stringWithFormat: @"<%@: %p>", [self className], self];
}

- (void*)allocMemoryWithSize: (size_t)size
{
	void *pointer;
	struct pre_mem *preMem;







>
|
>
>
>
>
>
>
>
>
>
>
>
>





>







840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
	/* Classes containing data should reimplement this! */
	return (self == object);
}

- (uint32_t)hash
{
	/* Classes containing data should reimplement this! */

	uintptr_t ptr = (uintptr_t)self;
	uint32_t hash;

	OF_HASH_INIT(hash);

	while (ptr != 0) {
		OF_HASH_ADD(hash, ptr & 0xFF);
		ptr <<= 8;
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}

- (OFString*)description
{
	/* Classes containing data should reimplement this! */

	return [OFString stringWithFormat: @"<%@: %p>", [self className], self];
}

- (void*)allocMemoryWithSize: (size_t)size
{
	void *pointer;
	struct pre_mem *preMem;

Modified src/macros.h from [419a2f32d2] to [ba733f14be].

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#endif

#define OF_ROL(value, bits)						\
	(((value) << ((bits) % (sizeof(value) * 8))) |			\
	(value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))

#define OF_HASH_INIT(hash) hash = of_hash_seed
#define OF_HASH_ADD(hash, byte)		\
	{				\
		hash += (uint8_t)byte;	\
		hash += (hash << 10);	\
		hash ^= (hash >> 6);	\
	}
#define OF_HASH_FINALIZE(hash)		\
	{				\
		hash += (hash << 3);	\
		hash ^= (hash >> 11);	\
		hash += (hash << 15);	\
	}







|
|
|
|
|







323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#endif

#define OF_ROL(value, bits)						\
	(((value) << ((bits) % (sizeof(value) * 8))) |			\
	(value) >> (sizeof(value) * 8 - ((bits) % (sizeof(value) * 8))))

#define OF_HASH_INIT(hash) hash = of_hash_seed
#define OF_HASH_ADD(hash, byte)			\
	{					\
		hash += (uint8_t)(byte);	\
		hash += (hash << 10);		\
		hash ^= (hash >> 6);		\
	}
#define OF_HASH_FINALIZE(hash)		\
	{				\
		hash += (hash << 3);	\
		hash ^= (hash >> 11);	\
		hash += (hash << 15);	\
	}

Modified tests/OFNumberTests.m from [aa0e94c4b8] to [18560fc0d4].

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

	TEST(@"+[numberWithIntMax:]",
	    (num = [OFNumber numberWithIntMax: 123456789]))

	TEST(@"-[isEqual:]",
	    [num isEqual: [OFNumber numberWithUInt32: 123456789]])

	TEST(@"-[hash]", [num hash] == 123456789)

	TEST(@"-[asDouble]", [num doubleValue] == 123456789.L)

	TEST(@"-[numberByDecreasing]",
	    [[num numberByDecreasing]
	    isEqual: [OFNumber numberWithInt32: 123456788]])

	TEST(@"-[numberByDividingBy:]",
	    [[num numberByDividingWithNumber: [OFNumber numberWithInt: 2]]







|

|







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

	TEST(@"+[numberWithIntMax:]",
	    (num = [OFNumber numberWithIntMax: 123456789]))

	TEST(@"-[isEqual:]",
	    [num isEqual: [OFNumber numberWithUInt32: 123456789]])

	TEST(@"-[hash]", [num hash] == 0x82D8BC42)

	TEST(@"-[doubleValue]", [num doubleValue] == 123456789.L)

	TEST(@"-[numberByDecreasing]",
	    [[num numberByDecreasing]
	    isEqual: [OFNumber numberWithInt32: 123456788]])

	TEST(@"-[numberByDividingBy:]",
	    [[num numberByDividingWithNumber: [OFNumber numberWithInt: 2]]

Modified tests/OFStringTests.m from [7769c4140e] to [bb5fb35102].

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

	TEST(@"-[appendString:] and -[appendUTF8String:]",
	    R([s[1] appendUTF8String: "1๐„ž"]) && R([s[1] appendString: @"3"]) &&
	    R([s[0] appendString: s[1]]) && [s[0] isEqual: @"tรคsโ‚ฌ1๐„ž3"])

	TEST(@"-[length]", [s[0] length] == 7)
	TEST(@"-[UTF8StringLength]", [s[0] UTF8StringLength] == 13)
	TEST(@"-[hash]", [s[0] hash] == 0x2E623BD1)

	TEST(@"-[characterAtIndex:]", [s[0] characterAtIndex: 0] == 't' &&
	    [s[0] characterAtIndex: 1] == 0xE4 &&
	    [s[0] characterAtIndex: 3] == 0x20AC &&
	    [s[0] characterAtIndex: 5] == 0x1D11E)

	EXPECT_EXCEPTION(@"Detect out of range in -[characterAtIndex:]",







|







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

	TEST(@"-[appendString:] and -[appendUTF8String:]",
	    R([s[1] appendUTF8String: "1๐„ž"]) && R([s[1] appendString: @"3"]) &&
	    R([s[0] appendString: s[1]]) && [s[0] isEqual: @"tรคsโ‚ฌ1๐„ž3"])

	TEST(@"-[length]", [s[0] length] == 7)
	TEST(@"-[UTF8StringLength]", [s[0] UTF8StringLength] == 13)
	TEST(@"-[hash]", [s[0] hash] == 0x705583C0)

	TEST(@"-[characterAtIndex:]", [s[0] characterAtIndex: 0] == 't' &&
	    [s[0] characterAtIndex: 1] == 0xE4 &&
	    [s[0] characterAtIndex: 3] == 0x20AC &&
	    [s[0] characterAtIndex: 5] == 0x1D11E)

	EXPECT_EXCEPTION(@"Detect out of range in -[characterAtIndex:]",

Modified tests/serialization.xml from [0489605714] to [2be1e47e3a].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version='1.0' encoding='UTF-8'?>
<serialization xmlns='https://webkeks.org/objfw/serialization' version='1'>
  <OFMutableDictionary>
    <key>
      <OFArray>
        <OFString>Qu&quot;xbar
test</OFString>
        <OFNumber type='signed'>1234</OFNumber>
        <OFNumber type='double'>40934a456d5cfaad</OFNumber>
        <OFMutableString>asd</OFMutableString>
        <OFDate>40934a456d5cfaad</OFDate>
      </OFArray>
    </key>
    <object>
      <OFString>Hello</OFString>
    </object>
    <key>
      <OFString>Blub</OFString>
    </key>
    <object>
      <OFString>B&quot;la</OFString>
    </object>
    <key>
      <OFDataArray>MDEyMzQ1Njc4OTo7PEFCQ0RFRkdISklLTE1OT1BRUlNUVVZXWFla</OFDataArray>
    </key>
    <object>
      <OFString>data</OFString>
    </object>
    <key>
      <OFList>
        <OFString>Hello</OFString>
        <OFString>Wo&#xD;ld!
How are you?</OFString>
        <OFURL>https://webkeks.org/</OFURL>
        <OFXMLElement name='x'>
          <children>




<
<
<
<
<
<
<
<
<
<
<
<
<






<
<
<
<
<
<







1
2
3
4













5
6
7
8
9
10






11
12
13
14
15
16
17
<?xml version='1.0' encoding='UTF-8'?>
<serialization xmlns='https://webkeks.org/objfw/serialization' version='1'>
  <OFMutableDictionary>
    <key>













      <OFString>Blub</OFString>
    </key>
    <object>
      <OFString>B&quot;la</OFString>
    </object>
    <key>






      <OFList>
        <OFString>Hello</OFString>
        <OFString>Wo&#xD;ld!
How are you?</OFString>
        <OFURL>https://webkeks.org/</OFURL>
        <OFXMLElement name='x'>
          <children>
53
54
55
56
57
58
59



















60
61
          </object>
        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>



















  </OFMutableDictionary>
</serialization>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
          </object>
        </OFCountedSet>
      </OFList>
    </key>
    <object>
      <OFString>list</OFString>
    </object>
    <key>
      <OFDataArray>MDEyMzQ1Njc4OTo7PEFCQ0RFRkdISklLTE1OT1BRUlNUVVZXWFla</OFDataArray>
    </key>
    <object>
      <OFString>data</OFString>
    </object>
    <key>
      <OFArray>
        <OFString>Qu&quot;xbar
test</OFString>
        <OFNumber type='signed'>1234</OFNumber>
        <OFNumber type='double'>40934a456d5cfaad</OFNumber>
        <OFMutableString>asd</OFMutableString>
        <OFDate>40934a456d5cfaad</OFDate>
      </OFArray>
    </key>
    <object>
      <OFString>Hello</OFString>
    </object>
  </OFMutableDictionary>
</serialization>