Selaa lähdekoodia

changed ObjectMap to a class

Simon Krajewski 12 vuotta sitten
vanhempi
commit
bf114ac908

+ 2 - 2
std/cpp/_std/haxe/ds/ObjectMap.hx

@@ -34,7 +34,7 @@ class ObjectMap<K,V> {
 		__Internal.set( untyped __global__.__hxcpp_obj_id(key), value );
 	}
 
-	public function get( key : K ) : V {
+	public function get( key : K ) : Null<V> {
 		return __Internal.get( untyped __global__.__hxcpp_obj_id(key) );
 	}
 
@@ -58,7 +58,7 @@ class ObjectMap<K,V> {
 		return __Internal.iterator();
 	}
 
-	private function toString() : String {
+	public function toString() : String {
 		var s = new StringBuf();
 		s.add("{");
 		var it = __Internal.keys();

+ 22 - 15
std/flash/_std/haxe/ds/ObjectMap.hx

@@ -1,32 +1,39 @@
 package haxe.ds;
 
-@:coreApi
-abstract ObjectMap<K, V>(flash.utils.Dictionary) {
-	public inline function new(weakKeys : Bool = false) {
-		this = new flash.utils.Dictionary(weakKeys);
-	}
-	
-	public inline function get( k : K ) : Null<V> {
-		return untyped this[k];
+class ObjectMap<K,V> extends flash.utils.Dictionary {
+
+	public inline function get( key : K ) : Null<V> {
+		return untyped this[key];
 	}
 
-	public inline function set( k : K, v : V ):Void {
-		untyped this[k] = v;
+	public inline function set( key : K, value : V ):Void {
+		untyped this[key] = value;
 	}
 
-	public inline function exists( k : K ) : Bool {
-		return untyped this[k] != null;
+	public inline function exists( key : K ) : Bool {
+		return untyped this[key] != null;
 	}
 
-	public inline function remove( k : K ):Bool {
-		return untyped __delete__(this,k);
+	public inline function remove( key : K ):Bool {
+		return untyped __delete__(this,key);
 	}
 
-	public inline function keys() : Array<K> {
+	public function keys() : Array<K> {
 		return untyped __keys__(this);
 	}
 
 	public function iterator() : Iterator<V> {
 		return untyped __keys__(this).iterator();
 	}
+
+	public function toString() : String {
+		var s = "";
+		var it = keys();
+		for( i in it ) {
+			s += (s == "" ? "" : ",") + i;
+			s += " => ";
+			s += Std.string(get(i));
+		}
+		return s + "}";
+	}
 }

+ 0 - 56
std/flash/utils/TypedDictionary.hx

@@ -1,56 +0,0 @@
-/*
- * Copyright (c) 2005-2008, The haXe Project Contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   - Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer.
- *   - Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
-package flash.utils;
-
-/**
-	This is a typed version of the Flash9 Dictionary class.
-**/
-class TypedDictionary<K,T> extends Dictionary {
-
-	public inline function get( k : K ) : Null<T> {
-		return untyped this[k];
-	}
-
-	public inline function set( k : K, v : T ) {
-		untyped this[k] = v;
-	}
-
-	public inline function exists( k : K ) : Bool {
-		return untyped this[k] != null;
-	}
-
-	public inline function delete( k : K ) {
-		untyped __delete__(this,k);
-	}
-
-	public inline function keys() : Array<K> {
-		return untyped __keys__(this);
-	}
-
-	public function iterator() : Iterator<K> {
-		return keys().iterator();
-	}
-
-}

+ 29 - 27
std/flash8/_std/haxe/ds/ObjectMap.hx

@@ -2,13 +2,13 @@
  * Copyright (C)2005-2013 Haxe Foundation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
+ * copy of h software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and h permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -23,52 +23,54 @@
 package haxe.ds;
 
 @:coreApi
-abstract ObjectMap <K:{ }, V> ({}){
+class ObjectMap <K:{ }, V> {
 	
 	static var count = 0;
 	
-	static inline function assignId(obj: { } ) {
+	static inline function assignId(obj: { } ):Int {
 		return untyped obj.__id__ = ++count;
 	}
 	
-	static inline function getId(obj: { } ) {
+	static inline function getId(obj: { } ):Int {
 		return untyped obj.__id__;
 	}
 	
-	public function new(weakKeys:Bool = false) {
-		this = untyped __new__(_global["Object"]);
-		untyped this.__keys__ = untyped __new__(_global["Object"]);
+	var h: { };
+	
+	public function new(?weakKeys:Bool = false):Void {
+		h = untyped __new__(_global["Object"]);
+		untyped h.__keys__ = untyped __new__(_global["Object"]);
 	}
 	
-	public function set(key:K, value:V) untyped {
+	public function set(key:K, value:V):Void untyped {
 		var id = "$" + (key.__id__ != null ? key.__id__ : assignId(key));
-		this[id] = value;
-		this.__keys__[id] = key;
+		h[id] = value;
+		h.__keys__[id] = key;
 	}
 	
-	public inline function get(key:K) {
-		return untyped this["$" +getId(key)];
+	public inline function get(key:K):Null<V> {
+		return untyped h["$" +getId(key)];
 	}
 	
-	public inline function exists(key:K) {
-		return untyped this["hasOwnProperty"]("$" +getId(key));
+	public inline function exists(key:K):Bool {
+		return untyped h["hasOwnProperty"]("$" +getId(key));
 	}
 	
 	public function remove( key : K ) : Bool {
 		var key = "$" + getId(key);
-		if( untyped !this["hasOwnProperty"](key) ) return false;
-		untyped __delete__(this,key);
-		untyped __delete__(this.__keys__,key);
+		if( untyped !h["hasOwnProperty"](key) ) return false;
+		untyped __delete__(h,key);
+		untyped __delete__(h.__keys__,key);
 		return true;
 	}
 	
 	public function keys() : Iterator<K> {
 		var a = [];
 		untyped {
-			var keys:Iterator<String> = __hkeys__(this.__keys__)["iterator"]();
+			var keys:Iterator<String> = __hkeys__(h.__keys__)["iterator"]();
 			for (key in keys) {
-				if( this.hasOwnProperty("$" +key) )
-					a.push(this.__keys__["$" +key]);
+				if( h.hasOwnProperty("$" +key) )
+					a.push(h.__keys__["$" +key]);
 			}
 		}
 		return a.iterator();
@@ -76,21 +78,21 @@ abstract ObjectMap <K:{ }, V> ({}){
 	
 	public function iterator() : Iterator<V> {
 		return untyped {
-			ref : this,
-			it : __hkeys__(this.__keys__)["iterator"](),
-			hasNext : function() { return __this__.it[__unprotect__("hasNext")](); },
-			next : function() { var i = __this__.it[__unprotect__("next")](); return __this__.ref["$" +i]; }
+			ref : h,
+			it : __hkeys__(h.__keys__)["iterator"](),
+			hasNext : function() { return __h__.it[__unprotect__("hasNext")](); },
+			next : function() { var i = __h__.it[__unprotect__("next")](); return __h__.ref["$" +i]; }
 		};
 	}
 	
 	public function toString() : String {
 		var s = new StringBuf();
 		s.add("{");
-		var it = keys(this);
+		var it = keys();
 		for( i in it ) {
 			s.add(i);
 			s.add(" => ");
-			s.add(Std.string(get(this,i)));
+			s.add(Std.string(get(i)));
 			if( it.hasNext() )
 				s.add(", ");
 		}

+ 2 - 1
std/haxe/ds/ObjectMap.hx

@@ -25,9 +25,10 @@ package haxe.ds;
 extern class ObjectMap < K: { }, V > {
 	public function new(?weakKeys:Bool = false):Void;
 	public function set(key:K, value:V):Void;
-	public function get(key:K):V;
+	public function get(key:K):Null<V>;
 	public function exists(key:K):Bool;
 	public function remove(key:K):Bool;
 	public function keys():Iterator<K>;
 	public function iterator():Iterator<V>;
+	public function toString():String;
 }

+ 27 - 25
std/js/_std/haxe/ds/ObjectMap.hx

@@ -2,13 +2,13 @@
  * Copyright (C)2005-2013 Haxe Foundation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
+ * copy of h software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and h permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -23,51 +23,53 @@
 package haxe.ds;
 
 @:coreApi
-abstract ObjectMap <K:{ }, V>({}){
+class ObjectMap<K:{ }, V> {
 	
 	static var count = 0;
 	
-	static inline function assignId(obj: { } ) {
+	static inline function assignId(obj: { } ):Int {
 		return untyped obj.__id__ = ++count;
 	}
 	
-	static inline function getId(obj: { } ) {
+	static inline function getId(obj: { } ):Int {
 		return untyped obj.__id__;
 	}
 	
-	public inline function new(weakKeys:Bool = false) {
-		this = { };
-		untyped this.__keys__ = { };
+	var h : { };
+	
+	public inline function new(weakKeys:Bool = false):Void {
+		h = { };
+		untyped h.__keys__ = { };
 	}
 	
-	public function set(key:K, value:V) untyped {
+	public function set(key:K, value:V):Void untyped {
 		var id = key.__id__ != null ? key.__id__ : assignId(key);
-		this[id] = value;
-		this.__keys__[id] = key;
+		h[id] = value;
+		h.__keys__[id] = key;
 	}
 	
-	public inline function get(key:K) {
-		return untyped this[getId(key)];
+	public inline function get(key:K):Null<V> {
+		return untyped h[getId(key)];
 	}
 	
-	public inline function exists(key:K) {
-		return untyped this.hasOwnProperty(getId(key));
+	public inline function exists(key:K):Bool {
+		return untyped h.hasOwnProperty(getId(key));
 	}
 	
 	public function remove( key : K ) : Bool {
 		var id = getId(key);
-		if ( untyped !this.hasOwnProperty(id) ) return false;
-		untyped  __js__("delete")(this[id]);
-		untyped  __js__("delete")(this.__keys__[id]);
+		if ( untyped !h.hasOwnProperty(id) ) return false;
+		untyped  __js__("delete")(h[id]);
+		untyped  __js__("delete")(h.__keys__[id]);
 		return true;
 	}
 	
 	public function keys() : Iterator<K> {
 		var a = [];
 		untyped {
-			__js__("for( var key in this1.__keys__ ) {");
-				if( this.hasOwnProperty(key) )
-					a.push(this.__keys__[key]);
+			__js__("for( var key in this.h.__keys__ ) {");
+				if( h.hasOwnProperty(key) )
+					a.push(h.__keys__[key]);
 			__js__("}");
 		}
 		return a.iterator();
@@ -75,8 +77,8 @@ abstract ObjectMap <K:{ }, V>({}){
 	
 	public function iterator() : Iterator<V> {
 		return untyped {
-			ref : this,
-			it : keys(this),
+			ref : h,
+			it : keys(),
 			hasNext : function() { return __this__.it.hasNext(); },
 			next : function() { var i = __this__.it.next(); return __this__.ref[getId(i)]; }
 		};
@@ -85,11 +87,11 @@ abstract ObjectMap <K:{ }, V>({}){
 	public function toString() : String {
 		var s = new StringBuf();
 		s.add("{");
-		var it = keys(this);
+		var it = keys();
 		for( i in it ) {
 			s.add(i);
 			s.add(" => ");
-			s.add(Std.string(get(this,i)));
+			s.add(Std.string(get(i)));
 			if( it.hasNext() )
 				s.add(", ");
 		}

+ 15 - 13
std/neko/_std/haxe/ds/ObjectMap.hx

@@ -2,13 +2,13 @@
  * Copyright (C)2005-2013 Haxe Foundation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
+ * copy of h software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and h permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
@@ -22,48 +22,50 @@
 package haxe.ds;
 
 @:coreApi
-abstract ObjectMap<K,V>({}) {
+class ObjectMap<K,V> {
 
+	var h : { };
+	
 	public function new(weakKeys:Bool = false) : Void {
-		this = untyped __dollar__hnew(0);
+		h = untyped __dollar__hnew(0);
 	}
 
 	public inline function set( key : K, value : V ) : Void {
-		untyped __dollar__hset(this,key,value,null);
+		untyped __dollar__hset(h,key,value,null);
 	}
 
-	public function get( key : K ) : V {
-		return untyped __dollar__hget(this,key,null);
+	public function get( key : K ) : Null<V> {
+		return untyped __dollar__hget(h,key,null);
 	}
 
 	public inline function exists( key : K ) : Bool {
-		return untyped __dollar__hmem(this,key,null);
+		return untyped __dollar__hmem(h,key,null);
 	}
 
 	public inline function remove( key : K ) : Bool {
-		return untyped __dollar__hremove(this,key,null);
+		return untyped __dollar__hremove(h,key,null);
 	}
 
 	public function keys() : Iterator<K> {
 		var l = new List<K>();
-		untyped __dollar__hiter(this,function(k,_) { l.push(k); });
+		untyped __dollar__hiter(h,function(k,_) { l.push(k); });
 		return l.iterator();
 	}
 
 	public function iterator() : Iterator<V> {
 		var l = new List<V>();
-		untyped __dollar__hiter(this,function(_,v) { l.push(v); });
+		untyped __dollar__hiter(h,function(_,v) { l.push(v); });
 		return l.iterator();
 	}
 
 	public function toString() : String {
 		var s = new StringBuf();
 		s.add("{");
-		var it = keys(this);
+		var it = keys();
 		for( i in it ) {
 			s.add(i);
 			s.add(" => ");
-			s.add(Std.string(get(this,i)));
+			s.add(Std.string(get(i)));
 			if( it.hasNext() )
 				s.add(", ");
 		}

+ 26 - 15
std/php/_std/haxe/ds/ObjectMap.hx

@@ -23,46 +23,57 @@
 package haxe.ds;
 
 @:coreApi
-abstract ObjectMap <K:{ }, V>(StringMap<V>) {
-	static function getId(key: { } ) {
+class ObjectMap <K:{ }, V> {
+	static function getId(key: { } ):String {
 		return untyped __php__("spl_object_hash($key)");
 	}
 	
-	public function new(weakKeys:Bool = false) {
-		this = new StringMap<V>();
+	var h : ArrayAccess<V>;
+	
+	public function new(weakKeys:Bool = false):Void {
+		h = untyped __call__('array');
 	}
 	
-	public function set(key:K, value:V) untyped {
-		untyped this.set(getId(key), value);
+	public function set(key:K, value:V):Void untyped {
+		untyped h[getId(key)] = value;
 	}
 	
-	public function get(key:K) {
-		return this.get(getId(key));
+	public function get(key:K):Null<V> {
+		var id = getId(key);
+		if (untyped __call__("array_key_exists", id, h))
+			return untyped h[id];
+		else
+			return null;
 	}
 	
-	public function exists(key:K) {
-		return this.exists(getId(key));
+	public function exists(key:K):Bool {
+		return untyped __call__("array_key_exists", getId(key), h);
 	}
 	
 	public function remove( key : K ) : Bool {
-		return this.remove(getId(key));
+		var id = getId(key);
+		if (untyped __call__("array_key_exists", id, h)) {
+			untyped __call__("unset", h[id]);
+			return true;
+		} else
+			return false;
 	}
 	
 	public inline function keys() : Iterator<K> {
-		return untyped __call__("new _hx_array_iterator", __call__("array_keys", this.h));
+		return untyped __call__("new _hx_array_iterator", __call__("array_keys", h));
 	}
 	
 	public inline function iterator() : Iterator<V> {
-		return untyped __call__("new _hx_array_iterator", __call__("array_values", this.h));
+		return untyped __call__("new _hx_array_iterator", __call__("array_values", h));
 	}
 	
 	public function toString() : String {
 		var s = "{";
-		var it = keys(this);
+		var it = keys();
 		for( i in it ) {
 			s += i;
 			s += " => ";
-			s += Std.string(get(this,i));
+			s += Std.string(get(i));
 			if( it.hasNext() )
 				s += ", ";
 		}

+ 1 - 1
typeload.ml

@@ -969,7 +969,7 @@ let init_core_api ctx c =
 			end;
 			(match follow f.cf_type, follow f2.cf_type with
 			| TFun (pl1,_), TFun (pl2,_) ->
-				if List.length pl1 != List.length pl2 then assert false;
+				if List.length pl1 != List.length pl2 then error "Argument count mismatch" p;
 				List.iter2 (fun (n1,_,_) (n2,_,_) ->
 					if n1 <> n2 then error ("Method parameter name '" ^ n2 ^ "' should be '" ^ n1 ^ "'") p;
 				) pl1 pl2;