<commit_before>package com.github.vlsi.mat.optiq.functions;

import com.github.vlsi.mat.optiq.HeapReference;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.*;

public class HeapFunctions {
    public static int get_id(Object r) {
        HeapReference ref = ensureHeapReference(r);
        return ref == null ? -1 : ref.getIObject().getObjectId();
    }

    public static String get_type(Object r) {
        HeapReference ref = ensureHeapReference(r);
        return ref == null ? "" : ref.getIObject().getClazz().getName();
    }

    public static String toString(Object r) {
        if (r == null) return null;
        return r.toString();
    }

    public static Object get_by_key(Object r, String key) {
        HeapReference ref = ensureHeapReference(r);
        if (ref == null) {
            return null;
        }
        IObject iObject = ref.getIObject();
        ISnapshot snapshot = iObject.getSnapshot();
        String className = iObject.getClazz().getName();

        if (!"java.util.HashMap".equals(className))
        {
            throw new RuntimeException("Unsupported map type: "+className);
        }

        try
        {
            IObjectArray table = (IObjectArray)iObject.resolveValue("table");
            long[] referenceArray = table.getReferenceArray();
            for (long entryAddress : referenceArray)
            {
                if (entryAddress != 0)
                {
                    int entryId = snapshot.mapAddressToId(entryAddress);
                    IObject entry = snapshot.getObject(entryId);
                    while (entry != null)
                    {
                        IObject keyObject = (IObject) entry.resolveValue("key");
                        if (key.equals(toString(keyObject)))
                        {
                            IObject valueObject = (IObject) entry.resolveValue(("value"));
                            return HeapReference.valueOf(valueObject);
                        }

                        entry = (IObject)entry.resolveValue("next");
                    }
                }
            }
        } catch (SnapshotException e)
        {
            throw new RuntimeException("Unable to lookup key " + key + " in " + r, e);
        }

        return null;
    }

    private static HeapReference ensureHeapReference(Object r) {
        return (r == null || !(r instanceof HeapReference)) ?
                null :
                (HeapReference) r;
    }

    private static String toString(IObject o) {
        String classSpecific = o.getClassSpecificName();
        if (classSpecific != null)
            return classSpecific;
        return o.getDisplayName();
    }
}<commit_msg>Added length() function to query length of referenced array<commit_after>package com.github.vlsi.mat.optiq.functions;

import com.github.vlsi.mat.optiq.HeapReference;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.model.*;

public class HeapFunctions {
    public static int get_id(Object r) {
        HeapReference ref = ensureHeapReference(r);
        return ref == null ? -1 : ref.getIObject().getObjectId();
    }

    public static String get_type(Object r) {
        HeapReference ref = ensureHeapReference(r);
        return ref == null ? "" : ref.getIObject().getClazz().getName();
    }

    public static String toString(Object r) {
        if (r == null) return null;
        return r.toString();
    }

    public static Object get_by_key(Object r, String key) {
        HeapReference ref = ensureHeapReference(r);
        if (ref == null) {
            return null;
        }
        IObject iObject = ref.getIObject();
        ISnapshot snapshot = iObject.getSnapshot();
        String className = iObject.getClazz().getName();

        if (!"java.util.HashMap".equals(className))
        {
            throw new RuntimeException("Unsupported map type: "+className);
        }

        try
        {
            IObjectArray table = (IObjectArray)iObject.resolveValue("table");
            long[] referenceArray = table.getReferenceArray();
            for (long entryAddress : referenceArray)
            {
                if (entryAddress != 0)
                {
                    int entryId = snapshot.mapAddressToId(entryAddress);
                    IObject entry = snapshot.getObject(entryId);
                    while (entry != null)
                    {
                        IObject keyObject = (IObject) entry.resolveValue("key");
                        if (key.equals(toString(keyObject)))
                        {
                            IObject valueObject = (IObject) entry.resolveValue(("value"));
                            return HeapReference.valueOf(valueObject);
                        }

                        entry = (IObject)entry.resolveValue("next");
                    }
                }
            }
        } catch (SnapshotException e)
        {
            throw new RuntimeException("Unable to lookup key " + key + " in " + r, e);
        }

        return null;
    }

    public int length(Object r) {
        HeapReference ref = ensureHeapReference(r);

        if (ref == null) {
            return -1;
        }

        IObject obj = ref.getIObject();

        return obj instanceof IArray ? ((IArray)obj).getLength() : -1;
    }

    private static HeapReference ensureHeapReference(Object r) {
        return (r == null || !(r instanceof HeapReference)) ?
                null :
                (HeapReference) r;
    }

    private static String toString(IObject o) {
        String classSpecific = o.getClassSpecificName();
        if (classSpecific != null)
            return classSpecific;
        return o.getDisplayName();
    }
}
{'fraction_non_alphanumeric': 0.10227467077133573, 'fraction_numerical': 0.001026167265264238, 'mean_word_length': 1.778147268408551, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567702', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1490, 'n_words': 355}