text:
// Original file from https://github.com/FasterXML/jackson-core under Apache-2.0 license.
package com.azure.json.implementation.jackson.core.sym;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

import com.azure.json.implementation.jackson.core.JsonFactory;
import com.azure.json.implementation.jackson.core.util.InternCache;

/**
 * Replacement for BytesToNameCanonicalizer which aims at more localized
 * memory access due to flattening of name quad data.
 * Performance improvement modest for simple JSON document data binding (maybe 3%),
 * but should help more for larger symbol tables, or for binary formats like Smile.
 *

 * Hash area is divided into 4 sections:
 *

 *
Primary area (1/2 of total size), direct match from hash (LSB)

 *
Secondary area (1/4 of total size), match from {@code hash (LSB) >> 1}

 *
Tertiary area (1/8 of total size), match from {@code hash (LSB) >> 2}

 *
Spill-over area (remaining 1/8) with linear scan, insertion order

 *

 *

 * and within every area, entries are 4 {@code int}s, where 1 - 3 ints contain 1 - 12
 * UTF-8 encoded bytes of name (null-padded), and last int is offset in
 * {@code _names} that contains actual name Strings.
 *
 * @since 2.6
 */
@SuppressWarnings("fallthrough")
public final class ByteQuadsCanonicalizer
{
    /**
     * Initial size of the primary hash area. Each entry consumes 4 ints (16 bytes),
     * and secondary area is same as primary; so default size will use 2kB of memory
     * (plus 64x4 or 64x8 (256/512 bytes) for references to Strings, and Strings
     * themselves).
     */
    private static final int DEFAULT_T_SIZE = 64;
//    private static final int DEFAULT_T_SIZE = 256;

    /**
     * Let's not expand symbol tables past some maximum size;
     * with unique (~= random) names.
     * Size is in
     */
    private static final int MAX_T_SIZE = 0x10000; // 64k entries == 2M mem hash area

    /**
     * No point in trying to construct tiny tables, just need to resize soon.
     */
    private final static int MIN_HASH_SIZE = 16;

    /**
     * Let's only share reasonably sized symbol tables. Max size set to 3/4 of 8k;
     * this corresponds to 256k main hash index. This should allow for enough distinct
     * names for almost any case, while preventing ballooning for cases where names
     * are unique (or close thereof).
     */
    protected final static int MAX_ENTRIES_FOR_REUSE = 6000;

    /*
    /**********************************************************
    /* Linkage, needed for merging symbol tables
    /**********************************************************
     */

    /**
     * Reference to the root symbol table, for child tables, so
     * that they can merge table information back as necessary.
     */
    protected final ByteQuadsCanonicalizer _parent;

    /**
     * Member that is only used by the root table instance: root
     * passes immutable state info child instances, and children
     * may return new state if they add entries to the table.
     * Child tables do NOT use the reference.
     */
    protected final AtomicReference<TableInfo> _tableInfo;

    /**
     * Seed value we use as the base to make hash codes non-static between
     * different runs, but still stable for lifetime of a single symbol table
     * instance.
     * This is done for security reasons, to avoid potential DoS attack via
     * hash collisions.
     */
    protected final int _seed;

    /*
    /**********************************************************
    /* Configuration
    /**********************************************************
     */

    /**
     * Whether canonical symbol Strings are to be intern()ed before added
     * to the table or not.
     *

     * NOTE: non-final to allow disabling intern()ing in case of excessive
     * collisions.
     */
    protected final boolean _intern;

    /**
     * Flag that indicates whether we should throw an exception if enough
     * hash collisions are detected (true); or just worked around (false).
     *
     * @since 2.4
     */
    protected final boolean _failOnDoS;

    /*
    /**********************************************************
    /* First, main hash area info
    /**********************************************************
     */

    /**
     * Primary hash information area: consists of 2 * _hashSize
     * entries of 16 bytes (4 ints), arranged in a cascading lookup
     * structure (details of which may be tweaked depending on expected rates
     * of collisions).
     */
    protected int[] _hashArea;

    /**
     * Number of slots for primary entries within {@link #_hashArea}; which is
     * at most 1/8 of actual size of the underlying array (4-int slots,
     * primary covers only half of the area; plus, additional area for longer
     * symbols after hash area).
     */
    protected int _hashSize;

    /**
     * Offset within {@link #_hashArea} where secondary entries start
     */
    protected int _secondaryStart;

    /**
     * Offset within {@link #_hashArea} where tertiary entries start
     */
    protected int _tertiaryStart;

    /**
     * Constant that determines size of buckets for tertiary entries:
     * 1 << _tertiaryShift is the size, and shift value
     * is also used for translating from primary offset into
     * tertiary bucket (shift right by 4 + _tertiaryShift).
     *

     * Default value is 2, for buckets of 4 slots; grows bigger with
     * bigger table sizes.
     */
    protected int _tertiaryShift;

    /**
     * Total number of Strings in the symbol table; only used for child tables.
     */
    protected int _count;

    /**
     * Array that contains String instances matching
     * entries in {@link #_hashArea}.
     * Contains nulls for unused entries. Note that this size is twice
     * that of {@link #_hashArea}
     */
    protected String[] _names;

    /*
    /**********************************************************
    /* Then information on collisions etc
    /**********************************************************
     */

    /**
     * Pointer to the offset within spill-over area where there is room
     * for more spilled over entries (if any).
     * Spill over area is within fixed-size portion of {@link #_hashArea}.
     */
    protected int _spilloverEnd;

    /**
     * Offset within {@link #_hashArea} that follows main slots and contains
     * quads for longer names (13 bytes or longer), and points to the
     * first available int that may be used for appending quads of the next
     * long name.
     * Note that long name area follows immediately after the fixed-size
     * main hash area ({@link #_hashArea}).
     */
    protected int _longNameOffset;

    /*
    /**********************************************************
    /* Sharing, versioning
    /**********************************************************
     */

    // // // Which of the buffers may be shared (and are copy-on-write)?

    /**
     * Flag that indicates whether underlying data structures for
     * the main hash area are shared or not. If they are, then they
     * need to be handled in copy-on-write way, i.e. if they need
     * to be modified, a copy needs to be made first; at this point
     * it will not be shared any more, and can be modified.
     *

     * This flag needs to be checked both when adding new main entries,
     * and when adding new collision list queues (i.e. creating a new
     * collision list head entry)
     */
    protected boolean _hashShared;

    /*
    /**********************************************************
    /* Life-cycle: constructors
    /**********************************************************
     */

    /**
     * Constructor used for creating per-{@code TokeanStreamFactory} "root"
     * symbol tables: ones used for merging and sharing common symbols
     *
     * @param sz Initial primary hash area size
     * @param seed Random seed valued used to make it more difficult to cause
     *   collisions (used for collision-based DoS attacks).
     */
    private ByteQuadsCanonicalizer(int sz, int seed)
    {
        // Settings to distinguish parent table: no parent
        _parent = null;
        _count = 0;

        // and mark as shared just in case to prevent modifications
        _hashShared = true;
        _seed = seed;
        _intern = false;
        _failOnDoS = true;
        // Sanity check: let's now allow hash sizes below certain minimum value
        if (sz < MIN_HASH_SIZE) {
            sz = MIN_HASH_SIZE;
        } else {
            // Also; size must be 2^N; otherwise hash algorithm won't
            // work... so let's just pad it up, if so
            if ((sz & (sz - 1)) != 0) { // only true if it's 2^N
                int curr = MIN_HASH_SIZE;
                while (curr < sz) {
                    curr += curr;
                }
                sz = curr;
            }
        }
        _tableInfo = new AtomicReference<TableInfo>(TableInfo.createInitial(sz));
    }

    /**
     * Constructor used when creating a child instance
     */
    private ByteQuadsCanonicalizer(ByteQuadsCanonicalizer parent, int seed,
            TableInfo state,
            boolean intern, boolean failOnDoS)
    {
        _parent = parent;
        _seed = seed;
        _intern = intern;
        _failOnDoS = failOnDoS;
        _tableInfo = null; // not used by child tables

        // Then copy shared state
        _count = state.count;
        _hashSize = state.size;
        _secondaryStart = _hashSize << 2; // right after primary area
        _tertiaryStart = _secondaryStart + (_secondaryStart >> 1); // right after secondary
        _tertiaryShift = state.tertiaryShift;

        _hashArea = state.mainHash;
        _names = state.names;

        _spilloverEnd = state.spilloverEnd;
        _longNameOffset = state.longNameOffset;

        // and then set other state to reflect sharing status
        _hashShared = true;
    }

    /**
     * Alternate constructor used in cases where a "placeholder" child
     * instance is needed when symbol table is not really used, but
     * caller needs a non-null placeholder to keep code functioning
     * with minimal awareness of distinction (all lookups fail to match
     * any name without error; add methods should NOT be called).
     *
     * @since 2.13
     */
    private ByteQuadsCanonicalizer(TableInfo state)
    {
        _parent = null;
        _seed = 0;
        _intern = false;
        _failOnDoS = true;
        _tableInfo = null; // not used by child tables

        // Then copy minimal pieces of shared state; only enough to guarantee
        // we will neither find anything nor fail -- primary hash is enough
        // for that purpose

        _count = -1;

        _hashArea = state.mainHash;
        _names = state.names;

        _hashSize = state.size;

        // But otherwise can just use markers towards end of table to
        // indicate error if access was attempted
        final int end = _hashArea.length;
        _secondaryStart = end;
        _tertiaryStart = end;
        _tertiaryShift = 1; //  bogus

        _spilloverEnd = end;
        _longNameOffset = end;

        // just in case something failed, to ensure copying would be done
        _hashShared = true;
    }

    /*
    /**********************************************************
    /* Life-cycle: factory methods, merging
    /**********************************************************
     */

    /**
     * Factory method to call to create a symbol table instance with a
     * randomized seed value.
     *
     * @return Root instance to use for constructing new child instances
     */
    public static ByteQuadsCanonicalizer createRoot() {
        // Need to use a variable seed, to thwart hash-collision based attacks.
        // 14-Feb-2017, tatu: Does this actually help?
        long now = System.currentTimeMillis();
        // ensure it's not 0; and might as well require to be odd so:
        int seed = (((int) now) + ((int) (now >>> 32))) | 1;
        return createRoot(seed);
    }

    // Factory method that should only be called from unit tests, where seed
    // value should remain the same.
    protected static ByteQuadsCanonicalizer createRoot(int seed) {
        return new ByteQuadsCanonicalizer(DEFAULT_T_SIZE, seed);
    }

    /**
     * Factory method used to create actual symbol table instance to
     * use for parsing.
     *
     * @param flags Bit flags of active {@link com.azure.json.implementation.jackson.core.JsonFactory.Feature}s enabled.
     *
     * @return Actual canonicalizer instance that can be used by a parser
     */
    public ByteQuadsCanonicalizer makeChild(int flags) {
        return new ByteQuadsCanonicalizer(this, _seed,
                _tableInfo.get(),
                JsonFactory.Feature.INTERN_FIELD_NAMES.enabledIn(flags),
                JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW.enabledIn(flags));
    }

    /**
     * Method similar to {@link #makeChild} but one that only creates real
     * instance of {@link com.azure.json.implementation.jackson.core.JsonFactory.Feature#CANONICALIZE_FIELD_NAMES} is
     * enabled: otherwise a "bogus" instance is created.
     *
     * @param flags Bit flags of active {@link com.azure.json.implementation.jackson.core.JsonFactory.Feature}s enabled.
     *
     * @return Actual canonicalizer instance that can be used by a parser if (and only if)
     *    canonicalization is enabled; otherwise a non-null "placeholder" instance.
     *
     * @since 2.13
     */
    public ByteQuadsCanonicalizer makeChildOrPlaceholder(int flags) {
        if (JsonFactory.Feature.CANONICALIZE_FIELD_NAMES.enabledIn(flags)) {
            // inlined "makeChild()"
            return new ByteQuadsCanonicalizer(this, _seed,
                    _tableInfo.get(),
                    JsonFactory.Feature.INTERN_FIELD_NAMES.enabledIn(flags),
                    JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW.enabledIn(flags));
        }
        return new ByteQuadsCanonicalizer(_tableInfo.get());
    }

    /**
     * Method called by the using code to indicate it is done with this instance.
     * This lets instance merge accumulated changes into parent (if need be),
     * safely and efficiently, and without calling code having to know about parent
     * information.
     */
    public void release()
    {
        // we will try to merge if child table has new entries
        // 28-Jul-2019, tatu: From [core#548]: do not share if immediate rehash needed
        if ((_parent != null) && maybeDirty()) {
            _parent.mergeChild(new TableInfo(this));
            // Let's also mark this instance as dirty, so that just in
            // case release was too early, there's no corruption of possibly shared data.
            _hashShared = true;
        }
    }

    private void mergeChild(TableInfo childState)
    {
        final int childCount = childState.count;
        TableInfo currState = _tableInfo.get();

        // Should usually grow; but occasionally could also shrink if (but only if)
        // collision list overflow ends up clearing some collision lists.
        if (childCount == currState.count) {
            return;
        }

        // One caveat: let's try to avoid problems with degenerate cases of documents with
        // generated "random" names: for these, symbol tables would bloat indefinitely.
        // One way to do this is to just purge tables if they grow
        // too large, and that's what we'll do here.
        if (childCount > MAX_ENTRIES_FOR_REUSE) {
            // At any rate, need to clean up the tables
            childState = TableInfo.createInitial(DEFAULT_T_SIZE);
        }
        _tableInfo.compareAndSet(currState, childState);
    }

    /*
    /**********************************************************
    /* API, accessors
    /**********************************************************
     */

    /**
     * @return Number of symbol entries contained by this canonicalizer instance
     */
    public int size()
    {
        if (_tableInfo != null) { // root table
            return _tableInfo.get().count;
        }
        // nope, child table
        return _count;
    }

    /**
     * @return number of primary slots table has currently
     */
    public int bucketCount() { return _hashSize; }

    /**
     * Method called to check to quickly see if a child symbol table
     * may have gotten additional entries. Used for checking to see
     * if a child table should be merged into shared table.
     *
     * @return Whether main hash area has been modified
     */
    public boolean maybeDirty() { return !_hashShared; }

    public int hashSeed() { return _seed; }

    /**
     * @return True for "real", canonicalizing child tables; false for
     *    root table as well as placeholder "child" tables.
     *
     * @since 2.13
     */
    public boolean isCanonicalizing() {
        // couple of options, but for now missing parent linkage simplest:
        return _parent != null;
    }

    /**
     * Method mostly needed by unit tests; calculates number of
     * entries that are in the primary slot set. These are
     * "perfect" entries, accessible with a single lookup
     *
     * @return Number of entries in the primary hash area
     */
    public int primaryCount()
    {
        int count = 0;
        for (int offset = 3, end = _secondaryStart; offset < end; offset += 4) {
            if (_hashArea[offset] != 0) {
                ++count;
            }
        }
        return count;
    }

    /**
     * Method mostly needed by unit tests; calculates number of entries
     * in secondary buckets
     *
     * @return Number of entries in the secondary hash area
     */
    public int secondaryCount() {
        int count = 0;
        int offset = _secondaryStart + 3;
        for (int end = _tertiaryStart; offset < end; offset += 4) {
            if (_hashArea[offset] != 0) {
                ++count;
            }
        }
        return count;
    }

    /**
     * Method mostly needed by unit tests; calculates number of entries
     * in tertiary buckets
     *
     * @return Number of entries in the tertiary hash area
     */
    public int tertiaryCount() {
        int count = 0;
        int offset = _tertiaryStart + 3; // to 1.5x, starting point of tertiary
        for (int end = offset + _hashSize; offset < end; offset += 4) {
            if (_hashArea[offset] != 0) {
                ++count;
            }
        }
        return count;
    }

    /**
     * Method mostly needed by unit tests; calculates number of entries
     * in shared spill-over area
     *
     * @return Number of entries in the linear spill-over areay
     */
    public int spilloverCount() {
        // difference between spillover end, start, divided by 4 (four ints per slot)
        return (_spilloverEnd - _spilloverStart()) >> 2;
    }

    public int totalCount()
    {
        int count = 0;
        for (int offset = 3, end = (_hashSize << 3); offset < end; offset += 4) {
            if (_hashArea[offset] != 0) {
                ++count;
            }
        }
        return count;
    }

    @Override
    public String toString() {
        int pri = primaryCount();
        int sec = secondaryCount();
        int tert = tertiaryCount();
        int spill = spilloverCount();
        int total = totalCount();
        return String.format("[%s: size=%d, hashSize=%d, %d/%d/%d/%d pri/sec/ter/spill (=%s), total:%d]",
                getClass().getName(), _count, _hashSize,
                pri, sec, tert, spill, (pri+sec+tert+spill), total);
    }

    /*
    /**********************************************************
    /* Public API, accessing symbols
    /**********************************************************
     */

    public String findName(int q1)
    {
        int offset = _calcOffset(calcHash(q1));
        // first: primary match?
        final int[] hashArea = _hashArea;

        int len = hashArea[offset+3];

        if (len == 1) {
            if (hashArea[offset] == q1) {
                return _names[offset >> 2];
            }
        } else if (len == 0) { // empty slot; unlikely but avoid further lookups if so
            return null;
        }
        // secondary? single slot shared by N/2 primaries
        int offset2 = _secondaryStart + ((offset >> 3) << 2);

        len = hashArea[offset2+3];

        if (len == 1) {
            if (hashArea[offset2] == q1) {
                return _names[offset2 >> 2];
            }
        } else if (len == 0) { // empty slot; unlikely but avoid further lookups if so
            return null;
        }

        // tertiary lookup & spillovers best to offline
        return _findSecondary(offset, q1);
    }

    public String findName(int q1, int q2)
    {
        int offset = _calcOffset(calcHash(q1, q2));

        final int[] hashArea = _hashArea;

        int len = hashArea[offset+3];

        if (len == 2) {
            if ((q1 == hashArea[offset]) && (q2 == hashArea[offset+1])) {
                return _names[offset >> 2];
            }
        } else if (len == 0) { // empty slot; unlikely but avoid further lookups if so
            return null;
        }
        // secondary?
        int offset2 = _secondaryStart + ((offset >> 3) << 2);

        len = hashArea[offset2+3];

        if (len == 2) {
            if ((q1 == hashArea[offset2]) && (q2 == hashArea[offset2+1])) {
                return _names[offset2 >> 2];
            }
        } else if (len == 0) { // empty slot? Short-circuit if no more spillovers
            return null;
        }
        return _findSecondary(offset, q1, q2);
    }

    public String findName(int q1, int q2, int q3)
    {
        int offset = _calcOffset(calcHash(q1, q2, q3));
        final int[] hashArea = _hashArea;
        int len = hashArea[offset+3];

        if (len == 3) {
            if ((q1 == hashArea[offset]) && (hashArea[offset+1] == q2) && (hashArea[offset+2] == q3)) {
                return _names[offset >> 2];
            }
        } else if (len == 0) { // empty slot; unlikely but avoid further lookups if so
            return null;
        }
        // secondary?
        int offset2 = _secondaryStart + ((offset >> 3) << 2);

        len = hashArea[offset2+3];

        if (len == 3) {
            if ((q1 == hashArea[offset2]) && (hashArea[offset2+1] == q2) && (hashArea[offset2+2] == q3)) {
                return _names[offset2 >> 2];
            }
        } else if (len == 0) { // empty slot? Short-circuit if no more spillovers
            return null;
        }
        return _findSecondary(offset, q1, q2, q3);
    }

    public String findName(int[] q, int qlen)
    {
        /* This version differs significantly, because longer names do not fit within cell.
         * Rather, they contain hash in main slot, and offset+length to extension area
         * that contains actual quads.
         */
        if (qlen < 4) { // another sanity check
            switch (qlen) {
            case 3:
                return findName(q[0], q[1], q[2]);
            case 2:
                return findName(q[0], q[1]);
            case 1:
                return findName(q[0]);
            default: // if 0 ever passed
                return "";
            }
        }
        final int hash = calcHash(q, qlen);
        int offset = _calcOffset(hash);

        final int[] hashArea = _hashArea;

        final int len = hashArea[offset+3];

        if ((hash == hashArea[offset]) && (len == qlen)) {
            // probable but not guaranteed: verify
            if (_verifyLongName(q, qlen, hashArea[offset+1])) {
                return _names[offset >> 2];
            }
        }
        if (len == 0) { // empty slot; unlikely but avoid further lookups if so
            return null;
        }
        // secondary?
        int offset2 = _secondaryStart + ((offset >> 3) << 2);

        final int len2 = hashArea[offset2+3];
        if ((hash == hashArea[offset2]) && (len2 == qlen)) {
            if (_verifyLongName(q, qlen, hashArea[offset2+1])) {
                return _names[offset2 >> 2];
            }
        }
        return _findSecondary(offset, hash, q, qlen);
    }

    private final int _calcOffset(int hash)
    {
        // NOTE: simple for initial impl, but we may want to interleave it a bit
        // in near future
        // So: first, hash into primary hash index
        int ix = hash & (_hashSize-1);
        // keeping in mind we have 4 ints per entry
        return (ix << 2);
    }

    /*
    /**********************************************************
    /* Access from spill-over areas
    /**********************************************************
     */

    private String _findSecondary(int origOffset, int q1)
    {
        // tertiary area division is dynamic. First; its size is N/4 compared to
        // primary hash size; and offsets are for 4 int slots. So to get to logical
        // index would shift by 4. But! Tertiary area is further split into buckets,
        // determined by shift value. And finally, from bucket back into physical offsets
        int offset = _tertiaryStart + ((origOffset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int[] hashArea = _hashArea;
        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset + bucketSize; offset < end; offset += 4) {
            int len = hashArea[offset+3];
            if ((q1 == hashArea[offset]) && (1 == len)) {
                return _names[offset >> 2];
            }
            if (len == 0) {
                return null;
            }
        }
        // but if tertiary full, check out spill-over area as last resort
        // shared spillover starts at 7/8 of the main hash area
        // (which is sized at 2 * _hashSize), so:
        for (offset = _spilloverStart(); offset < _spilloverEnd; offset += 4) {
            if ((q1 == hashArea[offset]) && (1 == hashArea[offset+3])) {
                return _names[offset >> 2];
            }
        }
        return null;
    }

    private String _findSecondary(int origOffset, int q1, int q2)
    {
        int offset = _tertiaryStart + ((origOffset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int[] hashArea = _hashArea;

        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset + bucketSize; offset < end; offset += 4) {
            int len = hashArea[offset+3];
            if ((q1 == hashArea[offset]) && (q2 == hashArea[offset+1]) && (2 == len)) {
                return _names[offset >> 2];
            }
            if (len == 0) {
                return null;
            }
        }
        for (offset = _spilloverStart(); offset < _spilloverEnd; offset += 4) {
            if ((q1 == hashArea[offset]) && (q2 == hashArea[offset+1]) && (2 == hashArea[offset+3])) {
                return _names[offset >> 2];
            }
        }
        return null;
    }

    private String _findSecondary(int origOffset, int q1, int q2, int q3)
    {
        int offset = _tertiaryStart + ((origOffset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int[] hashArea = _hashArea;

        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset + bucketSize; offset < end; offset += 4) {
            int len = hashArea[offset+3];
            if ((q1 == hashArea[offset]) && (q2 == hashArea[offset+1]) && (q3 == hashArea[offset+2]) && (3 == len)) {
                return _names[offset >> 2];
            }
            if (len == 0) {
                return null;
            }
        }
        for (offset = _spilloverStart(); offset < _spilloverEnd; offset += 4) {
            if ((q1 == hashArea[offset]) && (q2 == hashArea[offset+1]) && (q3 == hashArea[offset+2])
                    && (3 == hashArea[offset+3])) {
                return _names[offset >> 2];
            }
        }
        return null;
    }

    private String _findSecondary(int origOffset, int hash, int[] q, int qlen)
    {
        int offset = _tertiaryStart + ((origOffset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int[] hashArea = _hashArea;

        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset + bucketSize; offset < end; offset += 4) {
            int len = hashArea[offset+3];
            if ((hash == hashArea[offset]) && (qlen == len)) {
                if (_verifyLongName(q, qlen, hashArea[offset+1])) {
                    return _names[offset >> 2];
                }
            }
            if (len == 0) {
                return null;
            }
        }
        for (offset = _spilloverStart(); offset < _spilloverEnd; offset += 4) {
            if ((hash == hashArea[offset]) && (qlen == hashArea[offset+3])) {
                if (_verifyLongName(q, qlen, hashArea[offset+1])) {
                    return _names[offset >> 2];
                }
            }
        }
        return null;
    }

    private boolean _verifyLongName(int[] q, int qlen, int spillOffset)
    {
        final int[] hashArea = _hashArea;
        // spillOffset assumed to be physical index right into quad string
        int ix = 0;

        switch (qlen) {
        default:
            return _verifyLongName2(q, qlen, spillOffset);
        case 8:
            if (q[ix++] != hashArea[spillOffset++]) return false;
        case 7:
            if (q[ix++] != hashArea[spillOffset++]) return false;
        case 6:
            if (q[ix++] != hashArea[spillOffset++]) return false;
        case 5:
            if (q[ix++] != hashArea[spillOffset++]) return false;
        case 4: // always at least 4
            if (q[ix++] != hashArea[spillOffset++]) return false;
            if (q[ix++] != hashArea[spillOffset++]) return false;
            if (q[ix++] != hashArea[spillOffset++]) return false;
            if (q[ix++] != hashArea[spillOffset++]) return false;
        }
        return true;
    }

    private boolean _verifyLongName2(int[] q, int qlen, int spillOffset)
    {
        int ix = 0;
        do {
            if (q[ix++] != _hashArea[spillOffset++]) {
                return false;
            }
        } while (ix < qlen);
        return true;
    }

    /*
    /**********************************************************
    /* API, mutators
    /**********************************************************
     */

    public String addName(String name, int q1) {
        _verifySharing();
        if (_intern) {
            name = InternCache.instance.intern(name);
        }
        int offset = _findOffsetForAdd(calcHash(q1));
        _hashArea[offset] = q1;
        _hashArea[offset+3] = 1;
        _names[offset >> 2] = name;
        ++_count;
        return name;
    }

    public String addName(String name, int q1, int q2) {
        _verifySharing();
        if (_intern) {
            name = InternCache.instance.intern(name);
        }

        // 20-Mar-2021, tatu: For some reason, pre-2.13 there was logic
        //    to support "only one quad" case. Not sure why, does not make sense;
        //    removed from 2.13.0.
//        int hash = (q2 == 0) ? calcHash(q1) : calcHash(q1, q2);
        final int hash = calcHash(q1, q2);
        final int offset = _findOffsetForAdd(hash);
        _hashArea[offset] = q1;
        _hashArea[offset+1] = q2;
        _hashArea[offset+3] = 2;
        _names[offset >> 2] = name;
        ++_count;
        return name;
    }

    public String addName(String name, int q1, int q2, int q3) {
        _verifySharing();
        if (_intern) {
            name = InternCache.instance.intern(name);
        }
        int offset = _findOffsetForAdd(calcHash(q1, q2, q3));
        _hashArea[offset] = q1;
        _hashArea[offset+1] = q2;
        _hashArea[offset+2] = q3;
        _hashArea[offset+3] = 3;
        _names[offset >> 2] = name;
        ++_count;
        return name;
    }

    public String addName(String name, int[] q, int qlen)
    {
        _verifySharing();
        if (_intern) {
            name = InternCache.instance.intern(name);
        }
        int offset;

        switch (qlen) {
        case 1:
        {
                offset = _findOffsetForAdd(calcHash(q[0]));
                _hashArea[offset] = q[0];
                _hashArea[offset+3] = 1;
            }
            break;
        case 2:
            {
                offset = _findOffsetForAdd(calcHash(q[0], q[1]));
                _hashArea[offset] = q[0];
                _hashArea[offset+1] = q[1];
                _hashArea[offset+3] = 2;
            }
            break;
        case 3:
            {
                offset = _findOffsetForAdd(calcHash(q[0], q[1], q[2]));
                _hashArea[offset] = q[0];
                _hashArea[offset+1] = q[1];
                _hashArea[offset+2] = q[2];
                _hashArea[offset+3] = 3;
            }
            break;
        default:
            final int hash = calcHash(q, qlen);
            offset = _findOffsetForAdd(hash);

            _hashArea[offset] = hash;
            int longStart = _appendLongName(q, qlen);
            _hashArea[offset+1] = longStart;
            _hashArea[offset+3] = qlen;
        }
        // plus add the actual String
        _names[offset >> 2] = name;

        // and finally; see if we really should rehash.
        ++_count;
        return name;
    }

    private void _verifySharing()
    {
        if (_hashShared) {
            // 12-Mar-2021, tatu: prevent modifying of "placeholder" and
            //   parent tables
            if (_parent == null) {
                if (_count == 0) { // root
                    throw new IllegalStateException("Cannot add names to Root symbol table");
                }
                throw new IllegalStateException("Cannot add names to Placeholder symbol table");
            }

            _hashArea = Arrays.copyOf(_hashArea, _hashArea.length);
            _names = Arrays.copyOf(_names, _names.length);
            _hashShared = false;
        }
    }

    /**
     * Method called to find the location within hash table to add a new symbol in.
     */
    private int _findOffsetForAdd(int hash)
    {
        // first, check the primary: if slot found, no need for resize
        int offset = _calcOffset(hash);
        final int[] hashArea = _hashArea;
        if (hashArea[offset+3] == 0) {
//System.err.printf(" PRImary slot #%d, hash %X\n", (offset>>2), hash & 0x7F);
            return offset;
        }

        // Otherwise let's see if we are due resize():
        if (_checkNeedForRehash()) {
            return _resizeAndFindOffsetForAdd(hash);
        }

        // If not, proceed with secondary slot
        int offset2 = _secondaryStart + ((offset >> 3) << 2);
        if (hashArea[offset2+3] == 0) {
//System.err.printf(" SECondary slot #%d (start x%X), hash %X\n",(offset >> 3), _secondaryStart, (hash & 0x7F));
            return offset2;
        }
        // if not, tertiary?

        offset2 = _tertiaryStart + ((offset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset2 + bucketSize; offset2 < end; offset2 += 4) {
            if (hashArea[offset2+3] == 0) {
//System.err.printf(" TERtiary slot x%X (from x%X, start x%X), hash %X.\n", offset2, ((offset >> (_tertiaryShift + 2)) << _tertiaryShift), _tertiaryStart, (hash & 0x7F));
                return offset2;
            }
        }

        // and if even tertiary full, append at the end of spill area
        offset = _spilloverEnd;
        _spilloverEnd += 4;

//System.err.printf(" SPIll-over at x%X; start x%X; end x%X, hash %X\n", offset, _spilloverStart(), _hashArea.length, (hash & 0x7F));

        // one caveat: in the unlikely event if spill-over filling up,
        // check if that could be considered a DoS attack; handle appropriately
        // (NOTE: approximate for now; we could verify details if that becomes necessary)
        /* 31-Jul-2015, tatu: Note that spillover area does NOT end at end of array,
         *   since "long names" area follows. Instead, need to calculate from hash size.
         */
        final int end = (_hashSize << 3);
        if (_spilloverEnd >= end) {
            if (_failOnDoS) {
                _reportTooManyCollisions();
            }
            return _resizeAndFindOffsetForAdd(hash);
        }
        return offset;
    }

    // @since 2.10
    private int _resizeAndFindOffsetForAdd(int hash)
    {
        // First things first: we need to resize+rehash (or, if too big, nuke contents)
        rehash();

        // Copy of main _findOffsetForAdd except for checks to resize: can not be needed
        int offset = _calcOffset(hash);
        final int[] hashArea = _hashArea;
        if (hashArea[offset+3] == 0) {
            return offset;
        }
        int offset2 = _secondaryStart + ((offset >> 3) << 2);
        if (hashArea[offset2+3] == 0) {
            return offset2;
        }
        offset2 = _tertiaryStart + ((offset >> (_tertiaryShift + 2)) << _tertiaryShift);
        final int bucketSize = (1 << _tertiaryShift);
        for (int end = offset2 + bucketSize; offset2 < end; offset2 += 4) {
            if (hashArea[offset2+3] == 0) {
                return offset2;
            }
        }
        offset = _spilloverEnd;
        _spilloverEnd += 4;
        return offset;
    }

    // Helper method for checking if we should simply rehash() before add
    private boolean _checkNeedForRehash() {
        // Yes if above 80%, or above 50% AND have ~1% spill-overs
        if (_count > (_hashSize >> 1)) { // over 50%
            int spillCount = (_spilloverEnd - _spilloverStart()) >> 2;
            if ((spillCount > (1 + _count >> 7))
                    || (_count > (_hashSize * 0.80))) {
                return true;
            }
        }
        return false;
    }

    private int _appendLongName(int[] quads, int qlen)
    {
        int start = _longNameOffset;

        // note: at this point we must already be shared. But may not have enough space
        if ((start + qlen) > _hashArea.length) {
            // try to increment in reasonable chunks; at least space that we need
            int toAdd = (start + qlen) - _hashArea.length;
            // but at least 1/8 of regular hash area size or 16kB (whichever smaller)
            int minAdd = Math.min(4096, _hashSize);

            int newSize = _hashArea.length + Math.max(toAdd, minAdd);
            _hashArea = Arrays.copyOf(_hashArea, newSize);
        }
        System.arraycopy(quads, 0, _hashArea, start, qlen);
        _longNameOffset += qlen;
        return start;
    }

    /*
    /**********************************************************
    /* Hash calculation
    /**********************************************************
     */

    /* Note on hash calculation: we try to make it more difficult to
     * generate collisions automatically; part of this is to avoid
     * simple "multiply-add" algorithm (like JDK String.hashCode()),
     * and add bit of shifting. And other part is to make this
     * non-linear, at least for shorter symbols.
     */

    // JDK uses 31; other fine choices are 33 and 65599, let's use 33
    // as it seems to give fewest collisions for us
    // (see [http://www.cse.yorku.ca/~oz/hash.html] for details)
    private final static int MULT = 33;
    private final static int MULT2 = 65599;
    private final static int MULT3 = 31;

    public int calcHash(int q1)
    {
        int hash = q1 ^ _seed;
        /* 29-Mar-2015, tatu: Earlier used 15 + 9 right shifts, which worked ok
         *    except for one specific problem case: numbers. So needed to make sure
         *    that all 4 least-significant bits participate in hash. Couple of ways
         *    to work it out, but this is the simplest, fast and seems to do ok.
         */
        hash += (hash >>> 16); // to xor hi- and low- 16-bits
        hash ^= (hash << 3); // shuffle back a bit
        hash += (hash >>> 12); // and bit more
        return hash;
    }

    public int calcHash(int q1, int q2)
    {
        // For two quads, let's change algorithm a bit, to spice
        // things up (can do bit more processing anyway)
        int hash = q1;

        hash += (hash >>> 15); // try mixing first and second byte pairs first
        hash ^= (hash >>> 9); // as well as lowest 2 bytes
        hash += (q2 * MULT); // then add second quad
        hash ^= _seed;
        hash += (hash >>> 16); // and shuffle some more
        hash ^= (hash >>> 4);
        hash += (hash << 3);

        return hash;
    }

    public int calcHash(int q1, int q2, int q3)
    { // use same algorithm as multi-byte, tested to work well
        int hash = q1 ^ _seed;
        hash += (hash >>> 9);
        hash *= MULT3;
        hash += q2;
        hash *= MULT;
        hash += (hash >>> 15);
        hash ^= q3;
        // 26-Mar-2015, tatu: As per two-quad case, a short shift seems to help more here
        hash += (hash >>> 4);

        hash += (hash >>> 15);
        hash ^= (hash << 9);

        return hash;
    }

    public int calcHash(int[] q, int qlen)
    {
        if (qlen < 4) {
            throw new IllegalArgumentException();
        }
        /* And then change handling again for "multi-quad" case; mostly
         * to make calculation of collisions less fun. For example,
         * add seed bit later in the game, and switch plus/xor around,
         * use different shift lengths.
         */
        int hash = q[0] ^ _seed;
        hash += (hash >>> 9);
        hash += q[1];
        hash += (hash >>> 15);
        hash *= MULT;
        hash ^= q[2];
        hash += (hash >>> 4);

        for (int i = 3; i < qlen; ++i) {
            int next = q[i];
            next = next ^ (next >> 21);
            hash += next;
        }
        hash *= MULT2;

        // and finally shuffle some more once done
        hash += (hash >>> 19);
        hash ^= (hash << 5);
        return hash;
    }

    /*
    /**********************************************************
    /* Rehashing
    /**********************************************************
     */

    private void rehash()
    {
        // Note: since we'll make copies, no need to unshare, can just mark as such:
        _hashShared = false;

        // And then we can first deal with the main hash area. Since we are expanding
        // linearly (double up), we know there'll be no collisions during this phase.
        final int[] oldHashArea = _hashArea;
        final String[] oldNames = _names;
        final int oldSize = _hashSize;
        final int oldCount = _count;
        final int newSize = oldSize + oldSize;
        final int oldEnd = _spilloverEnd;

        /* 13-Mar-2010, tatu: Let's guard against OOME that could be caused by
         *    large documents with unique (or mostly so) names
         */
        if (newSize > MAX_T_SIZE) {
            nukeSymbols(true);
            return;
        }
        // double up main hash area, but do not expand long-name area:
        _hashArea = new int[oldHashArea.length + (oldSize<<3)];
        _hashSize = newSize;
        _secondaryStart = (newSize << 2); // 4 ints per entry
        _tertiaryStart = _secondaryStart + (_secondaryStart >> 1); // right after secondary
        _tertiaryShift = _calcTertiaryShift(newSize);

        // and simply double up name array
        _names = new String[oldNames.length << 1];
        nukeSymbols(false);

        // Plus we can scan only through the primary hash area, looking for non-empty
        // slots, without worrying about ordering. This should never reduce priority
        // of existing entries: primaries remain primaries; however, due to increased
        // space, secondaries may become primaries etc

        int copyCount = 0;
        int[] q = new int[16];
        for (int offset = 0, end = oldEnd; offset < end; offset += 4) {
            int len = oldHashArea[offset+3];
            if (len == 0) { // empty slot, skip
                continue;
            }
            ++copyCount;
            String name = oldNames[offset>>2];
            switch (len) {
            case 1:
                q[0] = oldHashArea[offset];
                addName(name, q, 1);
                break;
            case 2:
                q[0] = oldHashArea[offset];
                q[1] = oldHashArea[offset+1];
                addName(name, q, 2);
                break;
            case 3:
                q[0] = oldHashArea[offset];
                q[1] = oldHashArea[offset+1];
                q[2] = oldHashArea[offset+2];
                addName(name, q, 3);
                break;
            default:
                if (len > q.length) {
                    q = new int[len];
                }
                // #0 is hash, #1 offset
                int qoff = oldHashArea[offset+1];
                System.arraycopy(oldHashArea, qoff, q, 0, len);
                addName(name, q, len);
                break;
            }
        }

        // Sanity checks: since corruption difficult to detect, assert explicitly
        // with production code
        if (copyCount != oldCount) {
            throw new IllegalStateException("Failed rehash(): old count="+oldCount+", copyCount="+copyCount);
        }
    }

    /**
     * Helper method called to empty all shared symbols, but to leave
     * arrays allocated
     */
    private void nukeSymbols(boolean fill) {
        _count = 0;
        // reset spill-over to empty (starting at 7/8 of hash area)
        _spilloverEnd = _spilloverStart();
        // and long name area to empty, starting immediately after hash area
        _longNameOffset = _hashSize << 3;
        if (fill) {
            Arrays.fill(_hashArea, 0);
            Arrays.fill(_names, null);
        }
    }

    /*
    /**********************************************************
    /* Helper methods
    /**********************************************************
     */

    /**
     * Helper method that calculates start of the spillover area
     */
    private final int _spilloverStart() {
        // we'll need slot at 1.75x of hashSize, but with 4-ints per slot.
        // So basically multiply by 7
        int offset = _hashSize;
        return (offset << 3) - offset;
    }

    protected void _reportTooManyCollisions()
    {
        // First: do not fuzz about small symbol tables; may get balanced by doubling up
        if (_hashSize <= 1024) { // would have spill-over area of 128 entries
            return;
        }
        throw new IllegalStateException("Spill-over slots in symbol table with "+_count
                +" entries, hash area of "+_hashSize+" slots is now full (all "
                +(_hashSize >> 3)+" slots -- suspect a DoS attack based on hash collisions."
                +" You can disable the check via `JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW`");
    }

    static int _calcTertiaryShift(int primarySlots)
    {
        // first: we only get 1/4 of slots of primary, to divide
        int tertSlots = (primarySlots) >> 2;

        // default is for buckets of 4 slots (each 4 ints, i.e. 1 << 4)
        if (tertSlots < 64) {
            return 4;
        }
        if (tertSlots <= 256) { // buckets of 8 slots (up to 256 == 32 x 8)
            return 5;
        }
        if (tertSlots <= 1024) { // buckets of 16 slots (up to 1024 == 64 x 16)
            return 6;
        }
        // and biggest buckets have 32 slots
        return 7;
    }

    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */

    /**
     * Immutable value class used for sharing information as efficiently
     * as possible, by only require synchronization of reference manipulation
     * but not access to contents.
     *
     * @since 2.1
     */
    private final static class TableInfo
    {
        public final int size;
        public final int count;
        public final int tertiaryShift;
        public final int[] mainHash;
        public final String[] names;
        public final int spilloverEnd;
        public final int longNameOffset;

        public TableInfo(int size, int count, int tertiaryShift,
                int[] mainHash, String[] names, int spilloverEnd, int longNameOffset)
        {
            this.size = size;
            this.count = count;
            this.tertiaryShift = tertiaryShift;
            this.mainHash = mainHash;
            this.names = names;
            this.spilloverEnd = spilloverEnd;
            this.longNameOffset = longNameOffset;
        }

        public TableInfo(ByteQuadsCanonicalizer src)
        {
            size = src._hashSize;
            count = src._count;
            tertiaryShift = src._tertiaryShift;
            mainHash = src._hashArea;
            names = src._names;
            spilloverEnd = src._spilloverEnd;
            longNameOffset = src._longNameOffset;
        }

        public static TableInfo createInitial(int sz) {
            int hashAreaSize = sz << 3;
            int tertShift = _calcTertiaryShift(sz);

            return new TableInfo(sz, // hashSize
                    0, // count
                    tertShift,
                    new int[hashAreaSize], // mainHash, 2x slots, 4 ints per slot
                    new String[sz << 1], // names == 2x slots
                    hashAreaSize - sz, // at 7/8 of the total area
                    hashAreaSize // longNameOffset, immediately after main hashes
            );
        }
    }
}


text:
require 'mrg/grid/config'

module Mrg
  module Grid
    module Config
      module Auth
        module Priv
          NONE = 0

          READ = 1
          WRITE_ONLY = 1 << 1
          ACTIVATE = 1 << 2

          WRITE = WRITE_ONLY | READ
          ADMIN = READ | WRITE | ACTIVATE

          def self.to_string(priv)
            @PRIVS ||= Mrg::Grid::Config::Auth::Priv.constants.inject({}) {|acc, val| acc[Mrg::Grid::Config::Auth::Priv.const_get(val)] = val; acc}
            @PRIVS[priv] || "NONE"
          end
        end

        module RoleCache
          def self.populate(fixture=nil)
            @cache = {}
            (fixture || ::Mrg::Grid::Config::Auth::Role.find_all).each do |r|
              @cache[r.username] = r.privs
            end
            @cache
          end

          def self.authorized_to(verb, user)
            user ||= "anonymous"
            priv = ::Mrg::Grid::Config::Auth::Priv.const_get(verb)
            $WALLABY_SKIP_AUTH || cache.empty? || (cache[user].to_i & priv == priv) || (cache["*"].to_i & priv == priv)
          end

          def self.cache
            @cache ||= self.populate
          end
        end

        # Currently authentication is handled by the messaging broker.
        # Role maps from a list of user names to privilege levels.  Note
        # that it is not exposed over the API.  This class manages persistently-
        # stored Role mappings.
        class Role
          include ::Rhubarb::Persisting

          declare_column :username, :string, :not_null
          declare_column :privs, :integer, :not_null
        end

        module ORIZING
          module CM
            def authorize_before(method, action)
              orig_method = "UNCHECKED__#{method}".to_sym
              alias_method orig_method, method
              define_method(method) do |*args|
                unless ::Mrg::Grid::Config::Auth::RoleCache.authorized_to(action, qmf_user_id)
                  fail(::Mrg::Grid::Config::Errors.make(::Mrg::Grid::Config::Errors::UNAUTHORIZED), "'#{qmf_user_id}' does not have #{action} access and cannot invoke #{method}")
                end
                self.send(orig_method, *args)
              end
            end
          end

          module IM
            def authorize_now(action)
              unless ::Mrg::Grid::Config::Auth::RoleCache.authorized_to(action, qmf_user_id)
                fail(::Mrg::Grid::Config::Errors.make(::Mrg::Grid::Config::Errors::UNAUTHORIZED), "'#{qmf_user_id}' does not have #{action} access and cannot invoke #{self.class.name}##{caller[0][/`.*'/][1..-2]}")
              end
            end

            def authorized_via_secret(secret)
              (secret && $WALLABY_SECRET && secret == $WALLABY_SECRET)
            end
          end

          def self.included(receiver)
            receiver.extend CM
            receiver.send(:include, IM)
          end
        end
      end
    end
  end
end

redpajama_set_name: RedPajamaGithub


text:
"""
Old patterns not intended for new code.

These patterns are expected to be deleted eventually.
"""

import param
import numpy as np
from numpy import pi

from param.parameterized import ParamOverrides

from .patterngenerator import Constant, PatternGenerator, Composite
from . import Gaussian
from .image import FileImage, PatternSampler, ImageSampler, edge_average

class SeparatedComposite(Composite):
    """
    Generalized version of the Composite PatternGenerator that
    enforces spacing constraints between pattern centers.

    Currently supports minimum spacing, but can be generalized to
    support maximum spacing also (and both at once).
    """

    min_separation = param.Number(default=0.0, bounds = (0,None),
                            softbounds = (0.0,1.0), doc="""
        Minimum distance to enforce between all pairs of pattern centers.

        Useful for ensuring that multiple randomly generated patterns
        do not overlap spatially.  Note that as this this value is
        increased relative to the area in which locations are chosen,
        the likelihood of a pattern appearing near the center of the
        area will decrease.  As this value approaches the available
        area, the corners become far more likely to be chosen, due to
        the distances being greater along the diagonals.
        """)
        ### JABNOTE: Should provide a mechanism for collecting and
        ### plotting the training pattern center distribution, so that
        ### such issues can be checked.

    max_trials = param.Integer(default = 50, bounds = (0,None),
                         softbounds = (0,100), precedence=-1, doc="""
        Number of times to try for a new pattern location that meets
        the criteria.

        This is an essentially arbitrary timeout value that helps
        prevent an endless loop in case the requirements cannot be
        met.""")

    def __distance_valid(self, g0, g1, p):
        """
        Returns true if the distance between the (x,y) locations of
        two generators g0 and g1 is greater than a minimum separation.

        Can be extended easily to support other criteria.
        """
        dist = np.sqrt((g1.x - g0.x) ** 2 +
                    (g1.y - g0.y) ** 2)
        return dist >= p.min_separation

    def _advance_pattern_generators(self,p):
        """
        Advance the parameters for each generator for this
        presentation.

        Picks a position for each generator that is accepted by
        __distance_valid for all combinations.  Returns a new list of
        the generators, with some potentially omitted due to failure
        to meet the constraints.
        """

        valid_generators = []
        for g in p.generators:

            for trial in range(self.max_trials):
                # Generate a new position and add generator if it's ok

                if np.alltrue([self.__distance_valid(g,v,p) for v in valid_generators]):
                    valid_generators.append(g)
                    break

                g.force_new_dynamic_value('x')
                g.force_new_dynamic_value('y')

            else:
                self.warning("Unable to place pattern %s subject to given constraints" %
                             g.name)

        return valid_generators

#JABALERT: Can't this be replaced with a Composite?
class TwoRectangles(PatternGenerator):
    """Two 2D rectangle pattern generator."""

    aspect_ratio = param.Number(default=1.0,bounds=(0.0,None),softbounds=(0.0,6.0),
        precedence=0.31,doc=
        "Ratio of width to height; size*aspect_ratio gives the width of the rectangle.")

    x1 = param.Number(default=-0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="X center of rectangle 1.")

    y1 = param.Number(default=-0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="Y center of rectangle 1.")

    x2 = param.Number(default=0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="X center of rectangle 2.")

    y2 = param.Number(default=0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="Y center of rectangle 2.")

    size = param.Number(default=0.5)

    # YC: Maybe this can be implemented much more cleanly by calling
    # the parent's function() twice, but it's hard to see how to
    # set the (x,y) offset for the parent.
    def function(self,p):
        height = p.size
        width = p.aspect_ratio*height

        return np.bitwise_or(
               np.bitwise_and(np.bitwise_and(
                        (self.pattern_x-p.x1)<=p.x1+width/4.0,
                        (self.pattern_x-p.x1)>=p.x1-width/4.0),
                      np.bitwise_and(
                        (self.pattern_y-p.y1)<=p.y1+height/4.0,
                        (self.pattern_y-p.y1)>=p.y1-height/4.0)),
               np.bitwise_and(np.bitwise_and(
                        (self.pattern_x-p.x2)<=p.x2+width/4.0,
                        (self.pattern_x-p.x2)>=p.x2-width/4.0),
                      np.bitwise_and(
                        (self.pattern_y-p.y2)<=p.y2+height/4.0,
                        (self.pattern_y-p.y2)>=p.y2-height/4.0)))

### JABALERT: This class should be eliminated if at all possible; it
### is just a specialized version of Composite, and should be
### implementable directly using what is already in Composite.
class GaussiansCorner(PatternGenerator):
    """
    Two Gaussian pattern generators with a variable intersection point,
    appearing as a corner or cross.
    """

    x = param.Number(default=-0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="X center of the corner")

    y = param.Number(default=-0.15,bounds=(-1.0,1.0),softbounds=(-0.5,0.5),
                doc="Y center of the corner")

    size = param.Number(default=0.5,bounds=(0,None), softbounds=(0.1,1),
                doc="The size of the corner")

    aspect_ratio = param.Number(default=1/0.31, bounds=(0,None), softbounds=(1,10),
                doc="Ratio of the width to the height for both Gaussians")

    angle = param.Number(default=0.5*pi,bounds=(0,pi), softbounds=(0.01*pi,0.99*pi),
                doc="The angle of the corner")

    cross = param.Number(default=0.4, bounds=(0,1), softbounds=(0,1),
                doc="Where the two Gaussians cross, as a fraction of their half length")

    def __call__(self,**params_to_override):
        p = ParamOverrides(self,params_to_override)

        g_1 = Gaussian()
        g_2 = Gaussian()

        x_1 = g_1(orientation = p.orientation, bounds = p.bounds, xdensity = p.xdensity,
                            ydensity = p.ydensity, offset = p.offset, size = p.size,
                            aspect_ratio = p.aspect_ratio,
                            x = p.x + 0.7 * np.cos(p.orientation) * p.cross * p.size * p.aspect_ratio,
                            y = p.y + 0.7 * np.sin(p.orientation) * p.cross * p.size * p.aspect_ratio)
        x_2 = g_2(orientation = p.orientation+p.angle, bounds = p.bounds, xdensity = p.xdensity,
                            ydensity = p.ydensity, offset = p.offset, size = p.size,
                            aspect_ratio = p.aspect_ratio,
                            x = p.x + 0.7 * np.cos(p.orientation+p.angle) * p.cross * p.size * p.aspect_ratio,
                            y = p.y + 0.7 * np.sin(p.orientation+p.angle) * p.cross * p.size * p.aspect_ratio)

        return np.maximum( x_1, x_2 )

class Translator(PatternGenerator):
    """
    PatternGenerator that translates another PatternGenerator over
    time.

    This PatternGenerator will create a series of episodes, where in
    each episode the underlying generator is moved in a fixed
    direction at a fixed speed.  To begin an episode, the Translator's
    x, y, and direction are evaluated (e.g. from random
    distributions), and the underlying generator is then drawn at
    those values plus changes over time that are determined by the
    speed.  The orientation of the underlying generator should be set
    to 0 to get motion perpendicular to the generator's orientation
    (which is typical).

    Note that at present the parameter values for x, y, and direction
    cannot be passed in when the instance is called; only the values
    set on the instance are used.
    """
    generator = param.ClassSelector(default=Gaussian(),
        class_=PatternGenerator,doc="""Pattern to be translated.""")

    direction = param.Number(default=0.0,softbounds=(-pi,pi),doc="""
        The direction in which the pattern should move, in radians.""")

    speed = param.Number(default=0.01,bounds=(0.0,None),doc="""
        The speed with which the pattern should move,
        in sheet coordinates per time_fn unit.""")

    reset_period = param.Number(default=1,bounds=(0.0,None),doc="""
        Period between generating each new translation episode.""")

    episode_interval = param.Number(default=0,doc="""
        Interval between successive translation episodes.

        If nonzero, the episode_separator pattern is presented for
        this amount of time_fn time after each episode, e.g. to
        allow processing of the previous episode to complete.""")

    episode_separator = param.ClassSelector(default=Constant(scale=0.0),
         class_=PatternGenerator,doc="""
         Pattern to display during the episode_interval, if any.
         The default is a blank pattern.""")

    time_fn = param.Callable(default=param.Dynamic.time_fn,doc="""
        Function to generate the time used as a base for translation.""")

    def _advance_params(self):
        """
        Explicitly generate new values for these parameters only
        when appropriate.
        """
        for p in ['x','y','direction']:
            self.force_new_dynamic_value(p)
        self.last_time = self.time_fn()

    def __init__(self,**params):
        super(Translator,self).__init__(**params)
        self._advance_params()

    def __call__(self,**params_to_override):
        p=ParamOverrides(self,params_to_override)

        if self.time_fn() >= self.last_time + p.reset_period:
            ## Returns early if within episode interval
            if self.time_fn()<self.last_time+p.reset_period+p.episode_interval:
                return p.episode_separator(xdensity=p.xdensity,
                                           ydensity=p.ydensity,
                                           bounds=p.bounds)
            else:
                self._advance_params()

        # JABALERT: Does not allow x, y, or direction to be passed in
        # to the call; fixing this would require implementing
        # inspect_value and force_new_dynamic_value (for
        # use in _advance_params) for ParamOverrides.
        #
        # Access parameter values without giving them new values
        assert ('x' not in params_to_override and
                'y' not in params_to_override and
                'direction' not in params_to_override)
        x = self.inspect_value('x')
        y = self.inspect_value('y')
        direction = self.inspect_value('direction')

        # compute how much time elapsed from the last reset
        # float(t) required because time could be e.g. gmpy.mpq
        t = float(self.time_fn()-self.last_time)

        ## CEBALERT: mask gets applied twice, both for the underlying
        ## generator and for this one.  (leads to redundant
        ## calculations in current lissom_oo_or usage, but will lead
        ## to problems/limitations in the future).
        return p.generator(
            xdensity=p.xdensity,ydensity=p.ydensity,bounds=p.bounds,
            x=x+t*np.cos(direction)*p.speed+p.generator.x,
            y=y+t*np.sin(direction)*p.speed+p.generator.y,
            orientation=(direction-pi/2)+p.generator.orientation)

# Legacy Sweeper class which is used in lissom.ty, should be deleted
# once lissom.ty is deprecated
#
# CB: I removed motion_sign from this class because I think it is
# unnecessary. But maybe I misunderstood the original author's
# intention?
#
# In any case, the original implementation was incorrect - it was not
# possible to get some motion directions (directions in one whole
# quadrant were missed out).
#
# Note that to get a 2pi range of directions, one must use a 2pi range
# of orientations (there are two directions for any given
# orientation).  Alternatively, we could generate a random sign, and
# use an orientation restricted to a pi range.

class OldSweeper(PatternGenerator):
    """
    PatternGenerator that sweeps a supplied PatternGenerator in a
    direction perpendicular to its orientation.
    """

    generator = param.Parameter(default=Gaussian(),precedence=0.97, doc="Pattern to sweep.")

    speed = param.Number(default=0.25,bounds=(0.0,None),doc="""
        Sweep speed: number of sheet coordinate units per unit time.""")

    step = param.Number(default=1,doc="""
        Number of steps at the given speed to move in the sweep direction.
        The distance moved is speed*step.""")

    # Provide access to value needed for measuring maps
    def __get_phase(self): return self.generator.phase
    def __set_phase(self,new_val): self.generator.phase = new_val
    phase = property(__get_phase,__set_phase)

    def function(self,p):
        """Selects and returns one of the patterns in the list."""
        pg = p.generator
        motion_orientation=p.orientation+pi/2.0

        new_x = p.x+p.size*pg.x
        new_y = p.y+p.size*pg.y

        image_array = pg(xdensity=p.xdensity,ydensity=p.ydensity,bounds=p.bounds,
                         x=new_x + p.speed*p.step*np.cos(motion_orientation),
                         y=new_y + p.speed*p.step*np.sin(motion_orientation),
                         orientation=p.orientation,
                         scale=pg.scale*p.scale,offset=pg.offset+p.offset)

        return image_array

class NumpyFile(FileImage):
    """
    For backwards compatibility.
    """

    pattern_sampler = param.ClassSelector(class_=ImageSampler,
        default=PatternSampler(background_value_fn=edge_average,
                               size_normalization='original',
                               whole_pattern_output_fns=[]),doc="""
        The PatternSampler to use to resample/resize the image.""")

_public = list(set([_k for _k,_v in locals().items() if isinstance(_v,type) and issubclass(_v,PatternGenerator)]))
__all__ = _public


text:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title></title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />

NAME

VERSION

DESCRIPTION

What is a Meta Object Protocol?

Yet Another Class Builder! Why?

Who is this module for?

What changes do I have to make to use this module?

About Performance

About Metaclass compatibility

Using custom metaclasses

PROTOCOLS

The Class protocol

The Attribute protocol

The Method protocol

The Instance protocol

FUNCTIONS

Utility functions

Metaclass cache functions

SEE ALSO

Books

Papers

Prior Art

Articles

SIMILAR MODULES

BUGS

ACKNOWLEDGEMENTS

AUTHOR

COPYRIGHT AND LICENSE

NAME

Class::MOP - A Meta Object Protocol for Perl 5

VERSION

version 2.0604

DESCRIPTION

This module is a fully functioning meta object protocol for the Perl 5 object system. It makes no attempt to change the behavior or characteristics of the Perl 5 object system, only to create a protocol for its manipulation and introspection.

That said, it does attempt to create the tools for building a rich set of extensions to the Perl 5 object system. Every attempt has been made to abide by the spirit of the Perl 5 object system that we all know and love.

This documentation is sparse on conceptual details. We suggest looking at the items listed in the "SEE ALSO" section for more information. In particular the book "The Art of the Meta Object Protocol" was very influential in the development of this system.

What is a Meta Object Protocol?

A meta object protocol is an API to an object system.

To be more specific, it abstracts the components of an object system (classes, object, methods, object attributes, etc.). These abstractions can then be used to inspect and manipulate the object system which they describe.

It can be said that there are two MOPs for any object system; the implicit MOP and the explicit MOP. The implicit MOP handles things like method dispatch or inheritance, which happen automatically as part of how the object system works. The explicit MOP typically handles the introspection/reflection features of the object system.

All object systems have implicit MOPs. Without one, they would not work. Explicit MOPs are much less common, and depending on the language can vary from restrictive (Reflection in Java or C#) to wide open (CLOS is a perfect example).

Yet Another Class Builder! Why?

This is not a class builder so much as a class builder builder. The intent is that an end user will not use this module directly, but instead this module is used by module authors to build extensions and features onto the Perl 5 object system.

This system is used by Moose, which supplies a powerful class builder system built entirely on top of Class::MOP.

Who is this module for?

This module is for anyone who has ever created or wanted to create a module for the Class:: namespace. The tools which this module provides make doing complex Perl 5 wizardry simpler, by removing such barriers as the need to hack symbol tables, or understand the fine details of method dispatch.

What changes do I have to make to use this module?

This module was designed to be as unintrusive as possible. Many of its features are accessible without any change to your existing code. It is meant to be a complement to your existing code and not an intrusion on your code base. Unlike many other Class:: modules, this module does not require you subclass it, or even that you use it in within your module's package.

The only features which require additions to your code are the attribute handling and instance construction features, and these are both completely optional features. The only reason for this is because Perl 5's object system does not actually have these features built in. More information about this feature can be found below.

About Performance

It is a common misconception that explicit MOPs are a performance hit. This is not a universal truth, it is a side-effect of some specific implementations. For instance, using Java reflection is slow because the JVM cannot take advantage of any compiler optimizations, and the JVM has to deal with much more runtime type information as well.

Reflection in C# is marginally better as it was designed into the language and runtime (the CLR). In contrast, CLOS (the Common Lisp Object System) was built to support an explicit MOP, and so performance is tuned for it.

This library in particular does its absolute best to avoid putting any drain at all upon your code's performance. In fact, by itself it does nothing to affect your existing code. So you only pay for what you actually use.

About Metaclass compatibility

This module makes sure that all metaclasses created are both upwards and downwards compatible. The topic of metaclass compatibility is highly esoteric and is something only encountered when doing deep and involved metaclass hacking. There are two basic kinds of metaclass incompatibility; upwards and downwards.

Upwards metaclass compatibility means that the metaclass of a given class is either the same as (or a subclass of) all of the class's ancestors.

Downward metaclass compatibility means that the metaclasses of a given class's ancestors are all the same as (or a subclass of) that metaclass.

Here is a diagram showing a set of two classes (A and B) and two metaclasses (Meta::A and Meta::B) which have correct metaclass compatibility both upwards and downwards.

    +---------+     +---------+
    | Meta::A |<----| Meta::B |      <....... (instance of  )
    +---------+     +---------+      <------- (inherits from)
         ^               ^
         :               :
    +---------+     +---------+
    |    A    |<----|    B    |
    +---------+     +---------+

In actuality, all of a class's metaclasses must be compatible, not just the class metaclass. That includes the instance, attribute, and method metaclasses, as well as the constructor and destructor classes.

Class::MOP will attempt to fix some simple types of incompatibilities. If all the metaclasses for the parent class are subclasses of the child's metaclasses then we can simply replace the child's metaclasses with the parent's. In addition, if the child is missing a metaclass that the parent has, we can also just make the child use the parent's metaclass.

As I said this is a highly esoteric topic and one you will only run into if you do a lot of subclassing of Class::MOP::Class. If you are interested in why this is an issue see the paper Uniform and safe metaclass composition linked to in the "SEE ALSO" section of this document.

Using custom metaclasses

Always use the metaclass pragma when using a custom metaclass, this will ensure the proper initialization order and not accidentally create an incorrect type of metaclass for you. This is a very rare problem, and one which can only occur if you are doing deep metaclass programming. So in other words, don't worry about it.

Note that if you're using Moose we encourage you to not use the metaclass pragma, and instead use Moose::Util::MetaRole to apply roles to a class's metaclasses. This topic is covered at length in various Moose::Cookbook recipes.

PROTOCOLS

The meta-object protocol is divided into 4 main sub-protocols:

The Class protocol

This provides a means of manipulating and introspecting a Perl 5 class. It handles symbol table hacking for you, and provides a rich set of methods that go beyond simple package introspection.

See Class::MOP::Class for more details.

The Attribute protocol

This provides a consistent representation for an attribute of a Perl 5 class. Since there are so many ways to create and handle attributes in Perl 5 OO, the Attribute protocol provide as much of a unified approach as possible. Of course, you are always free to extend this protocol by subclassing the appropriate classes.

See Class::MOP::Attribute for more details.

The Method protocol

This provides a means of manipulating and introspecting methods in the Perl 5 object system. As with attributes, there are many ways to approach this topic, so we try to keep it pretty basic, while still making it possible to extend the system in many ways.

See Class::MOP::Method for more details.

The Instance protocol

This provides a layer of abstraction for creating object instances. Since the other layers use this protocol, it is relatively easy to change the type of your instances from the default hash reference to some other type of reference. Several examples are provided in the examples/ directory included in this distribution.

See Class::MOP::Instance for more details.

FUNCTIONS

Note that this module does not export any constants or functions.

Utility functions

Note that these are all called as functions, not methods.

Class::MOP::get_code_info($code)

This function returns two values, the name of the package the $code is from and the name of the $code itself. This is used by several elements of the MOP to determine where a given $code reference is from.

Class::MOP::class_of($instance_or_class_name)

This will return the metaclass of the given instance or class name. If the class lacks a metaclass, no metaclass will be initialized, and undef will be returned.

Metaclass cache functions

Class::MOP holds a cache of metaclasses. The following are functions (not methods) which can be used to access that cache. It is not recommended that you mess with these. Bad things could happen, but if you are brave and willing to risk it: go for it!

Class::MOP::get_all_metaclasses

This will return a hash of all the metaclass instances that have been cached by Class::MOP::Class, keyed by the package name.

Class::MOP::get_all_metaclass_instances

This will return a list of all the metaclass instances that have been cached by Class::MOP::Class.

Class::MOP::get_all_metaclass_names

This will return a list of all the metaclass names that have been cached by Class::MOP::Class.

Class::MOP::get_metaclass_by_name($name)

This will return a cached Class::MOP::Class instance, or nothing if no metaclass exists with that $name.

Class::MOP::store_metaclass_by_name($name, $meta)

This will store a metaclass in the cache at the supplied $key.

Class::MOP::weaken_metaclass($name)

In rare cases (e.g. anonymous metaclasses) it is desirable to store a weakened reference in the metaclass cache. This function will weaken the reference to the metaclass stored in $name.

Class::MOP::metaclass_is_weak($name)

Returns true if the metaclass for $name has been weakened (via weaken_metaclass).

Class::MOP::does_metaclass_exist($name)

This will return true of there exists a metaclass stored in the $name key, and return false otherwise.

Class::MOP::remove_metaclass_by_name($name)

This will remove the metaclass stored in the $name key.

Some utility functions (such as Class::MOP::load_class) that were previously defined in Class::MOP regarding loading of classes have been extracted to Class::Load. Please see Class::Load for documentation.

SEE ALSO

Books

There are very few books out on Meta Object Protocols and Metaclasses because it is such an esoteric topic. The following books are really the only ones I have found. If you know of any more, please email me and let me know, I would love to hear about them.

The Art of the Meta Object Protocol

Advances in Object-Oriented Metalevel Architecture and Reflection

Putting MetaClasses to Work

Smalltalk: The Language

Papers

"Uniform and safe metaclass composition"

An excellent paper by the people who brought us the original Traits paper. This paper is on how Traits can be used to do safe metaclass composition, and offers an excellent introduction section which delves into the topic of metaclass compatibility.

http://scg.unibe.ch/archive/papers/Duca05ySafeMetaclassTrait.pdf

"Safe Metaclass Programming"

This paper seems to precede the above paper, and propose a mix-in based approach as opposed to the Traits based approach. Both papers have similar information on the metaclass compatibility problem space.

http://citeseer.ist.psu.edu/37617.html

Prior Art

The Perl 6 MetaModel work in the Pugs project

http://svn.openfoundry.org/pugs/misc/Perl-MetaModel/

http://github.com/perl6/p5-modules/tree/master/Perl6-ObjectSpace/

Articles

CPAN Module Review of Class::MOP

http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html

SIMILAR MODULES

As I have said above, this module is a class-builder-builder, so it is not the same thing as modules like Class::Accessor and Class::MethodMaker. That being said there are very few modules on CPAN with similar goals to this module. The one I have found which is most like this module is Class::Meta, although its philosophy and the MOP it creates are very different from this modules.

BUGS

All complex software has bugs lurking in it, and this module is no exception.

Please report any bugs to bug-class-mop@rt.cpan.org, or through the web interface at http://rt.cpan.org.

You can also discuss feature requests or possible bugs on the Moose mailing list (moose@perl.org) or on IRC at irc://irc.perl.org/#moose.

ACKNOWLEDGEMENTS

Rob Kinyon

Thanks to Rob for actually getting the development of this module kick-started.

AUTHOR

Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for details.

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Infinity Interactive, Inc..

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


text:
redirect_to: https://anvio.org/help/7.1/artifacts/summary
---


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.matching;

import com.facebook.presto.matching.pattern.CapturePattern;
import com.facebook.presto.matching.pattern.EqualsPattern;
import com.facebook.presto.matching.pattern.FilterPattern;
import com.facebook.presto.matching.pattern.TypeOfPattern;
import com.facebook.presto.matching.pattern.WithPattern;

public class DefaultPrinter
        implements PatternVisitor
{
    private StringBuilder result = new StringBuilder();
    private int level = 0;

    public String result()
    {
        return result.toString();
    }

    @Override
    public void visitTypeOf(TypeOfPattern<?> pattern)
    {
        visitPrevious(pattern);
        appendLine("typeOf(%s)", pattern.expectedClass().getSimpleName());
    }

    @Override
    public void visitWith(WithPattern<?> pattern)
    {
        visitPrevious(pattern);
        appendLine("with(%s)", pattern.getProperty().getName());
        level += 1;
        pattern.getPattern().accept(this);
        level -= 1;
    }

    @Override
    public void visitCapture(CapturePattern<?> pattern)
    {
        visitPrevious(pattern);
        appendLine("capturedAs(%s)", pattern.capture().description());
    }

    @Override
    public void visitEquals(EqualsPattern<?> pattern)
    {
        visitPrevious(pattern);
        appendLine("equals(%s)", pattern.expectedValue());
    }

    @Override
    public void visitFilter(FilterPattern<?> pattern)
    {
        visitPrevious(pattern);
        appendLine("filter(%s)", pattern.predicate());
    }

    private void appendLine(String template, Object... arguments)
    {
        result.append(Util.indent(level, template + "\n", arguments));
    }
}


text:
"""Operator overloads for `RaggedTensor`."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.ops import math_ops
from tensorflow.python.ops.ragged import ragged_getitem
from tensorflow.python.ops.ragged import ragged_tensor
from tensorflow.python.util import tf_decorator

def _right(operator):
  """Right-handed version of an operator: swap args x and y."""
  return tf_decorator.make_decorator(operator, lambda y, x: operator(x, y))

# Indexing
ragged_tensor.RaggedTensor.__getitem__ = ragged_getitem.ragged_tensor_getitem

# Ordering operators
ragged_tensor.RaggedTensor.__ge__ = math_ops.greater_equal
ragged_tensor.RaggedTensor.__gt__ = math_ops.greater
ragged_tensor.RaggedTensor.__le__ = math_ops.less_equal
ragged_tensor.RaggedTensor.__lt__ = math_ops.less

# Logical operators
ragged_tensor.RaggedTensor.__and__ = math_ops.logical_and
ragged_tensor.RaggedTensor.__rand__ = _right(math_ops.logical_and)
ragged_tensor.RaggedTensor.__invert__ = math_ops.logical_not
ragged_tensor.RaggedTensor.__ror__ = _right(math_ops.logical_or)
ragged_tensor.RaggedTensor.__or__ = math_ops.logical_or
ragged_tensor.RaggedTensor.__xor__ = math_ops.logical_xor
ragged_tensor.RaggedTensor.__rxor__ = _right(math_ops.logical_xor)

# Arithmetic operators
ragged_tensor.RaggedTensor.__abs__ = math_ops.abs
ragged_tensor.RaggedTensor.__add__ = math_ops.add
ragged_tensor.RaggedTensor.__radd__ = _right(math_ops.add)
ragged_tensor.RaggedTensor.__div__ = math_ops.div
ragged_tensor.RaggedTensor.__rdiv__ = _right(math_ops.div)
ragged_tensor.RaggedTensor.__floordiv__ = math_ops.floordiv
ragged_tensor.RaggedTensor.__rfloordiv__ = _right(math_ops.floordiv)
ragged_tensor.RaggedTensor.__mod__ = math_ops.floormod
ragged_tensor.RaggedTensor.__rmod__ = _right(math_ops.floormod)
ragged_tensor.RaggedTensor.__mul__ = math_ops.multiply
ragged_tensor.RaggedTensor.__rmul__ = _right(math_ops.multiply)
ragged_tensor.RaggedTensor.__neg__ = math_ops.negative
ragged_tensor.RaggedTensor.__pow__ = math_ops.pow
ragged_tensor.RaggedTensor.__rpow__ = _right(math_ops.pow)
ragged_tensor.RaggedTensor.__sub__ = math_ops.subtract
ragged_tensor.RaggedTensor.__rsub__ = _right(math_ops.subtract)
ragged_tensor.RaggedTensor.__truediv__ = math_ops.truediv
ragged_tensor.RaggedTensor.__rtruediv__ = _right(math_ops.truediv)

# Dummy methods
def _dummy_bool(_):
  """Dummy method to prevent a RaggedTensor from being used as a Python bool."""
  raise TypeError("RaggedTensor may not be used as a boolean.")

ragged_tensor.RaggedTensor.__bool__ = _dummy_bool
ragged_tensor.RaggedTensor.__nonzero__ = _dummy_bool


text:
/*
 * $Log: XMLFactory.java,v $
 * Revision 1.5  2011/07/27 17:07:21  vizigoth
 * Additions and alterations coincidence with editing the Reg-XML document part 1.
 *
 * Revision 1.4  2011/02/14 22:32:58  vizigoth
 * First commit after major sourceforge outage.
 *
 * Revision 1.3  2011/01/04 10:40:23  vizigoth
 * Refactor all package names to simpler forms more consistent with typical Java usage.
 *
 * Revision 1.2  2010/11/08 16:42:15  vizigoth
 * Tidied up imports.
 *
 * Revision 1.1  2010/06/16 14:56:41  vizigoth
 * Towards better Reg XML support for complete documents ... still work in progress.
 *
 * Revision 1.4  2009/05/14 16:15:34  vizigoth
 * Major refactor to remove dependency on JPA and introduce better interface and implementation separation. Removed all setPropertiesFromInterface and castFromInterface methods.
 *
 * Revision 1.3  2009/03/30 09:05:07  vizigoth
 * Refactor to use SMPTE harmonized names and add early KLV file support.
 *
 * Revision 1.2  2008/10/15 14:16:08  vizigoth
 * Documentation improved to an early release level.
 *
 * Revision 1.1  2007/11/13 22:14:54  vizigoth
 * Public release of MAJ API.
 */

package tv.amwa.maj.io.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import tv.amwa.maj.model.Preface;
import tv.amwa.maj.model.impl.IdentificationImpl;

// TODO comments
// TODO tests

/**
 *
Provides a means to read and write XML files from the local filing system.

 *
 *
THE DOCUMENTATION FOR THIS CLASS IS INCOMPLETE.

 *
 *
 */
public class XMLFactory {

	public final static Preface readPreface(
			String inputFileName)
		throws FileNotFoundException,
			IOException,
			NullPointerException,
			SAXException {

		if (inputFileName == null)
			throw new NullPointerException("The given AAF XML file reference is null.");

		File aafxFile = new File(inputFileName);
		aafxFile = aafxFile.getAbsoluteFile();

		if (!(aafxFile.exists()))
			throw new FileNotFoundException("The given AAF XML file does not exist.");

		if (!(aafxFile.isFile()))
			throw new FileNotFoundException("The given AAF XML file name is not a file.");

		if (!(aafxFile.canRead()))
			throw new IOException("Insufficient privileges to read the given AAF XML file.");

		Map<String, InputStream> streams = XMLBuilder.parseDocTypeToStreams(aafxFile);

		return (Preface) XMLBuilder.createFromXML(
				new InputSource(new FileInputStream(aafxFile)),
				streams);
	}

	public final static void writePreface(
			Preface preface,
			String outputFileName)
		throws NullPointerException,
			IOException {

		if (outputFileName == null)
			throw new NullPointerException("Cannot write to a file specified as null.");

		File aafxFile = new File(outputFileName);
		aafxFile = aafxFile.getAbsoluteFile();

//		if (aafxFile.exists())
//			throw new IOException("Cannot overwrite the given file as it already exists.");

		overwriteFile(aafxFile, preface);
	}

	public final static void overwriteFile(
			File aafxFile,
			Preface preface)
		throws NullPointerException,
			IOException {

		if (aafxFile == null)
			throw new NullPointerException("Cannot write to a file specified as null.");
		if (preface == null)
			throw new NullPointerException("Cannot write to an AAF XML file with a null header.");

		if (!(aafxFile.getParentFile().canWrite()))
			throw new IOException("Cannot write to the directory of the given file.");

		if ((aafxFile.exists()) && (!aafxFile.canWrite()))
			throw new IOException("Insufficient privileges to write to the given file.");

		preface.updateDictionaries();
		preface.updateEssenceContainers();
		// TODO deal with updating descriptive schemes
		AAFElement documentRoot = new AAFElement(preface);
		String asXML = XMLBuilder.toXML(documentRoot, aafxFile);

		FileWriter fileWriter = new FileWriter(aafxFile);
		fileWriter.write(asXML);
		fileWriter.close();

		XMLBuilder.writeStreams(documentRoot.getDocument(), aafxFile);
	}

	public final static void main(String args[]) {

		if (args.length == 0) {
			System.out.println("Usage: java tv.amwa.maj.io.xml.XMLFactory <reg_xml_file>");
			System.exit(1);
		}

		try {
			Preface header = readPreface(args[0]);
			header.appendIdentification(new IdentificationImpl(
					IdentificationImpl.APICompanyName,
					IdentificationImpl.APIProductName,
					IdentificationImpl.APIProductVersionString,
					IdentificationImpl.APIProductID));
			System.out.println(header.toString());
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

}


text:
package org.apache.isis.viewer.restfulobjects.server;

import java.util.List;
import javax.jdo.JDOException;
import com.google.common.collect.Lists;

class RuntimeExceptionPojo {

    public static RuntimeExceptionPojo create(final Exception ex) {
        return new RuntimeExceptionPojo(ex);
    }

    private static String format(final StackTraceElement stackTraceElement) {
        return stackTraceElement.toString();
    }

    private final String className;
    private final String message;
    private final List<String> stackTrace = Lists.newArrayList();
    private RuntimeExceptionPojo causedBy;

    public RuntimeExceptionPojo(final Throwable ex) {
        this.className = ex.getClass().getName();
        this.message = messageFor(ex);
        final StackTraceElement[] stackTraceElements = ex.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTraceElements) {
            this.stackTrace.add(format(stackTraceElement));
        }

        final Throwable cause = causeOf(ex);
        if (cause != null && cause != ex) {
            this.causedBy = new RuntimeExceptionPojo(cause);
        }
    }

    private static Throwable causeOf(Throwable ex) {
        if (ex instanceof JDOException) {
            final JDOException jdoException = (JDOException) ex;
            final Throwable[] nestedExceptions = jdoException.getNestedExceptions();
            return nestedExceptions.length > 0? nestedExceptions[0]: null;
        }
        else {
            return ex.getCause();
        }
    }

    private static String messageFor(final Throwable ex) {
        final String message = ex.getMessage();
        return message != null ? message : ex.getClass().getName();
    }

    @SuppressWarnings("unused")
    public String getClassName() {
        return className;
    }

    @SuppressWarnings("unused")
    public String getMessage() {
        return message;
    }

    @SuppressWarnings("unused")
    public List<String> getStackTrace() {
        return stackTrace;
    }

    @SuppressWarnings("unused")
    public RuntimeExceptionPojo getCausedBy() {
        return causedBy;
    }
}


text:
package Run4YourBits;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * Menu displayed at the end each game
 */
public class EndMenu extends JFrame implements ActionListener {

    private JButton retry, returnMenu;
    Fondo fondo;
    Background back;
    Menu menu;

    /**
     * Constructs the EndMenu
     *
     * @param back returns the menu
     */
    public EndMenu(Background back) {

        this.back = back;

        this.setUndecorated(true);
        this.setTitle("Pause");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(180, 350);
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("Images" + File.separator + "logo.png"));

        this.add(fondo = new Fondo("fondoPausa.png"));
        fondo.setLayout(new GridLayout(2, 2));

        retry = CustomButton.createButton("Retry", this.getGraphicsConfiguration(), 16);
        retry.addActionListener(this);

        returnMenu = CustomButton.createButton("Return to Menu", this.getGraphicsConfiguration(), 16);
        returnMenu.addActionListener(this);

        fondo.add(returnMenu);
        fondo.add(retry);

        try {
            this.getContentPane().setCursor(
                    Toolkit.getDefaultToolkit().createCustomCursor(
                    CompatibleImage.toCompatibleImage(
                    ImageIO.read(new File("Images" + File.separator + "cursor.png"))), new Point(0, 0), "cursor"));
        } catch (IOException ex) {
            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.pack();
        this.setLocationRelativeTo(null);

        this.setVisible(true);

        this.requestFocus();
    }

    /**
     * Defines and runs the action for each button
     *
     * @param e Required ActionEvent
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == retry) {

            Background newLevel = new Background(back.getLevelLength(), back.getStandardHeight(),
                    back.getMaxHeightValue(), back.getCharacterImage(),
                    back.getCharacterHitImage(), back.getCharacterGameOverImage(),
                    back.getCharacterSteps(), back.getSpeed(), 75, back.getBackgroundImage(),
                    back.getFrameImage(), back.getLowObstacleImage(),
                    back.getHighObstacleImage(), back.getObstacleDistance(), back.getLowObstacleHeight(),
                    back.getMusicFile(), back.getJumpSound(), back.getHitSound(),
                    back.getDashSound(), back.getDelta(), back.getStringColor(), false);

            JFrame frame = (JFrame) back.getTopLevelAncestor();
            frame.setVisible(false);
            frame.remove(back);
            frame.add(newLevel);
            frame.setVisible(true);
            this.transferFocusBackward();
            this.dispose();
        }

        if (e.getSource() == returnMenu) {
            JFrame frame = (JFrame) back.getTopLevelAncestor();
            frame.setVisible(false);
            menu = new Menu();
            frame.dispose();
            this.dispose();
        }
    }
}


text:
package org.spockframework.runtime.model.parallel;

import org.spockframework.util.Beta;

import java.util.Objects;

/**
 * An exclusive resource identified by a key with a lock mode that is used to
 * synchronize access to shared resources when executing nodes in parallel.
 *
 * @since 2.0
 */
@Beta
public class ExclusiveResource {

  private final String key;
  private final ResourceAccessMode mode;
  private int hash;

  public ExclusiveResource(String key, ResourceAccessMode mode) {
    this.key = Objects.requireNonNull(key, "key must not be null");
    this.mode = Objects.requireNonNull(mode, "mode must be non null");
  }

  public String getKey() {
    return key;
  }

  public ResourceAccessMode getMode() {
    return mode;
  }
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ExclusiveResource that = (ExclusiveResource) o;
    return Objects.equals(key, that.key) && mode == that.mode;
  }

  @Override
  public int hashCode() {
    int h = hash;
    if (h == 0) {
      h = hash = Objects.hash(key, mode);
    }
    return h;
  }

}


text:
package com.nagopy.android.callconfirm;

import android.app.Application;

import com.nagopy.android.callconfirm.di.ApplicationComponent;
import com.nagopy.android.callconfirm.di.ApplicationModule;
import com.nagopy.android.callconfirm.di.DaggerApplicationComponent;

import timber.log.Timber;

public class App extends Application {

    public static ApplicationComponent component;

    @Override
    public void onCreate() {
        super.onCreate();

        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }

        component = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }

    public static ApplicationComponent getComponent() {
        return component;
    }
}


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import bean.ArticleChp1;
import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Boolean;
import java.util.List;
import javax.swing.JTable;

/**
 *
 * @author kamal
 */
public class Article1Helper extends AbstractViewHelper<ArticleChp1> {

    public Article1Helper(JTable jTable, List<ArticleChp1> articleChp1s) {
        super(new String[]{"Article chaitre1"});
        this.jTable = jTable;
        this.list = articleChp1s;

    }

//    private List<ArticleChp1> Articlecocher(List<ArticleChp1> articleChp1s) {
//
//        for (int i = 0; i < articleChp1s.size(); i++) {
//            articleChp1s.get(i).setC(false);
//        }
//        return articleChp1s;
//    }

    @Override

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < list.size()) {
            switch (columnIndex) {
                case 0:
                    return list.get(rowIndex).getTitre();
                default:
                    return null;
            }
        }
        return null;
    }
}