فهرست منبع

[python] move python.lib.Set to python.Set, cleanup and add missing methods, use native "set" instead of importing from builtins

Dan Korostelev 10 سال پیش
والد
کامیت
9ae0a2c7d9
5فایلهای تغییر یافته به همراه80 افزوده شده و 62 حذف شده
  1. 1 1
      genpy.ml
  2. 0 1
      std/python/Boot.hx
  3. 78 0
      std/python/Set.hx
  4. 0 59
      std/python/lib/Set.hx
  5. 1 1
      tests/unit/src/unit/TestPython.hx

+ 1 - 1
genpy.ml

@@ -80,7 +80,7 @@ module KeywordHandler = struct
 		List.iter (fun s -> Hashtbl.add h s ()) [
 			"len"; "int"; "float"; "list"; "bool"; "str"; "isinstance"; "print"; "min"; "max";
 			"hasattr"; "getattr"; "setattr"; "callable"; "type"; "ord"; "chr"; "iter"; "map"; "filter";
-			"tuple"; "dict";
+			"tuple"; "dict"; "set";
 		];
 		h
 

+ 0 - 1
std/python/Boot.hx

@@ -30,7 +30,6 @@ import python.internal.HxException;
 import python.internal.AnonObject;
 import python.internal.UBuiltins;
 import python.lib.Inspect;
-import python.lib.Set;
 
 import python.Syntax;
 

+ 78 - 0
std/python/Set.hx

@@ -0,0 +1,78 @@
+/*
+ * Copyright (C)2005-2015 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this 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
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package python;
+
+import python.internal.UBuiltins;
+import python.NativeIterator;
+import python.NativeIterable;
+import python.Syntax;
+
+@:native("set")
+extern class Set<T> {
+	@:overload(function(?array:Array<T>):Void {})
+	function new(?iterable:NativeIterable<T>):Void;
+
+	var length(get,never):Int;
+	private inline function get_length():Int {
+		return UBuiltins.len(this);
+	}
+
+	inline function has(v:T):Bool {
+		return Syntax.isIn(v, this);
+	}
+
+	function isdisjoint(other:Set<T>):Bool;
+	function issubset(other:Set<T>):Bool;
+	inline function issubset_proper(other:Set<T>):Bool {
+		return Syntax.binop(this, "<", other);
+	}
+	function issuperset(other:Set<T>):Bool;
+	inline function issuperset_proper(other:Set<T>):Bool {
+		return Syntax.binop(this, ">", other);
+	}
+	function union(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function intersection(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function difference(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function symmetric_difference(other:Set<T>):Set<T>;
+	function copy():Set<T>;
+
+	function update(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function intersection_update(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function difference_update(other:Set<T>, others:haxe.Rest<Set<T>>):Set<T>;
+	function symmetric_difference_update(other:Set<T>):Set<T>;
+
+	function add(elem:T):Void;
+	function remove(elem:T):Void;
+	function discard(elem:T):Void;
+	function pop():T;
+	function clear():Void;
+
+	inline function iter():NativeIterator<T> {
+		return UBuiltins.iter(this);
+	}
+
+	inline function iterator():Iterator<T> {
+		return iter();
+	}
+
+	private function __iter__():NativeIterator<T>;
+}

+ 0 - 59
std/python/lib/Set.hx

@@ -1,59 +0,0 @@
-/*
- * Copyright (C)2005-2012 Haxe Foundation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this 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
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-package python.lib;
-
-import python.NativeIterator;
-import python.NativeIterable;
-
-@:pythonImport("builtins", "set")
-extern class Set <T>
-{
-	@:overload(function (?array:Array<T>):Void {})
-	public function new (?iterable:NativeIterable<T>):Void;
-
-	public inline function length ():Int
-	{
-		return python.internal.UBuiltins.len(this);
-	}
-
-	public inline function has (v:T):Bool
-	{
-		return python.Syntax.isIn(v, this);
-	}
-
-
-	public inline function minus (other:Set<T>):Set<T>
-	{
-		return python.Syntax.binop(this, "-", other);
-	}
-	public inline function plus (other:Set<T>):Set<T>
-	{
-		return python.Syntax.binop(this, "+", other);
-	}
-
-	function __iter__ ():NativeIterator<T>;
-
-	public inline function iterator ():NativeIterator<T>
-	{
-		return __iter__();
-	}
-}

+ 1 - 1
tests/unit/src/unit/TestPython.hx

@@ -22,7 +22,6 @@ import python.lib.Os;
 import python.lib.Pprint;
 import python.lib.Random;
 import python.lib.Re;
-import python.lib.Set;
 import python.lib.Shutil;
 import python.lib.Subprocess;
 import python.lib.Sys;
@@ -33,6 +32,7 @@ import python.lib.Time;
 import python.lib.Traceback;
 import python.lib.Tty;
 import python.Tuple;
+import python.Set;
 
 import python.lib.datetime.Datetime;
 import python.lib.datetime.Timedelta;