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 #10290

Closed
wants to merge 23 commits into from
Closed
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 @@ -916,6 +916,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 @@ -1546,6 +1552,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 @@ -1605,6 +1615,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 @@ -1663,6 +1677,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 @@ -3155,6 +3173,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 @@ -3167,6 +3186,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 @@ -3179,6 +3199,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 @@ -3450,6 +3471,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 @@ -57,6 +57,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 @@ -72,6 +73,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
7 changes: 7 additions & 0 deletions std/cpp/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
last = null;
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;
Expand Down
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 @@ -105,6 +105,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
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 @@ -105,6 +105,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 @@ -105,6 +105,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 @@ -98,4 +98,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);
}
}
6 changes: 6 additions & 0 deletions std/cs/_std/EReg.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ import cs.system.text.regularexpressions.*;
public function matchedPos():{pos:Int, len:Int} {
return {pos: m.Index, len: m.Length};
}

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

public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
m = if (len < 0) regex.Match(s, pos) else regex.Match(s, pos, len);
Expand Down
24 changes: 14 additions & 10 deletions std/cs/_std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import cs.NativeArray;
private var vals:NativeArray<T>;

private var nBuckets:Int;
private var size:Int;
private var _size:Int;
private var nOccupied:Int;
private var upperBound:Int;

Expand All @@ -57,7 +57,7 @@ import cs.NativeArray;
public function set(key:Int, value:T):Void {
var targetIndex:Int;
if (nOccupied >= upperBound) {
if (nBuckets > (size << 1)) {
if (nBuckets > (_size << 1)) {
resize(nBuckets - 1); // clear "deleted" elements
} else {
resize(nBuckets + 1);
Expand Down Expand Up @@ -99,13 +99,13 @@ import cs.NativeArray;
_keys[targetIndex] = key;
vals[targetIndex] = value;
setIsBothFalse(flags, targetIndex);
size++;
_size++;
nOccupied++;
} else if (isDel(flag)) {
_keys[targetIndex] = key;
vals[targetIndex] = value;
setIsBothFalse(flags, targetIndex);
size++;
_size++;
} else {
#if debug
assert(_keys[targetIndex] == key);
Expand Down Expand Up @@ -223,7 +223,7 @@ import cs.NativeArray;
#end
if (!isEither(getFlag(flags, idx))) {
setIsDelTrue(flags, idx);
--size;
--_size;

vals[idx] = null;
// we do NOT reset the keys here, as unlike StringMap, we check for keys equality
Expand All @@ -245,10 +245,10 @@ import cs.NativeArray;
newNBuckets = roundUp(newNBuckets);
if (newNBuckets < 4)
newNBuckets = 4;
if (size >= (newNBuckets * HASH_UPPER + 0.5))
/* requested size is too small */ {
if (_size >= (newNBuckets * HASH_UPPER + 0.5))
/* requested _size is too small */ {
j = 0;
} else { /* hash table size to be changed (shrink or expand); rehash */
} else { /* hash table _size to be changed (shrink or expand); rehash */
var nfSize = flagsSize(newNBuckets);
newFlags = new NativeArray(nfSize);
for (i in 0...nfSize) {
Expand Down Expand Up @@ -338,7 +338,7 @@ import cs.NativeArray;

this.flags = newFlags;
this.nBuckets = newNBuckets;
this.nOccupied = size;
this.nOccupied = _size;
this.upperBound = Std.int(newNBuckets * HASH_UPPER + .5);
}
}
Expand Down Expand Up @@ -382,14 +382,18 @@ import cs.NativeArray;
_keys = null;
vals = null;
nBuckets = 0;
size = 0;
_size = 0;
nOccupied = 0;
upperBound = 0;
#if !no_map_cache
cachedKey = 0;
cachedIndex = -1;
#end
}

public inline function size():Int {
return _size;
}

private static inline function assert(x:Bool):Void {
#if debug
Expand Down
22 changes: 13 additions & 9 deletions std/cs/_std/haxe/ds/ObjectMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import cs.NativeArray;
public function set(key:K, value:V):Void {
var x:Int, k:Int;
if (nOccupied >= upperBound) {
if (nBuckets > (size << 1))
if (nBuckets > (_size << 1))
resize(nBuckets - 1); // clear "deleted" elements
else
resize(nBuckets + 2);
Expand Down Expand Up @@ -117,13 +117,13 @@ import cs.NativeArray;
keys[x] = key;
vals[x] = value;
hashes[x] = k;
size++;
_size++;
nOccupied++;
} else if (isDel(flag)) {
keys[x] = key;
vals[x] = value;
hashes[x] = k;
size++;
_size++;
} else {
assert(_keys[x] == key);
vals[x] = value;
Expand Down Expand Up @@ -171,10 +171,10 @@ import cs.NativeArray;
newNBuckets = roundUp(newNBuckets);
if (newNBuckets < 4)
newNBuckets = 4;
if (size >= (newNBuckets * HASH_UPPER + 0.5))
/* requested size is too small */ {
if (_size >= (newNBuckets * HASH_UPPER + 0.5))
/* requested _size is too small */ {
j = 0;
} else { /* hash table size to be changed (shrink or expand); rehash */
} else { /* hash table _size to be changed (shrink or expand); rehash */
var nfSize = newNBuckets;
newHash = new NativeArray(nfSize);
if (nBuckets < newNBuckets) // expand
Expand Down Expand Up @@ -263,7 +263,7 @@ import cs.NativeArray;

this.hashes = newHash;
this.nBuckets = newNBuckets;
this.nOccupied = size;
this.nOccupied = _size;
this.upperBound = Std.int(newNBuckets * HASH_UPPER + .5);
}
}
Expand Down Expand Up @@ -351,7 +351,7 @@ import cs.NativeArray;
hashes[idx] = FLAG_DEL;
_keys[idx] = null;
vals[idx] = null;
--size;
--_size;

return true;
}
Expand Down Expand Up @@ -396,7 +396,7 @@ import cs.NativeArray;
_keys = null;
vals = null;
nBuckets = 0;
size = 0;
_size = 0;
nOccupied = 0;
upperBound = 0;
#if !no_map_cache
Expand All @@ -410,6 +410,10 @@ import cs.NativeArray;
maxProbe = 0;
#end
}

public inline function size():Int {
return _size;
}

extern private static inline function roundUp(x:Int):Int {
--x;
Expand Down
Loading