Optional class-code generation in dependence of functions implemented by generics in haxe - metaprogramming

Assuming I have the following classes in Haxe:
class Pair<U, V> {
public var first:U = null;
public var second:V = null;
public function new(u:U, v:V) {
this.first = u;
this.second = v;
}
}
class HashablePair<U:{ function hashCode():Int; }, V:{ function hashCode():Int; }> {
public var first:U = null;
public var second:V = null;
public function new(u:U, v:V) {
this.first = u;
this.second = v;
}
public function hashCode():Int { // just a sample way ...
var h1:Int = (first == null) ? 0 : first.hashCode();
var h2:Int = (second == null) ? 0 : second.hashCode();
return 3 * h1 + 5 * h2;
}
}
I wondered if it is possible to write a macro that adds the hashCode function to the pair class, if and only if both generics U and V implement the hashCode-function ... and thus make it possible to combine the two classes into a single one via meta-programming.

You can achieve the desired behavior by simply switching to an abstract:
typedef Hashable = { function hashCode():Int; };
abstract HashablePair<U:Hashable,V:Hashable>(Pair<U,V>)from Pair<U,V> {
public function new(u:U, v:V)
this = new Pair(u, v);
public function hashCode():Int { // just a sample way ...
var h1:Int = (this.first == null) ? 0 : this.first.hashCode();
var h2:Int = (this.second == null) ? 0 : this.second.hashCode();
return 3 * h1 + 5 * h2;
}
}
The from Pair<U,V> makes Pair<U,V> casts to HashablePair<U,V> allowed, as long as the necessary constrains on U and V are respected.
For a complete exemple, check out Try Haxe #d76E1.

Related

ES5 how to implement .Net IEqualityComaparer on data class for comparisons?

When I want to use a custom type T as hash key in .Net, I implement IEqualityComparer and pass it to hash map like Dictionary or HashSet, when adding new item, the GetHashCode and Equals method will be called to check whether two T instance are same.
for example, I have a immutable data class Foo:
sealed class Foo
{
public Foo(int field1, string field2)
{
Prop_1 = field1;
Prop_2 = field2;
}
public int Prop_1 { get; }
public string Prop_2 { get; }
}
and FooEuqalityComparer:
sealed class FooEuqalityComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
return x == null ? y == null :
x.Prop_1 == y.Prop_1 &&
x.Prop_2 == y.Prop_2;
}
public int GetHashCode(Foo obj)
{
if (obj == null)
return 0;
return obj.Prop_1.GetHashCode() ^ obj.Prop_2.GetHashCode();
}
}
test:
var set = new HashSet<Foo>(new FooEuqalityComparer());
var foo1 = new Foo(1, "foo 1");
var not_foo2 = new Foo(1, "foo 1");
var foo3 = new Foo(3, "foo 3");
set.Add(foo1);
set.Add(not_foo2);
Assert.AreEqual(1, set.Count);
Assert.AreSame(foo1, set.Single());
set.Add(foo3);
Assert.AreEqual(2, set.Count);
How can I do it in nodejs?
Overwrite toString() is not a option because I want to keep reference to that object as key inside map.
After some search, I realized that javascript or ECMAScript use SameValueZero algorithm to compare objects, the best way still is using string as key.
so I use two map to achieve this:
class ObjectKeyMap {
/**
* #param {Object[]} keys -
* #param {function():string} keys[].getHashCode -
* #param {function(Object):T} valueSelector -
*
* #typedef {Object} T
*/
constructor(keys, valueSelector) {
const keyReferences = {};
keys.forEach(it => {
keyReferences[it.getHashCode()] = it;
});
this.keyReferences = keyReferences;
this.map = new Map(keys.map(it => [it.getHashCode(), valueSelector(it)]));
}
/**
* #param {string|{getHashCode:function():string}} key -
*
* #returns {string}
*/
_getStringKey(key) {
if (!key) {
return null;
}
if (Object.prototype.toString.call(key) === "[object String]") {
return key;
} else {
return key.getHashCode();
}
}
/**
* #param {string|{getHashCode:function():string}} key -
*
* #returns {T}
*/
get(key) {
const stringKey = this._getStringKey(key);
if (!stringKey || stringKey === "") {
return null;
}
return this.map.get(stringKey);
}
values() {
return [...this.map.values()];
}
/**
* #param {string|{getHashCode:function():string}} key -
*/
key(key) {
const stringKey = this._getStringKey(key);
if (!stringKey || stringKey === "") {
return null;
}
return this.keyReferences[stringKey];
}
keys() {
return Object.values(this.keyReferences).slice();
}
}
ObjectKeyMap assumes object to be used as key must have a getHashCode function which return identity string. It should be more readable if written in TypeScript.

Using functions as map keys in Haxe

I want to use functions as keys in a Map like this:
var timers : Map<Void->Void, snow.api.Timer>;
But Haxe won't compile:
Abstract Map has no #:to function that accepts IMap<Void -> Void, snow.api.Timer>
Is there a way to do this ?
It's easy to write a custom implementation:
import haxe.Constraints;
class FunctionMap<K:Function,V> implements IMap<K,V> {
private var _keys : Array<K>;
private var _values : Array<V>;
public function new () {
_keys = [];
_values = [];
}
public function get(k:K):Null<V> {
var keyIndex = index(k);
if (keyIndex < 0) {
return null;
} else {
return _values[keyIndex];
}
}
public function set(k:K, v:V):Void {
var keyIndex = index(k);
if (keyIndex < 0) {
_keys.push(k);
_values.push(v);
} else {
_values[keyIndex] = v;
}
}
public function exists(k:K):Bool {
return index(k) >= 0;
}
public function remove(k:K):Bool {
var keyIndex = index(k);
if (keyIndex < 0) {
return false;
} else {
_keys.splice(keyIndex, 1);
_values.splice(keyIndex, 1);
return true;
}
}
public function keys():Iterator<K> {
return _keys.iterator();
}
public function iterator():Iterator<V> {
return _values
.iterator();
}
public function toString():String {
var s = new StringBuf();
s.add("{");
for( i in 0..._keys.length ) {
s.add('<function>');
s.add(" => ");
s.add(Std.string(_values[i]));
if( i < _keys.length - 1 )
s.add(", ");
}
s.add("}");
return s.toString();
}
private function index(key:K) : Int {
for (i in 0..._keys.length) {
if (Reflect.compareMethods(key, _keys[i])) {
return i;
}
}
return -1;
}}
http://try.haxe.org/#DdF31
I just tried this in try.haxe.org, and the compiler doesn't seem to like it, so I'm guessing the answer is "no."
You could get around this with some cleverness:
class Test {
static function main() {
var map:Map<VoidVoid,String>;
map = new Map<VoidVoid,String>();
var thing = {func:foo};
map.set(thing,"bar");
trace(map.get({func:foo})); //fails
trace(map.get(thing)); //succeeds;
}
static function foo():Void
{
}
}
typedef VoidVoid = {
var func:Void->Void;
}
But that's not an ideal solution because wrapping it in a typedef like that will make it fail if it's not the exact same instance, even if the value inside is the same.
I also tried making a Map<Dynamic,String> since you can stuff function references in those, but that didn't work either.
At this point I should ask, what problem are you trying to solve this way? Perhaps it could be better solved some other way.

How to print all object fields dynamicly even if on of the fields is object

I want want to print all the fields in object and if one of the field is object i want to print it fields and on and on (with recursion).
I wrote function but i get this error once i've entering to recursion.
Field '_str1' defined on type 'ConsoleApplication1.StringTest' is not
a field on the target object which is of type
'System.Reflection.RtFieldInfo'.
This is the Code:
static void OutPutObject(dynamic dyn,Type pType)
{
List<string> lFieldsList = new List<string>();
// Get the type of MyClass.
Type lType = pType;
// Get the FieldInfo of MyClass.
FieldInfo[] myFields = lType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
// Display the values of the fields.
for (int i = 0; i < myFields.Length; i++)
{
if (myFields[i].FieldType.IsClass && myFields[i].FieldType.Name != "String")
{
Type tType = myFields[i].FieldType.GetType();
OutPutObject(myFields[i], pType);
}
else
{
Console.WriteLine(string.Format("{0}: {1}", myFields[i].Name, myFields[i].GetValue(dyn)));
}
}
}
public class IntTest
{
private int a = 1;
private int b = 2;
}
public class StringTest
{
private string _str1;
private string _str2;
private IntTest intl;
public StringTest()
{
_str1 = "str1";
_str2 = "str2";
}
}
I've change your code and now it works!
Please notice that the inner classes must have default constructor.
static void OutPutObject(object obj, Type pType, bool isFirst)
{
// Get the FieldInfo of MyClass.
FieldInfo[] myFields = pType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
// Display the values of the fields.
for (int i = 0; i < myFields.Length; i++)
{
if (myFields[i].FieldType.IsPrimitive || myFields[i].FieldType == typeof(Decimal) || myFields[i].FieldType == typeof(String))
{
if (!isFirst)
{
object temp = Activator.CreateInstance(pType);
obj = temp;
}
Console.WriteLine(string.Format("{0}: {1}", myFields[i].Name, myFields[i].GetValue(obj)));
}
else if (myFields[i].FieldType.IsClass)
{
Type tType = myFields[i].FieldType;
OutPutObject(myFields[i], tType, false);
}
}
}
You can use following lines to write object public fields. I tested on .NET 4.0
Type type = obj.GetType();
foreach (var f in type.GetFields().Where(f => f.IsPublic)) {
Console.WriteLine("Name: "+f.Name+" Value: "+ f.GetValue(obj) );
}

getting list of broken constraints for final best solution in drools-planner

I'm using drools-planner-5.4.0.CR1 and I wanna get list of broken constraints for final best solution
and also looked of examples of 5.4.0.CR1
I've implemented like in example but it returns empty list, in other words getScoreDetailList().size() is equal to 0, but solution.getScore() is equal to -54.
is there any suggestions?
public class SomeClass {
...
private volatile Solver solver;
private ScoreDirector scoreDirector;
...
public void someMethod() {
SolverFactory solverFactory = new XmlSolverFactory(SOLVER_CONFIG);
solver = solverFactory.buildSolver();
scoreDirector = solver.getScoreDirectorFactory().buildScoreDirector();
...
this.scoreDirector.setWorkingSolution(solution);
this.solver.setPlanningProblem(this.scoreDirector.getWorkingSolution());
this.solver.solve();
SomeSolution solution = (SomeSolution) solver.getBestSolution();
this.scoreDirector.setWorkingSolution(solution);
System.out.println( "Score: " + solution.getScore());
System.out.println( "getScoreDetailList size:" + getScoreDetailList().size());
}
public List<ScoreDetail> getScoreDetailList() {
if (!(scoreDirector instanceof DroolsScoreDirector)) {
return null;
}
Map<String, ScoreDetail> scoreDetailMap = new HashMap<String, ScoreDetail>();
WorkingMemory workingMemory = ((DroolsScoreDirector) scoreDirector).getWorkingMemory();
if (workingMemory == null) {
return Collections.emptyList();
}
Iterator<ConstraintOccurrence> it = (Iterator<ConstraintOccurrence>) workingMemory.iterateObjects(
new ClassObjectFilter(ConstraintOccurrence.class));
while (it.hasNext()) {
ConstraintOccurrence constraintOccurrence = it.next();
ScoreDetail scoreDetail = scoreDetailMap.get(constraintOccurrence.getRuleId());
if (scoreDetail == null) {
scoreDetail = new ScoreDetail(constraintOccurrence.getRuleId(), constraintOccurrence.getConstraintType());
scoreDetailMap.put(constraintOccurrence.getRuleId(), scoreDetail);
}
scoreDetail.addConstraintOccurrence(constraintOccurrence);
}
List<ScoreDetail> scoreDetailList = new ArrayList<ScoreDetail>(scoreDetailMap.values());
Collections.sort(scoreDetailList);
return scoreDetailList;
}
}
After
this.scoreDirector.setWorkingSolution(solution);
you forgot to call
this.scoreDirector.calculateScore();
I 'll docs about using Solver.getScoreDirectorFactory() in 5.4.0.Final.

Easy way to search a string for strings

I'm trying to find the easiest way to search a string for an array of possible strings. I know the easy way to do this for characters is to use myString.IndexOfAny(charArray). But how what if I'd like to search my string for strings and not just characters? Are there any .net tricks or methods that make this easier?
Basically, I'd like to do something like this:
string myName = "rahkim";
string[] names = new string[] {"joe","bob","chris"};
if(myName.IndexOfAny(names) >= 0)
{
//success code//
}
I know there are ways to do this with loops, etc. But I was hoping for something inherent in the framework.
You should define if you want to to find equal strings or search for a matching substring. Both ways are easy pre-LINQ and with LINQ.
string myName = "rahkim";
string[] names = new string[] { "joe", "bob", "chris" };
Equal Strings, LINQ
bool contains = names.Contains(myName);
Equal Strings, Pre-LINQ
bool contains = new List<string>(name).Contains(myName);
Substrings, LINQ
bool contains = names.Any(name => name.Contains(myName));
Substring, Pre-LINQ
bool contains = false;
foreach(string name in names)
if (name.Contains(myName))
contains = true;
If anyone else found this while trying to search for a .Net method like String.IndexOfAny(String[]), this is my solution:
C#
public int IndexOfAny(string test, string[] values)
{
int first = -1;
foreach (string item in values) {
int i = test.IndexOf(item);
if (i >= 0) {
if (first > 0) {
if (i < first) {
first = i;
}
} else {
first = i;
}
}
}
return first;
}
VB
Public Function IndexOfAny(test As String, values As String()) As Integer
Dim first As Integer = -1
For Each item As String In values
Dim i As Integer = test.IndexOf(item)
If i >= 0 Then
If first > 0 Then
If i < first Then
first = i
End If
Else
first = i
End If
End If
Next
Return first
End Function
You can do a LastIndexOfAny(String[]) by just switching the
i < first
to
i > first
You can (also) use the static IndexOf method of the Array class:
bool hasName = Array.IndexOf(names, myName) > -1;
int IndexOfAny(String[] rgs) would indeed be nice but it's nominally an O(n^2) operation. If, in your application, the set of strings rgs is large and always the same, the most efficient approach is to load them into a trie data structure once, and then use the trie repeatedly to search for them within the unknown strings given at runtime.
Here is the relevant code, adapted from a C# trie source I found on the web, attributed to "Kerry D. Wong." In my version, each string in the trie has a "payload" of generic type TValue. To use this trie to simply search for substrings, the payload could always be set to true, as illustrated with simple_trie.
The other thing I changed here is that this trie automatically adapts allow for storage of arbitrary Unicode strings. The array at each node—which characterizes a trie—adjusts its base and length to accomodate the range of Unicode characters which need to be stored at that node. This allows for case-sensitive matching, for example.
The C# 3.0 initialization syntax is handy for this trie, but enabling it requires a dummy implementation of IEnumerable in order to compile. The CLR doesn't seem to call GetEnumerator() and I suggest that you don't try to enumerate with its result either.
using System;
using System.Collections.Generic;
using System.Linq; // only used in Main()
class Program
{
// trie with payload of type <String>
static Trie<String> value_trie = new Trie<String>
{
{ "rabbit", "cute" },
{ "giraffe", "tall" },
{ "ape", "smart" },
{ "hippo", "large" },
};
// degenerate case of a trie without payload
static Trie<bool> simple_trie = new Trie<bool>
{
{ "rabbit", true },
{ "giraffe", true },
{ "ape", true },
{ "hippo", true },
};
static void Main(String[] args)
{
String s = "Once upon a time, a rabbit met an ape in the woods.";
// Retrieve payloads for words in the string.
//
// output:
// cute
// smart
foreach (String word in value_trie.AllSubstringValues(s))
Console.WriteLine(word);
// Simply test a string for any of the words in the trie.
// Note that the Any() operator ensures that the input is no longer
// traversed once a single result is found.
//
// output:
// True
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e=>e));
s = "Four score and seven years ago.";
// output:
// False
Console.WriteLine(simple_trie.AllSubstringValues(s).Any(e => e));
}
}
class TrieNode<TValue>
{
private TrieNode<TValue>[] nodes = null;
private TValue m_value = default(TValue);
private Char m_base;
public Char Base { get { return m_base; } }
public bool IsEnd { get { return !m_value.Equals(default(TValue)); } }
public TValue Value
{
get { return m_value; }
set { m_value = value; }
}
public IEnumerable<TrieNode<TValue>> Nodes { get { return nodes; } }
public TrieNode<TValue> this[char c]
{
get
{
if (nodes != null && m_base <= c && c < m_base + nodes.Length)
return nodes[c - m_base];
return null;
}
}
public TrieNode<TValue> AddChild(char c)
{
if (nodes == null)
{
m_base = c;
nodes = new TrieNode<TValue>[1];
}
else if (c >= m_base + nodes.Length)
{
Array.Resize(ref nodes, c - m_base + 1);
}
else if (c < m_base)
{
Char c_new = (Char)(m_base - c);
TrieNode<TValue>[] tmp = new TrieNode<TValue>[nodes.Length + c_new];
nodes.CopyTo(tmp, c_new);
m_base = c;
nodes = tmp;
}
TrieNode<TValue> node = nodes[c - m_base];
if (node == null)
{
node = new TrieNode<TValue>();
nodes[c - m_base] = node;
}
return node;
}
};
class Trie<TValue> : System.Collections.IEnumerable
{
private TrieNode<TValue> _root = new TrieNode<TValue>();
// This dummy enables C# 3.0 initialization syntax
public System.Collections.IEnumerator GetEnumerator()
{
return null;
}
public void Add(String s, TValue v)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
node = node.AddChild(c);
node.Value = v;
}
public bool Contains(String s)
{
TrieNode<TValue> node = _root;
foreach (Char c in s)
{
node = node[c];
if (node == null)
return false;
}
return node.IsEnd;
}
public TValue Find(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
return default(TValue);
}
return node.Value;
}
public IEnumerable<TValue> FindAll(String s_in)
{
TrieNode<TValue> node = _root;
foreach (Char c in s_in)
{
node = node[c];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
}
}
public IEnumerable<TValue> AllSubstringValues(String s)
{
int i_cur = 0;
while (i_cur < s.Length)
{
TrieNode<TValue> node = _root;
int i = i_cur;
while (i < s.Length)
{
node = node[s[i]];
if (node == null)
break;
if (node.Value != null)
yield return node.Value;
i++;
}
i_cur++;
}
}
};
Here's the right syntax:
if(names.Contains(myName))
{
//success code//
}
if (names.Contains(myName))
{
//success code//
}

Resources