summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <matthias@benkard.de>2009-01-13 19:25:00 +0100
committerMatthias Andreas Benkard <matthias@benkard.de>2009-01-13 19:25:00 +0100
commitdf867c1495f92c0df046199557cab647a2f48170 (patch)
tree927d26f6b34784aa4a84d4b86233e77a8d439918
parentc456224caf73a20c9421f6250f0f876196c3d8d0 (diff)
MLKDictionary: Implement required methods.
-rw-r--r--MLKCons.h3
-rw-r--r--MLKCons.m10
-rw-r--r--MLKDictionary.h6
-rw-r--r--MLKDictionary.m123
4 files changed, 129 insertions, 13 deletions
diff --git a/MLKCons.h b/MLKCons.h
index 32c346c..2fb8855 100644
--- a/MLKCons.h
+++ b/MLKCons.h
@@ -131,6 +131,7 @@
-(id) copyWithZone:(NSZone *)zone;
-(BOOL) isEqual:(id)object;
-
+-(unsigned int) hash;
+
-(void) dealloc;
@end
diff --git a/MLKCons.m b/MLKCons.m
index 2ab5a8b..07c8739 100644
--- a/MLKCons.m
+++ b/MLKCons.m
@@ -177,12 +177,18 @@
-(BOOL) isEqual:(id)object
{
if ([object isKindOfClass:[MLKCons class]])
- return ([((MLKCons*)object)->_car isEqual:_car]
- && [((MLKCons*)object)->_cdr isEqual:_cdr]);
+ return ([nullify(((MLKCons*)object)->_car) isEqual:_car]
+ && [nullify(((MLKCons*)object)->_cdr) isEqual:_cdr]);
else
return NO;
}
+-(unsigned int) hash
+{
+ // FIXME: Well...
+ return [nullify(_car) hash]/2 + [nullify(_cdr) hash]/2;
+}
+
-(id) copyWithZone:(NSZone *)zone
{
MLKCons *copy = [MLKCons allocWithZone:zone];
diff --git a/MLKDictionary.h b/MLKDictionary.h
index cf3b03a..c88ad55 100644
--- a/MLKDictionary.h
+++ b/MLKDictionary.h
@@ -29,7 +29,9 @@
+(void) initialize;
+(id) dictionary;
+
-(id) init;
+-(id) initWithCapacity:(unsigned int)numItems;
-(id) initEqTable;
-(id) initEqlTable;
-(id) initEqualTable;
@@ -42,6 +44,10 @@
-(NSEnumerator *) keyEnumerator;
-(id) objectForKey:(id)key;
-(void) setObject:(id)object forKey:(id)key;
+-(void) removeObjectForKey:(id)key;
+-(void) removeAllObjects;
+-(NSArray *) allKeys;
+-(NSArray *) allValues;
-(void) dealloc;
-(void) finalize;
diff --git a/MLKDictionary.m b/MLKDictionary.m
index f80b886..9095c91 100644
--- a/MLKDictionary.m
+++ b/MLKDictionary.m
@@ -16,7 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#import "MLKCharacter.h"
+#import "MLKCons.h"
#import "MLKDictionary.h"
+#import "MLKInteger.h"
#import "functions.h"
#import "globals.h"
#import "util.h"
@@ -28,6 +31,7 @@
#include <string.h>
+// http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/doc/uid/20000055-BCIGHBEC
static NSMapTableValueCallBacks value_callbacks;
static NSMapTableKeyCallBacks
@@ -56,7 +60,12 @@ static BOOL eql (NSMapTable *table, const void *p_x, const void *p_y)
if ((MLKInstanceP (x) && ![x isKindOfClass:[MLKInteger class]])
|| (MLKInstanceP (y) && ![x isKindOfClass:[MLKInteger class]]))
- return x == y;
+ {
+ if ([x isKindOfClass:[MLKCharacter class]])
+ return [x isEqual:y];
+ else
+ return x == y;
+ }
else
{
x = MLKCanoniseInteger (x);
@@ -71,32 +80,58 @@ static BOOL eql (NSMapTable *table, const void *p_x, const void *p_y)
}
}
-static unsigned eql_hash (NSMapTable *table, const void *x)
+static unsigned int eql_hash (NSMapTable *table, const void *x)
{
if (MLKInstanceP ((id) x))
- return (unsigned) x;
+ return (unsigned int) x;
else
- return (unsigned) MLKCanoniseInteger(x);
+ return (unsigned int) MLKCanoniseInteger(x);
}
-static BOOL equal (NSMapTable *table, const void *x, const void *y)
+static BOOL equal (NSMapTable *table, const void *p_x, const void *p_y)
{
- // FIXME
+ id x = (id)p_x, y = (id)p_y;
+
+ if (MLKFixnumP(x)
+ || MLKFixnumP(y)
+ || [x isKindOfClass:[MLKNumber class]]
+ || [x isKindOfClass:[MLKCharacter class]])
+ return eql (table, x, y);
+ else if ([x isKindOfClass:[MLKCons class]])
+ return ([y isKindOfClass:[MLKCons class]]
+ && equal(table, [x car], [y car])
+ && equal(table, [x cdr], [y cdr]));
+ else if ([x isKindOfClass:[NSString class]])
+ return [x isEqual:y];
+ // FIXME: Missing cases: pathname, bit vector
+ else
+ return (x == y);
}
-static unsigned equal_hash (NSMapTable *table, const void *x)
+static unsigned int equal_hash (NSMapTable *table, const void *p_x)
{
- // FIXME
+ id x = (id)p_x;
+
+ if (MLKFixnumP(x))
+ return (unsigned int) x;
+ else if ([x isKindOfClass:[MLKCons class]]
+ || [x isKindOfClass:[NSString class]])
+ return [x hash];
+ // FIXME: Missing cases: pathname, bit vector
+ else
+ return (unsigned int) x;
}
-static BOOL equalp (NSMapTable *table, const void *x, const void *y)
+static BOOL equalp (NSMapTable *table, const void *p_x, const void *p_y)
{
// FIXME
+ return equal(table, p_x, p_y);
}
-static unsigned equalp_hash (NSMapTable *table, const void *x)
+static unsigned int equalp_hash (NSMapTable *table, const void *x)
{
// FIXME
+ return 0;
}
@@ -146,15 +181,83 @@ static unsigned equalp_hash (NSMapTable *table, const void *x)
return [self initEqlTable];
}
+-(id) initWithCapacity:(unsigned int)numItems
+{
+ m_table = NSCreateMapTable (eql_callbacks, value_callbacks, numItems);
+ return self;
+}
+
+-(id) initEqTable
+{
+ m_table = NSCreateMapTable (eq_callbacks, value_callbacks, 0);
+ return self;
+}
+
-(id) initEqlTable
{
m_table = NSCreateMapTable (eql_callbacks, value_callbacks, 0);
return self;
}
+-(id) initEqualTable
+{
+ m_table = NSCreateMapTable (equal_callbacks, value_callbacks, 0);
+ return self;
+}
+
+-(id) initEqualPTable
+{
+ m_table = NSCreateMapTable (equalp_callbacks, value_callbacks, 0);
+ return self;
+}
+
+
+-(NSUInteger) count
+{
+ return NSCountMapTable (m_table);
+}
+
+-(NSEnumerator *) keyEnumerator
+{
+ NSArray *keys = NSAllMapTableKeys (m_table);
+ return [keys objectEnumerator];
+}
+
+-(id) objectForKey:(id)key
+{
+ return NSMapGet (m_table, key);
+}
+
+-(void) setObject:(id)object forKey:(id)key
+{
+ NSMapInsert (m_table, key, object);
+}
+
+-(void) removeObjectForKey:(id)key
+{
+ NSMapRemove (m_table, key);
+}
+
+-(void) removeAllObjects
+{
+ NSResetMapTable (m_table);
+}
+
+-(NSArray *) allKeys
+{
+ return NSAllMapTableKeys (m_table);
+}
+
+-(NSArray *) allValues
+{
+ return NSAllMapTableValues (m_table);
+}
+
+
-(void) dealloc
{
NSFreeMapTable (m_table);
+ [super dealloc];
}
-(void) finalize