Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to get Map and EReg sizes #11961

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions src/macro/eval/evalStdLib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,12 @@ module StdEReg = struct
vfalse
end
)

let matchedNum = vifun0 (fun vthis ->
let this = this vthis in
let substrings = if Array.length this.r_groups = 0 then exc_string "Invalid regex operation because no match was made" else this.r_groups.(0) in
vint (num_of_subs substrings)
)

let replace = vifun2 (fun vthis s by ->
let this = this vthis in
Expand Down Expand Up @@ -1554,6 +1560,10 @@ module StdIntMap = struct
IntHashtbl.clear (this vthis);
vnull
)

let size = vifun0 (fun vthis ->
vint (IntHashtbl.size (this vthis))
)
end

module StdStringMap = struct
Expand Down Expand Up @@ -1613,6 +1623,10 @@ module StdStringMap = struct
StringHashtbl.clear (this vthis);
vnull
)

let size = vifun0 (fun vthis ->
vint (StringHashtbl.size (this vthis))
)
end

module StdObjectMap = struct
Expand Down Expand Up @@ -1671,6 +1685,10 @@ module StdObjectMap = struct
ValueHashtbl.reset (this vthis);
vnull
)

let size = vifun0 (fun vthis ->
vint (ValueHashtbl.length (this vthis))
)
end

let random = Random.State.make_self_init()
Expand Down Expand Up @@ -3199,6 +3217,7 @@ let init_maps builtins =
"set",StdIntMap.set;
"toString",StdIntMap.toString;
"clear",StdIntMap.clear;
"size",StdIntMap.size;
];
init_fields builtins (["haxe";"ds"],"ObjectMap") [] [
"copy",StdObjectMap.copy;
Expand All @@ -3211,6 +3230,7 @@ let init_maps builtins =
"set",StdObjectMap.set;
"toString",StdObjectMap.toString;
"clear",StdObjectMap.clear;
"size",StdObjectMap.size;
];
init_fields builtins (["haxe";"ds"],"StringMap") [] [
"copy",StdStringMap.copy;
Expand All @@ -3223,6 +3243,7 @@ let init_maps builtins =
"set",StdStringMap.set;
"toString",StdStringMap.toString;
"clear",StdStringMap.clear;
"size",StdStringMap.size;
]

let init_constructors builtins =
Expand Down Expand Up @@ -3494,6 +3515,7 @@ let init_standard_library builtins =
"matchedPos",StdEReg.matchedPos;
"matchedRight",StdEReg.matchedRight;
"matchSub",StdEReg.matchSub;
"matchedNum",StdEReg.matchedNum;
"replace",StdEReg.replace;
"split",StdEReg.split;
];
Expand Down
2 changes: 2 additions & 0 deletions src/macro/eval/evalValue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module StringHashtbl = struct
let mem this key = StringMap.mem key.sstring !this
let remove this key = this := StringMap.remove key.sstring !this
let clear this = this := StringMap.empty
let size this = StringMap.cardinal !this
end

module IntHashtbl = struct
Expand All @@ -71,6 +72,7 @@ module IntHashtbl = struct
let mem this key = Hashtbl.mem this key
let remove this key = Hashtbl.remove this key
let clear this = Hashtbl.clear this
let size this = Hashtbl.length this
end

type vregex = {
Expand Down
12 changes: 12 additions & 0 deletions std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ class EReg {
public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
return false;
}

/**
Returns the total number of groups captures by the last matched substring.

To stay consistent with `this.matched`, the matched substring is also
counted as a group.

If no substring has been matched, an error is thrown.
**/
public function matchedNum():Int {
return 0;
}

/**
Splits String `s` at all substrings `this` EReg matches.
Expand Down
4 changes: 4 additions & 0 deletions std/cpp/Int64Map.hx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ import haxe.Int64;
#end
}

public function size():Int {
return untyped __global__.__root_hash_size(h);
}

#if (scriptable)
private function setString(key:Int64, val:String):Void {
untyped __int64_hash_set_string(__cpp__("HX_MAP_THIS"), key, val);
Expand Down
10 changes: 10 additions & 0 deletions std/cpp/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
return p;
}

public function matchedNum():Int {
var num = _hx_regexp_matched_num(r);
if (num == -1)
throw "No string matched!";
return num;
}

public function split(s:String):Array<String> {
var pos = 0;
var len = s.length;
Expand Down Expand Up @@ -190,4 +197,7 @@

@:native("_hx_regexp_matched_pos")
extern static function _hx_regexp_matched_pos(handle:Dynamic, match:Int):{pos:Int, len:Int};

@:native("_hx_regexp_matched_num")
extern static function _hx_regexp_matched_num(handle:Dynamic):Int;
}
4 changes: 4 additions & 0 deletions std/cpp/_std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ package haxe.ds;
h = null;
#end
}

public function size():Int {
return untyped __global__.__root_hash_size(h);
}

#if (scriptable)
private function setString(key:Int, val:String):Void {
Expand Down
7 changes: 7 additions & 0 deletions std/cpp/_std/haxe/ds/Map.hx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ abstract Map<K, V>(IMap<K, V>) {
this.clear();
}

/**
Returns size of `this` Map.
**/
public inline function size():Int {
return this.size();
}

@:arrayAccess @:noCompletion public inline function arrayWrite(k:K, v:V):V {
this.set(k, v);
return v;
Expand Down
4 changes: 4 additions & 0 deletions std/cpp/_std/haxe/ds/ObjectMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
h = null;
#end
}

public function size():Int {
return untyped __global__.__root_hash_size(h);
}

#if (scriptable)
private function setString(key:Dynamic, val:String):Void {
Expand Down
4 changes: 4 additions & 0 deletions std/cpp/_std/haxe/ds/StringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ package haxe.ds;
h = null;
#end
}

public function size():Int {
return untyped __global__.__root_hash_size(h);
}

#if (scriptable)
private function setString(key:String, val:String):Void {
Expand Down
4 changes: 4 additions & 0 deletions std/cpp/_std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ class WeakMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
h = null;
#end
}

public function size():Int {
return untyped __global__.__root_hash_size(h);
}
}
1 change: 1 addition & 0 deletions std/eval/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ extern class EReg {
function matchedRight():String;
function matchedPos():{pos:Int, len:Int};
function matchSub(s:String, pos:Int, len:Int = -1):Bool;
function matchedNum():Int;
function split(s:String):Array<String>;
function replace(s:String, by:String):String;
function map(s:String, f:EReg->String):String;
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
throw "No string matched";
return {pos: result.index, len: (result[0] : String).length};
}

public function matchedNum():Int {
if (result == null)
throw "No string matched";
return result.length;
}

public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
return if (r.global) {
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ package haxe.ds;
public inline function clear():Void {
h = new flash.utils.Dictionary();
}

public function size():Int {
var s = 0;
for(_ in keys()) s++;
return s;
}
}

// this version uses __has_next__/__forin__ special SWF opcodes for iteration with no allocation
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/haxe/ds/ObjectMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class ObjectMap<K:{}, V> extends flash.utils.Dictionary implements haxe.Constrai
for (i in keys())
untyped __delete__(this, i);
}

public function size():Int {
var s = 0;
for(_ in keys()) s++;
return s;
}
}

private class NativePropertyIterator {
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/haxe/ds/StringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ package haxe.ds;
h = {};
rh = null;
}

public function size():Int {
var s = 0;
for(_ in keys()) s++;
return s;
}
}

// this version uses __has_next__/__forin__ special SWF opcodes for iteration with no allocation
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/haxe/ds/UnsafeStringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class UnsafeStringMap<T> implements haxe.Constraints.IMap<String, T> {
public inline function clear():Void {
h = new flash.utils.Dictionary();
}

public function size():Int {
var s = 0;
for(_ in keys()) s++;
return s;
}
}

// this version uses __has_next__/__forin__ special SWF opcodes for iteration with no allocation
Expand Down
6 changes: 6 additions & 0 deletions std/flash/_std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class WeakMap<K:{}, V> extends flash.utils.Dictionary implements haxe.Constraint
for (i in keys())
untyped __delete__(this, i);
}

public function size():Int {
var s = 0;
for(_ in keys()) s++;
return s;
}
}

private class NativePropertyIterator {
Expand Down
1 change: 1 addition & 0 deletions std/haxe/Constraints.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ abstract NotVoid(Dynamic) { }
abstract Constructible<T>(Dynamic) {}

interface IMap<K, V> {
function size():Int;
function get(k:K):Null<V>;
function set(k:K, v:V):Void;
function exists(k:K):Bool;
Expand Down
12 changes: 12 additions & 0 deletions std/haxe/ds/BalancedTree.hx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ class BalancedTree<K, V> implements haxe.Constraints.IMap<K, V> {
}
}

static function sizeLoop<K,V>(node:TreeNode<K, V>):Int {
if (node != null) {
return sizeLoop(node.left) + 1 + sizeLoop(node.right);
} else {
return 0;
}
}

function merge(t1, t2) {
if (t1 == null)
return t2;
Expand Down Expand Up @@ -236,6 +244,10 @@ class BalancedTree<K, V> implements haxe.Constraints.IMap<K, V> {
public function clear():Void {
root = null;
}

public function size():Int {
return sizeLoop(root);
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions std/haxe/ds/HashMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ abstract HashMap<K:{function hashCode():Int;}, V>(HashMapData<K, V>) {
this.keys.clear();
this.values.clear();
}

/**
See `Map.size`
**/
public inline function size():Int {
return this.keys.size();
}
}

private class HashMapData<K:{function hashCode():Int;}, V> {
Expand Down
2 changes: 2 additions & 0 deletions std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ extern class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
See `Map.clear`
**/
function clear():Void;

function size():Int;
}
7 changes: 7 additions & 0 deletions std/haxe/ds/Map.hx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ abstract Map<K, V>(IMap<K, V>) {
this.clear();
}

/**
Returns size of `this` Map.
**/
public inline function size():Int {
return this.size();
}

@:arrayAccess @:noCompletion public inline function arrayWrite(k:K, v:V):V {
this.set(k, v);
return v;
Expand Down
2 changes: 2 additions & 0 deletions std/haxe/ds/ObjectMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ extern class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
See `Map.clear`
**/
function clear():Void;

public function size():Int;
}
2 changes: 2 additions & 0 deletions std/haxe/ds/StringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ extern class StringMap<T> implements haxe.Constraints.IMap<String, T> {
See `Map.clear`
**/
function clear():Void;

public function size():Int;
}
7 changes: 7 additions & 0 deletions std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ class WeakMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
See `Map.clear`
**/
public function clear():Void {}

/**
See `Map.size`
**/
public function size():Int {
return 0;
}
}
Loading
Loading