Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/CFBag.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ CFBagCreate (CFAllocatorRef allocator, const void **values, CFIndex numValues,
{
return (CFBagRef)GSHashTableCreate (allocator, _kCFBagTypeID,
values, values, numValues,
(const GSHashTableKeyCallBacks*)callBacks, NULL);
(const GSHashTableKeyCallBacks*)callBacks, NULL, true);
}

CFBagRef
Expand Down
2 changes: 1 addition & 1 deletion Source/CFDictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CFDictionaryCreate (CFAllocatorRef allocator, const void **keys,
return (CFDictionaryRef)GSHashTableCreate (allocator, _kCFDictionaryTypeID,
keys, values, numValues,
(const GSHashTableKeyCallBacks*)keyCallBacks,
(const GSHashTableValueCallBacks*)valueCallBacks);
(const GSHashTableValueCallBacks*)valueCallBacks, false);
}

CFDictionaryRef
Expand Down
2 changes: 1 addition & 1 deletion Source/CFSet.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ CFSetCreate (CFAllocatorRef allocator, const void **values, CFIndex numValues,
return (CFSetRef) GSHashTableCreate (allocator, _kCFSetTypeID,
values, values, numValues,
(const GSHashTableKeyCallBacks *)
callBacks, NULL);
callBacks, NULL, false);
}

CFSetRef
Expand Down
17 changes: 13 additions & 4 deletions Source/GSHashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ GSHashTableRef
GSHashTableCreate (CFAllocatorRef alloc, CFTypeID typeID,
const void **keys, const void **values, CFIndex numValues,
const GSHashTableKeyCallBacks * keyCallBacks,
const GSHashTableValueCallBacks * valueCallBacks)
const GSHashTableValueCallBacks * valueCallBacks,
Boolean shouldCount)
{
CFIndex arraySize;
CFIndex capacity;
Expand All @@ -290,6 +291,9 @@ GSHashTableCreate (CFAllocatorRef alloc, CFTypeID typeID,

new->_capacity = capacity;

if (shouldCount)
GSHashTableSetShouldCount (new);

if (keyCallBacks == NULL)
keyCallBacks = &_kGSNullHashTableKeyCallBacks;
if (valueCallBacks == NULL)
Expand All @@ -306,8 +310,12 @@ GSHashTableCreate (CFAllocatorRef alloc, CFTypeID typeID,
{
bucket = GSHashTableFindBucket (new, keys[idx],
_kGSHashTableInsert);
GSHashTableAddKeyValuePair (new, bucket, keys[idx], values[idx]);
new->_count += 1;
if (shouldCount || bucket->count <= 0)
{
GSHashTableAddKeyValuePair (new, bucket, keys[idx],
values[idx]);
new->_count += 1;
}
}
}
}
Expand All @@ -324,7 +332,8 @@ GSHashTableCreateCopy (CFAllocatorRef alloc, GSHashTableRef table)
count = GSHashTableGetCount (table);
new = GSHashTableCreate (alloc, CFGetTypeID (table), NULL, NULL,
count, &table->_keyCallBacks,
&table->_valueCallBacks);
&table->_valueCallBacks,
GSHashTableShouldCount (table));
if (new)
{
CFIndex idx;
Expand Down
3 changes: 2 additions & 1 deletion Source/GSHashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ GS_PRIVATE GSHashTableRef
GSHashTableCreate (CFAllocatorRef alloc, CFTypeID typeID,
const void **keys, const void **values, CFIndex count,
const GSHashTableKeyCallBacks * keyCallBacks,
const GSHashTableValueCallBacks * valueCallBacks);
const GSHashTableValueCallBacks * valueCallBacks,
Boolean shouldCount);

GS_PRIVATE GSHashTableRef
GSHashTableCreateCopy (CFAllocatorRef alloc, GSHashTableRef table);
Expand Down
27 changes: 27 additions & 0 deletions Tests/CFDictionary/create_duplicates.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "CoreFoundation/CFDictionary.h"
#include "CoreFoundation/CFString.h"
#include "../CFTesting.h"

int main (void)
{
const void *keys[2];
const void *values[2];
CFDictionaryRef dict;

keys[0] = CFSTR ("k");
keys[1] = CFSTR ("k");
values[0] = CFSTR ("v1");
values[1] = CFSTR ("v2");

dict = CFDictionaryCreate (NULL, keys, values, 2,
&kCFCopyStringDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
PASS_CF(CFDictionaryGetCount (dict) == 1,
"A key repeated in the input array is stored once.");
PASS_CF(CFDictionaryContainsKey (dict, CFSTR ("k")),
"The key is present.");

CFRelease (dict);

return 0;
}
24 changes: 24 additions & 0 deletions Tests/CFSet/create_duplicates.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "CoreFoundation/CFSet.h"
#include "CoreFoundation/CFString.h"
#include "../CFTesting.h"

int main (void)
{
const void *values[3];
CFSetRef set;

values[0] = CFSTR ("a");
values[1] = CFSTR ("b");
values[2] = CFSTR ("a");

set = CFSetCreate (NULL, values, 3, &kCFTypeSetCallBacks);
PASS_CF(CFSetGetCount (set) == 2,
"A value repeated in the input array is stored once.");
PASS_CF(CFSetContainsValue (set, CFSTR ("a"))
&& CFSetContainsValue (set, CFSTR ("b")),
"Both distinct values are present.");

CFRelease (set);

return 0;
}
Loading