text:
#pragma once

namespace Ht {

#	ifndef HT_MODEL_NO_UNIT_THREADS
#	define HT_MODEL_UNIT_THREADS
#	endif

	class CHtHifBase;
	class CHtModelUnitBase;
	class CHtModelHifLibBase;
	class CHtModelUnitLibBase;

	extern void HtCoprocModel(Ht::CHtHifLibBase *);

	class CHtModelHifLibBase {
		friend class CHtModelHif;
		friend class CHtModelHifBase;
	protected:
		static CHtModelHifLibBase * NewHtModelHifLibBase(CHtHifLibBase * pHtHifLibBase);
		virtual ~CHtModelHifLibBase() {}
		virtual int GetUnitCnt() { assert(0); return 0; }
		virtual CHtModelUnitLibBase * const * AllocAllUnits() { assert(0); return 0; }
		virtual void FreeAllUnits() { assert(0); }
		virtual CHtModelUnitLibBase * AllocUnit(CHtModelUnitBase * pHtModelUnitBase) { assert(0); return 0; }
		virtual void FreeUnit(CHtModelUnitLibBase * pHtModelUnitLibBase) { assert(0); }
	};

	class CHtModelUnitLibBase {
		friend class CHtModelUnitBase;
	protected:
		virtual ~CHtModelUnitLibBase() {}
		virtual void SetUsingCallback(bool bUsingCallback) = 0;
		virtual ERecvType RecvPoll(bool bUseCbCall, bool bUseCbMsg, bool bUseCbData) = 0;
		virtual void SendHostMsg(uint8_t msgType, uint64_t msgData) = 0;
		virtual bool RecvHostMsg(uint8_t &msgType, uint64_t &msgData) = 0;
		virtual int SendHostData(int qwCnt, uint64_t * pData) = 0;
		virtual bool SendHostDataMarker() = 0;
		virtual void FlushHostData() = 0;
		virtual bool PeekHostData(uint64_t & data) = 0;
		virtual int RecvHostData(int qwCnt, uint64_t * pData) = 0;
		virtual bool RecvHostDataMarker() = 0;
		virtual bool RecvHostHalt() = 0;
		virtual bool RecvCall(int & argc, uint64_t * pArgv) = 0;
		virtual bool SendReturn(int argc, uint64_t * pArgv) = 0;
	};

	class CHtModelHifBase {
		friend class CHtModelUnitBase;
	protected:
		CHtModelHifBase(CHtHifLibBase * pHtHifLibBase) {
			m_pHtModelHifLibBase = CHtModelHifLibBase::NewHtModelHifLibBase(pHtHifLibBase);
		}
		~CHtModelHifBase() {
			delete m_pHtModelHifLibBase;
		}
		CHtModelUnitLibBase * AllocUnit(CHtModelUnitBase * pHtModelUnitBase) {
			return m_pHtModelHifLibBase->AllocUnit(pHtModelUnitBase);
		}
		void FreeUnit(CHtModelUnitLibBase * pHtModelUnitLibBase) {
			m_pHtModelHifLibBase->FreeUnit(pHtModelUnitLibBase);
		}
		int GetUnitCntBase() {
			return m_pHtModelHifLibBase->GetUnitCnt();
		}

	protected:
		CHtModelHifLibBase * m_pHtModelHifLibBase;
	};

	class CHtModelUnitBase {
		friend class CHtModelUnitLib;
	protected:
		CHtModelUnitBase(CHtModelHifBase * pHtModelHifBase) {
			m_pHtModelHifBase = pHtModelHifBase;
			m_pHtModelUnitLibBase = m_pHtModelHifBase->AllocUnit(this);
		}

	public:
		virtual ~CHtModelUnitBase() {
			m_pHtModelHifBase->FreeUnit(m_pHtModelUnitLibBase);
		}

	public:
		void SetUsingCallback( bool bUsingCallback ) {
			m_pHtModelUnitLibBase->SetUsingCallback(bUsingCallback);
		}

		Ht::ERecvType RecvPoll(bool bUseCbCall=true, bool bUseCbMsg=true, bool bUseCbData=true) {
			return m_pHtModelUnitLibBase->RecvPoll(bUseCbCall, bUseCbMsg, bUseCbData);
		}

		bool RecvHostHalt() {
			return m_pHtModelUnitLibBase->RecvHostHalt();
		}

		virtual void RecvCallback( Ht::ERecvType ) { assert(0); }

	protected:
		void SendHostMsg(uint8_t msgType, uint64_t msgData) {
			m_pHtModelUnitLibBase->SendHostMsg(msgType, msgData);
		}

		bool RecvHostMsg(uint8_t &msgType, uint64_t &msgData) {
			return m_pHtModelUnitLibBase->RecvHostMsg(msgType, msgData);
		}

		int SendHostData(int qwCnt, uint64_t * pData) {
			return m_pHtModelUnitLibBase->SendHostData(qwCnt, pData);
		}

		bool SendHostDataMarker() {
			return m_pHtModelUnitLibBase->SendHostDataMarker();
		}

		void FlushHostData() {
			m_pHtModelUnitLibBase->FlushHostData();
		}

		bool PeekHostData(uint64_t & data) {
			return m_pHtModelUnitLibBase->PeekHostData(data);
		}

		int RecvHostData(int qwCnt, uint64_t * pData) {
			return m_pHtModelUnitLibBase->RecvHostData(qwCnt, pData);
		}

		bool RecvHostDataMarker() {
			return m_pHtModelUnitLibBase->RecvHostDataMarker();
		}

		bool RecvCall(int & argc, uint64_t * pArgv) {
			return m_pHtModelUnitLibBase->RecvCall(argc, pArgv);
		}

		bool SendReturn(int argc, uint64_t * pArgv) {
			return m_pHtModelUnitLibBase->SendReturn(argc, pArgv);
		}

	protected:
		CHtModelUnitLibBase * m_pHtModelUnitLibBase;

	private:
		CHtModelHifBase * m_pHtModelHifBase;
	};

	class CHtModelHif : public Ht::CHtModelHifBase {
	public:
		CHtModelHif(Ht::CHtHifLibBase * pHtHifLibBase) : Ht::CHtModelHifBase(pHtHifLibBase) {}
		int GetUnitCnt() { return GetUnitCntBase(); }
		template<typename T> void StartUnitThreads();
		void WaitForUnitThreads();
	private:
		pthread_t * m_pthreads;
		Ht::CHtModelUnitBase ** m_ppUnits;
	};

#	ifdef HT_MODEL_UNIT_THREADS
	template<typename T> inline void CHtModelHif::StartUnitThreads() {
		int unitCnt = GetUnitCnt();

		// start threads
		m_pthreads = new pthread_t [unitCnt];
		m_ppUnits = new Ht::CHtModelUnitBase * [unitCnt];
		for (int i = 0; i < unitCnt; i += 1) {
			m_ppUnits[i] = new T(this);
			assert(m_ppUnits[i]);
			pthread_create(&m_pthreads[i], 0, T::StartUnitThread, m_ppUnits[i]);
		}
	}

	inline void CHtModelHif::WaitForUnitThreads() {
		// wait for threads to finish
		int unitCnt = GetUnitCnt();
		void * status;
		for (int i = 0; i < unitCnt; i += 1) {
			if (m_ppUnits[i]) {
				int pthread_join_rtn = pthread_join(m_pthreads[i], &status);
				assert(pthread_join_rtn == 0);
				delete m_ppUnits[i];
				m_ppUnits[i] = 0;
			}
		}
		delete m_pthreads;
	}
#	endif

} // namespace Ht


text:
<resources>
    <string name="rating_dialog_experience">Como foi sua experiência conosco?</string>
    <string name="rating_dialog_maybe_later">Mais tarde</string>
    <string name="rating_dialog_never">Nunca</string>
    <string name="rating_dialog_feedback_title">Comentários</string>
    <string name="rating_dialog_submit">Enviar</string>
    <string name="rating_dialog_cancel">Cancelar</string>
    <string name="rating_dialog_suggestions">Sugira-nos o que deu errado e vamos trabalhar nisso!</string>
</resources>

redpajama_set_name: RedPajamaGithub


text:
package org.elasticsearch.index.store;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.store.*;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.compress.Compressor;
import org.elasticsearch.common.compress.CompressorFactory;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.Directories;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.index.CloseableIndexComponent;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.store.distributor.Distributor;
import org.elasticsearch.index.store.support.ForceSyncDirectory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.Adler32;
import java.util.zip.Checksum;

/**
 */
public class Store extends AbstractIndexShardComponent implements CloseableIndexComponent {

    static final String CHECKSUMS_PREFIX = "_checksums-";

    public static final boolean isChecksum(String name) {
        return name.startsWith(CHECKSUMS_PREFIX);
    }
    private final AtomicBoolean isClosed = new AtomicBoolean(false);
    private final AtomicInteger refCount = new AtomicInteger(1);

    private final IndexStore indexStore;
    final CodecService codecService;
    private final DirectoryService directoryService;
    private final StoreDirectory directory;

    private volatile ImmutableOpenMap<String, StoreFileMetaData> filesMetadata = ImmutableOpenMap.of();
    private volatile String[] files = Strings.EMPTY_ARRAY;
    private final Object mutex = new Object();

    private final boolean sync;

    @Inject
    public Store(ShardId shardId, @IndexSettings Settings indexSettings, IndexStore indexStore, CodecService codecService, DirectoryService directoryService, Distributor distributor) throws IOException {
        super(shardId, indexSettings);
        this.indexStore = indexStore;
        this.codecService = codecService;
        this.directoryService = directoryService;
        this.sync = componentSettings.getAsBoolean("sync", true); // TODO we don't really need to fsync when using shared gateway...
        this.directory = new StoreDirectory(distributor);
    }

    public IndexStore indexStore() {
        ensureOpen();
        return this.indexStore;
    }

    public Directory directory() {
        ensureOpen();
        return directory;
    }

    private final void ensureOpen() {
        if (this.refCount.get() <= 0) {
            throw new AlreadyClosedException("Store is already closed");
        }
    }

    public ImmutableMap<String, StoreFileMetaData> list() throws IOException {
        ensureOpen();
        ImmutableMap.Builder<String, StoreFileMetaData> builder = ImmutableMap.builder();
        for (String name : files) {
            StoreFileMetaData md = metaData(name);
            if (md != null) {
                builder.put(md.name(), md);
            }
        }
        return builder.build();
    }

    public StoreFileMetaData metaData(String name) throws IOException {
        ensureOpen();
        StoreFileMetaData md = filesMetadata.get(name);
        if (md == null) {
            return null;
        }
        // IndexOutput not closed, does not exists
        if (md.length() == -1) {
            return null;
        }
        return md;
    }

    /**
     * Deletes the content of a shard store. Be careful calling this!.
     */
    public void deleteContent() throws IOException {
        ensureOpen();
        String[] files = directory.listAll();
        IOException lastException = null;
        for (String file : files) {
            if (isChecksum(file)) {
                try {
                    directory.deleteFileChecksum(file);
                } catch (IOException e) {
                    lastException = e;
                }
            } else {
                try {
                    directory.deleteFile(file);
                } catch (NoSuchFileException | FileNotFoundException e) {
                    // ignore
                } catch (IOException e) {
                    lastException = e;
                }
            }
        }
        if (lastException != null) {
            throw lastException;
        }
    }

    public StoreStats stats() throws IOException {
        ensureOpen();
        return new StoreStats(Directories.estimateSize(directory), directoryService.throttleTimeInNanos());
    }

    public ByteSizeValue estimateSize() throws IOException {
        ensureOpen();
        return new ByteSizeValue(Directories.estimateSize(directory));
    }

    public void renameFile(String from, String to) throws IOException {
        ensureOpen();
        synchronized (mutex) {
            StoreFileMetaData fromMetaData = filesMetadata.get(from); // we should always find this one
            if (fromMetaData == null) {
                throw new FileNotFoundException(from);
            }
            directoryService.renameFile(fromMetaData.directory(), from, to);
            StoreFileMetaData toMetaData = new StoreFileMetaData(to, fromMetaData.length(), fromMetaData.checksum(), fromMetaData.directory());
            filesMetadata = ImmutableOpenMap.builder(filesMetadata).fRemove(from).fPut(to, toMetaData).build();
            files = filesMetadata.keys().toArray(String.class);
        }
    }

    public static Map<String, String> readChecksums(File[] locations) throws IOException {
        Directory[] dirs = new Directory[locations.length];
        try {
            for (int i = 0; i < locations.length; i++) {
                dirs[i] = new SimpleFSDirectory(locations[i]);
            }
            return readChecksums(dirs, null);
        } finally {
            IOUtils.closeWhileHandlingException(dirs);
        }
    }

    private static Map<String, String> readChecksums(Directory[] dirs, Map<String, String> defaultValue) throws IOException {
        long lastFound = -1;
        Directory lastDir = null;
        for (Directory dir : dirs) {
            for (String name : dir.listAll()) {
                if (!isChecksum(name)) {
                    continue;
                }
                long current = Long.parseLong(name.substring(CHECKSUMS_PREFIX.length()));
                if (current > lastFound) {
                    lastFound = current;
                    lastDir = dir;
                }
            }
        }
        if (lastFound == -1) {
            return defaultValue;
        }
        try (IndexInput indexInput = lastDir.openInput(CHECKSUMS_PREFIX + lastFound, IOContext.READONCE)) {
            indexInput.readInt(); // version
            return indexInput.readStringStringMap();
        } catch (Throwable e) {
            // failed to load checksums, ignore and return an empty map
            return defaultValue;
        }
    }

    public void writeChecksums() throws IOException {
        ensureOpen();
        ImmutableMap<String, StoreFileMetaData> files = list();
        String checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis();
        synchronized (mutex) {
            Map<String, String> checksums = new HashMap<>();
            for (StoreFileMetaData metaData : files.values()) {
                if (metaData.checksum() != null) {
                    checksums.put(metaData.name(), metaData.checksum());
                }
            }
            while (directory.fileExists(checksumName)) {
                checksumName = CHECKSUMS_PREFIX + System.currentTimeMillis();
            }
            try (IndexOutput output = directory.createOutput(checksumName, IOContext.DEFAULT, true)) {
                output.writeInt(0); // version
                output.writeStringStringMap(checksums);
            }

        }
        for (StoreFileMetaData metaData : files.values()) {
            if (metaData.name().startsWith(CHECKSUMS_PREFIX) && !checksumName.equals(metaData.name())) {
                try {
                    directory.deleteFileChecksum(metaData.name());
                } catch (Throwable e) {
                    // ignore
                }
            }
        }
    }

    /**
     * Returns <tt>true</tt> by default.
     */
    public boolean suggestUseCompoundFile() {
        return false;
    }

    public final void incRef() {
        do {
            int i = refCount.get();
            if (i > 0) {
                if (refCount.compareAndSet(i, i+1)) {
                    return;
                }
            } else {
                throw new AlreadyClosedException("Store is already closed can't increment refCount current count [" + i + "]");
            }
        } while(true);
    }

    public final void decRef() {
        int i = refCount.decrementAndGet();
        assert i >= 0;
        if (i == 0) {
            closeInternal();
        }

    }

    public void close() {
        if (isClosed.compareAndSet(false, true)) {
            // only do this once!
            decRef();
        }
    }

    private void closeInternal() {
        synchronized (mutex) { // if we close the dir we need to make sure nobody writes checksums
            try {
                directory.closeInternal(); // don't call close here we throw an exception there!
            } catch (IOException e) {
                logger.debug("failed to close directory", e);
            }
        }
    }

    /**
     * Creates a raw output, no checksum is computed, and no compression if enabled.
     */
    public IndexOutput createOutputRaw(String name) throws IOException {
        ensureOpen();
        return directory.createOutput(name, IOContext.DEFAULT, true);
    }

    /**
     * Opened an index input in raw form, no decompression for example.
     */
    public IndexInput openInputRaw(String name, IOContext context) throws IOException {
        ensureOpen();
        StoreFileMetaData metaData = filesMetadata.get(name);
        if (metaData == null) {
            throw new FileNotFoundException(name);
        }
        return metaData.directory().openInput(name, context);
    }

    public void writeChecksum(String name, String checksum) throws IOException {
        ensureOpen();
        // update the metadata to include the checksum and write a new checksums file
        synchronized (mutex) {
            StoreFileMetaData metaData = filesMetadata.get(name);
            metaData = new StoreFileMetaData(metaData.name(), metaData.length(), checksum, metaData.directory());
            filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(name, metaData).build();
            writeChecksums();
        }
    }

    public void writeChecksums(Map<String, String> checksums) throws IOException {
        ensureOpen();
        // update the metadata to include the checksum and write a new checksums file
        synchronized (mutex) {
            for (Map.Entry<String, String> entry : checksums.entrySet()) {
                StoreFileMetaData metaData = filesMetadata.get(entry.getKey());
                metaData = new StoreFileMetaData(metaData.name(), metaData.length(), entry.getValue(), metaData.directory());
                filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(entry.getKey(), metaData).build();
            }
            writeChecksums();
        }
    }

    /**
     * The idea of the store directory is to cache file level meta data, as well as md5 of it
     */
    public class StoreDirectory extends BaseDirectory implements ForceSyncDirectory {

        private final Distributor distributor;

        StoreDirectory(Distributor distributor) throws IOException {
            this.distributor = distributor;
            synchronized (mutex) {
                ImmutableOpenMap.Builder<String, StoreFileMetaData> builder = ImmutableOpenMap.builder();
                Map<String, String> checksums = readChecksums(distributor.all(), new HashMap<String, String>());
                for (Directory delegate : distributor.all()) {
                    for (String file : delegate.listAll()) {
                        String checksum = checksums.get(file);
                        builder.put(file, new StoreFileMetaData(file, delegate.fileLength(file), checksum, delegate));
                    }
                }
                filesMetadata = builder.build();
                files = filesMetadata.keys().toArray(String.class);
            }
        }

        public ShardId shardId() {
            return Store.this.shardId();
        }

        public Settings settings() {
            return Store.this.indexSettings();
        }

        @Nullable
        public CodecService codecService() {
            return Store.this.codecService;
        }

        public Directory[] delegates() {
            return distributor.all();
        }

        @Override
        public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
            ensureOpen();
            // lets the default implementation happen, so we properly open an input and create an output
            super.copy(to, src, dest, context);
        }

        @Override
        public String[] listAll() throws IOException {
            ensureOpen();
            return files;
        }

        @Override
        public boolean fileExists(String name) throws IOException {
            ensureOpen();
            return filesMetadata.containsKey(name);
        }

        public void deleteFileChecksum(String name) throws IOException {
            ensureOpen();
            StoreFileMetaData metaData = filesMetadata.get(name);
            if (metaData != null) {
                try {
                    metaData.directory().deleteFile(name);
                } catch (IOException e) {
                    if (metaData.directory().fileExists(name)) {
                        throw e;
                    }
                }
            }
            synchronized (mutex) {
                filesMetadata = ImmutableOpenMap.builder(filesMetadata).fRemove(name).build();
                files = filesMetadata.keys().toArray(String.class);
            }
        }

        @Override
        public void deleteFile(String name) throws IOException {
            ensureOpen();
            // we don't allow to delete the checksums files, only using the deleteChecksum method
            if (isChecksum(name)) {
                return;
            }
            StoreFileMetaData metaData = filesMetadata.get(name);
            if (metaData != null) {
                try {
                    metaData.directory().deleteFile(name);
                } catch (IOException e) {
                    if (metaData.directory().fileExists(name)) {
                        throw e;
                    }
                }
            }
            synchronized (mutex) {
                filesMetadata = ImmutableOpenMap.builder(filesMetadata).fRemove(name).build();
                files = filesMetadata.keys().toArray(String.class);
            }
        }

        /**
         * Returns the *actual* file length, not the uncompressed one if compression is enabled, this
         * messes things up when using compound file format, but it shouldn't be used in any case...
         */
        @Override
        public long fileLength(String name) throws IOException {
            ensureOpen();
            StoreFileMetaData metaData = filesMetadata.get(name);
            if (metaData == null) {
                throw new FileNotFoundException(name);
            }
            // not set yet (IndexOutput not closed)
            if (metaData.length() != -1) {
                return metaData.length();
            }
            return metaData.directory().fileLength(name);
        }

        @Override
        public IndexOutput createOutput(String name, IOContext context) throws IOException {
            return createOutput(name, context, false);
        }

        public IndexOutput createOutput(String name, IOContext context, boolean raw) throws IOException {
            ensureOpen();
            Directory directory;
            // we want to write the segments gen file to the same directory *all* the time
            // to make sure we don't create multiple copies of it
            if (isChecksum(name) || IndexFileNames.SEGMENTS_GEN.equals(name)) {
                directory = distributor.primary();
            } else {
                directory = distributor.any();
            }
            IndexOutput out = directory.createOutput(name, context);
            boolean success = false;
            try {
                synchronized (mutex) {
                    StoreFileMetaData metaData = new StoreFileMetaData(name, -1, null, directory);
                    filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(name, metaData).build();
                    files = filesMetadata.keys().toArray(String.class);
                    boolean computeChecksum = !raw;
                    if (computeChecksum) {
                        // don't compute checksum for segment based files
                        if (IndexFileNames.SEGMENTS_GEN.equals(name) || name.startsWith(IndexFileNames.SEGMENTS)) {
                            computeChecksum = false;
                        }
                    }
                    if (computeChecksum) {
                        out = new BufferedChecksumIndexOutput(out, new Adler32());
                    }

                    final StoreIndexOutput storeIndexOutput = new StoreIndexOutput(metaData, out, name);
                    success = true;
                    return storeIndexOutput;
                }
            } finally {
                if (!success) {
                    IOUtils.closeWhileHandlingException(out);
                }
            }
        }

        @Override
        public IndexInput openInput(String name, IOContext context) throws IOException {
            ensureOpen();
            StoreFileMetaData metaData = filesMetadata.get(name);
            if (metaData == null) {
                throw new FileNotFoundException(name);
            }
            IndexInput in = metaData.directory().openInput(name, context);
            boolean success = false;
            try {
                // Only for backward comp. since we now use Lucene codec compression
                if (name.endsWith(".fdt") || name.endsWith(".tvf")) {
                    Compressor compressor = CompressorFactory.compressor(in);
                    if (compressor != null) {
                        in = compressor.indexInput(in);
                    }
                }
                success = true;
            } finally {
                if (!success) {
                    IOUtils.closeWhileHandlingException(in);
                }
            }
            return in;
        }

        @Override
        public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException {
            ensureOpen();
            StoreFileMetaData metaData = filesMetadata.get(name);
            if (metaData == null) {
                throw new FileNotFoundException(name);
            }
            // Only for backward comp. since we now use Lucene codec compression
            if (name.endsWith(".fdt") || name.endsWith(".tvf")) {
                // rely on the slicer from the base class that uses an input, since they might be compressed...
                // note, it seems like slicers are only used in compound file format..., so not relevant for now
                return super.createSlicer(name, context);
            }
            return metaData.directory().createSlicer(name, context);
        }

        @Override
        public void close() throws IOException {
            assert false : "Nobody should close this directory except of the Store itself";
        }

        synchronized void closeInternal() throws IOException {
            if (isOpen) {
                isOpen = false;
                for (Directory delegate : distributor.all()) {
                    delegate.close();
                }
                synchronized (mutex) {
                    filesMetadata = ImmutableOpenMap.of();
                    files = Strings.EMPTY_ARRAY;
                }
            }
        }

        @Override
        public Lock makeLock(String name) {
            return distributor.primary().makeLock(name);
        }

        @Override
        public void clearLock(String name) throws IOException {
            distributor.primary().clearLock(name);
        }

        @Override
        public void setLockFactory(LockFactory lockFactory) throws IOException {
            distributor.primary().setLockFactory(lockFactory);
        }

        @Override
        public LockFactory getLockFactory() {
            return distributor.primary().getLockFactory();
        }

        @Override
        public String getLockID() {
            return distributor.primary().getLockID();
        }

        @Override
        public void sync(Collection<String> names) throws IOException {
            ensureOpen();
            if (sync) {
                Map<Directory, Collection<String>> map = Maps.newHashMap();
                for (String name : names) {
                    StoreFileMetaData metaData = filesMetadata.get(name);
                    if (metaData == null) {
                        throw new FileNotFoundException(name);
                    }
                    Collection<String> dirNames = map.get(metaData.directory());
                    if (dirNames == null) {
                        dirNames = new ArrayList<>();
                        map.put(metaData.directory(), dirNames);
                    }
                    dirNames.add(name);
                }
                for (Map.Entry<Directory, Collection<String>> entry : map.entrySet()) {
                    entry.getKey().sync(entry.getValue());
                }
            }
            for (String name : names) {
                // write the checksums file when we sync on the segments file (committed)
                if (!name.equals(IndexFileNames.SEGMENTS_GEN) && name.startsWith(IndexFileNames.SEGMENTS)) {
                    writeChecksums();
                    break;
                }
            }
        }

        @Override
        public void forceSync(String name) throws IOException {
            sync(ImmutableList.of(name));
        }

        @Override
        public String toString() {
            return "store(" + distributor.toString() + ")";
        }
    }

    class StoreIndexOutput extends IndexOutput {

        private final StoreFileMetaData metaData;

        private final IndexOutput out;

        private final String name;

        StoreIndexOutput(StoreFileMetaData metaData, IndexOutput delegate, String name) {
            this.metaData = metaData;
            this.out = delegate;
            this.name = name;
        }

        @Override
        public void close() throws IOException {
            out.close();
            String checksum = null;
            IndexOutput underlying = out;
            // TODO: cut over to lucene's CRC
            // *WARNING*: lucene has classes in same o.a.l.store package with very similar names,
            // but using CRC, not Adler!
            if (underlying instanceof BufferedChecksumIndexOutput) {
                Checksum digest = ((BufferedChecksumIndexOutput) underlying).digest();
                assert digest instanceof Adler32;
                checksum = Long.toString(digest.getValue(), Character.MAX_RADIX);
            }
            synchronized (mutex) {
                StoreFileMetaData md = new StoreFileMetaData(name, metaData.directory().fileLength(name), checksum, metaData.directory());
                filesMetadata = ImmutableOpenMap.builder(filesMetadata).fPut(name, md).build();
                files = filesMetadata.keys().toArray(String.class);
            }
        }

        @Override
        public void copyBytes(DataInput input, long numBytes) throws IOException {
            out.copyBytes(input, numBytes);
        }

        @Override
        public long getFilePointer() {
            return out.getFilePointer();
        }

        @Override
        public void writeByte(byte b) throws IOException {
            out.writeByte(b);
        }

        @Override
        public void writeBytes(byte[] b, int offset, int length) throws IOException {
            out.writeBytes(b, offset, length);
        }

        @Override
        public void flush() throws IOException {
            out.flush();
        }

        @Override
        public void seek(long pos) throws IOException {
            out.seek(pos);
        }

        @Override
        public long length() throws IOException {
            return out.length();
        }

        @Override
        public void setLength(long length) throws IOException {
            out.setLength(length);
        }

        @Override
        public String toString() {
            return out.toString();
        }

        @Override
        public long getChecksum() throws IOException {
            return out.getChecksum();
        }
    }
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Orchard.MPUnovo.Models
{
    public class PartnerViewMode
    {
        public string PartnerCode { get; set; }
        public string CompanyName { get; set; }
        public string Address { get; set; }
        public List<PartnerContactUser> Contacts { get; set; } = new List<PartnerContactUser>();
    }

    public class PartnerContactUser
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
    }

}


text:
//
//  MCArrayLinkedList.h
//  Sapindus
//
//  Created by YuliSun on 8/3/16.
//  Copyright © 2016 oreisoft. All rights reserved.
//

#ifndef MCArrayLinkedList_h
#define MCArrayLinkedList_h

#include "monkc.h"

//MC array linked list
typedef struct MCALItemStruct {
    struct MCALItemStruct* next;
    struct MCALItemStruct* prev;
    MCGeneric value;
    MCGeneric userdata;
} MCALItem;

MCInline MCALItem MCALItemMake(MCGeneric val)
{
    return (MCALItem){null, null, val};
}

MCInline void MCALItemLink(MCALItem* A, MCALItem* B)
{
    A->next = B;
    B->prev = A;
}

MCInline MCBool MCALItemIsHead(MCALItem* item)
{
    return (item->prev == null);
}

MCInline MCBool MCALItemIsTail(MCALItem* item)
{
    return (item->next == null);
}

#define MCArrayLinkedListMax 1024
typedef struct {
    MCBool isCircle;
    size_t count;
    MCALItem* head;
    MCALItem array[MCArrayLinkedListMax];
} MCArrayLinkedList;

MCInline void MCALSetHead(MCArrayLinkedList* list, MCALItem* item)
{
    item->prev = null;
    list->head = item;
}

MCInline void MCALSetTail(MCArrayLinkedList* list, MCALItem* item)
{
    item->next = null;
}

MCInline MCBool MCALIsEmpty(MCArrayLinkedList* list)
{
    return (list->head == null);
}

MCArrayLinkedList* MCArrayLinkedListInit(MCArrayLinkedList* list, MCGeneric values[], const size_t count);
MCArrayLinkedList* MCArrayLinkedListInitCircle(MCArrayLinkedList* list, MCGeneric values[], const size_t count);

MCALItem* MCALDeleteItem(MCArrayLinkedList* list, MCALItem* item);

typedef struct {
    MCGeneric data[MCArrayLinkedListMax];
    int nexti[MCArrayLinkedListMax];
    int previ[MCArrayLinkedListMax];
    int count;
} MCArrayList;

MCArrayList* MCArrayListInit(MCArrayList* list);
MCArrayList* MCArrayListAdd(MCArrayList* list, MCGeneric data);

#endif /* MCArrayLinkedList_h */


text:
var console = window.console || {
	log: function () {}
};

function log(text) {
	console.log(text);
}

var $DM = $DM || {};
(function (core) {
	var TokenKey = 'dm_core_token';

	var currentProjectId = null;
	var notificationTimeout = null;
	var loadingTimeout = null;
	var staticData = {
		allEntities: {}
	};

	function loadItem(key) {
		var result = localStorage.getItem(key);
		if (result === null) {
			return null;
		}
		return JSON.parse(result);
	}

	function storeItem(key, item) {
		return localStorage.setItem(key, JSON.stringify(item));
	}

	function removeItem(key) {
		return localStorage.removeItem(key);
	}

	function pageChanged(params) {
		var page = params.page;
		if (page === undefined) {
			page = 'main';
		}

		currentProjectId = null;

		showPage(page);
		switch (page) {
			case 'main':
				loadProjects();
				break;
			case 'project':
				loadProject(params.id);
				break;
			default:
				location.hash = '#page=main';
				break;
		}
	}

	function showPage(page) {
		$('div[data-page]').each(function () {
			var that = $(this);
			if (that.attr('data-page') === page) {
				that.show();
			} else {
				that.hide();
			}
		});
	}

	function showTab(pageRoot, tab) {
		pageRoot.find('[data-tab]').each(function () {
			var that = $(this);
			if (that.attr('data-tab') === tab) {
				that.show();
			} else {
				that.hide();
			}
		});
	}

	function showLogin() {
		$('#login').show(0, function () {
			$('#login_username').focus();
		});
		$('#app').hide();
	}

	function hideLogin() {
		$('#login').hide();
		$('#app').show();
	}

	function showLoading(text) {
		hideLoading();
		$('#loading').find('.loading_text').text(text);
		loadingTimeout = window.setTimeout(function () {
			$('#loading').show();
		}, 100);
	}

	function hideLoading() {
		if (loadingTimeout !== null) {
			window.clearTimeout(loadingTimeout);
			loadingTimeout = null;
		}
		$('#loading').hide();
	}

	function hideNotification() {
		var element = $('#notification');
		if (notificationTimeout !== null) {
			window.clearTimeout(notificationTimeout);
			notificationTimeout = null;
		}
		element.slideUp(250);
	}

	function displayNotification(text) {
		var element = $('#notification');
		if (notificationTimeout !== null) {
			window.clearTimeout(notificationTimeout);
			notificationTimeout = null;
		}
		element.text(text).slideDown(250, function () {
			notificationTimeout = window.setTimeout(function () {
				element.slideUp(250, function () {
					notificationTimeout = null;
				});
			}, 3000);
		});
	}

	function hideDialog() {
		var element = $('#dialog');
		element.fadeOut(150, function () {
			element.find('.dialog_body').empty();
		});
	}

	function displayDialog(title, callback) {
		var element = $('#dialog');
		element.find('.dialog_title').text(title);
		callback(element.find('.dialog_body'));
		element.fadeIn(250);
	}

	function createProjectListItem(item) {
		var element = $('
OpenDelete
');
		element.find('span').text(item.projectName);
		element.find('a:eq(0)').attr('href', '#page=project&id=' + encodeURIComponent(item.projectId));
		element.find('a:eq(1)').click(function () {
			if (!displayConfirm('Do you really want to remove project \'' + item.projectName + '\'?')) {
				return;
			}
			deleteCall('api/project/' + encodeURIComponent(item.projectId), function () {
				loadProjects();
			});
		});
		return element;
	}

	function createStoredItem(projectId, item) {
		var element = $('
Delete
');
		element.find('span').text(item);
		element.find('a').click(function () {
			if (!displayConfirm('Do you really want to remove stored item \'' + item + '\'?')) {
				return;
			}
			deleteCall('api/project/' + encodeURIComponent(projectId) + '/item/' + encodeURIComponent(item), function () {
				loadStoredItems(projectId);
			});
		});
		return element;
	}

	function loadProject(projectId) {
		currentProjectId = projectId;
		var root = $('div[data-page="project"]');
		showTab(root, 'project');
		restCall('Loading project detail', 'GET', 'api/project/' + encodeURIComponent(projectId), null, function (result) {
			var div = $('div[data-template="project"]');

			var tmpl = core.templates.create('all_project', div, function () {
				tmpl.setConfig(result);
				root.find('[data-id="project_name"]').val(result.name);
				loadStoredItems(projectId);

				root.find('pre[data-id="project-config"]').text('');
				window.setTimeout(function () {
					root.find('pre[data-id="project-config"]').text(JSON.stringify(tmpl.getConfig(), null, "  "));
				}, 500);
				root.find('pre[data-id="project-result"]').text('');
			});
			root.data('template', tmpl);
		});
	}

	function loadStoredItems(projectId) {
		restCall('Loading project items', 'GET', 'api/project/' + encodeURIComponent(projectId) + '/items', null, function (result) {
			var list = $('#project_items');
			list.empty();
			for (var i = 0; i < result.length; i++) {
				createStoredItem(projectId, result[i]).appendTo(list);
			}
			if (result.length === 0) {
				$('
').text('No items found').appendTo(list);
			}
		});
	}

	function loadProjects() {
		restCall('Loading projects', 'GET', 'api/projects', null, function (result) {
			var list = $('#project_list');
			list.empty();
			for (var i = 0; i < result.length; i++) {
				createProjectListItem(result[i]).appendTo(list);
			}
			if (result.length === 0) {
				$('
').text('No projects found').appendTo(list);
			}
		});
	}

	function checkToken() {
		restCall('Testing connection', 'GET', 'api/app/ping', null, function (result) {
			hideLogin();
			initAppAfterLogin();
		});
	}

	function isEmpty(string) {
		if (string === null || string === undefined || string === '') {
			return true;
		}
		return false;
	}

	function displayAlert(message) {
		window.alert(message);
	}

	function displayConfirm(message) {
		return window.confirm(message);
	}

	function restCall(description, method, url, data, success, expectedDataType) {
		showLoading(description);
		var ajaxObj = {
			url: url,
			type: method,
			dataType: expectedDataType ? expectedDataType : "json",
			success: function (result) {
				hideLoading();
				success(result);
			},
			error: function (xhr) {
				hideLoading();
				var title = 'Error';
				var text = 'Server error.';
				if (xhr.status) {
					title += ' ' + xhr.status;
				}

				if (xhr.statusText) {
					title += ' - ' + xhr.statusText;
				}

				if (xhr.responseText) {
					text = xhr.responseText;
				}
				log('Error: ' + title + "\n" + text);
			}
		};
		if (data !== null) {
			ajaxObj.data = JSON.stringify(data);
			ajaxObj.contentType = "application/json; charset=utf-8";
		}
		return $.ajax(ajaxObj);
	}

	function postJson(url, data, success) {
		return restCall('Sending data', 'POST', url, data, success);
	}

	function deleteCall(url, success) {
		return restCall('Removing data', 'DELETE', url, null, success);
		return $.ajax(url, {
			type: 'DELETE',
			dataType: 'json',
			success: success
		});
	}

	function uploadForm(url, data, success) {
		showLoading("Uploading file");
		return $.ajax(url, {
			data: data,
			contentType: false,
			processData: false,
			type: 'POST',
			dataType: 'json',
			success: function (result) {
				hideLoading();
				success(result);
			},
			error: function (xhr) {
				hideLoading();
				if (xhr.status === 401) {
					// not authorized - handled with a different code
					return;
				}

				var title = 'Error';
				var text = 'Server error.';
				if (xhr.status) {
					title += ' ' + xhr.status;
				}

				if (xhr.statusText) {
					title += ' - ' + xhr.statusText;
				}

				if (xhr.responseText) {
					text = xhr.responseText;
				}
				log('Error: ' + title + "\n" + text);
			}
		});
	}

	function loadStaticData(callback) {
		restCall('Loading application data', 'GET', 'api/app/all_entities', null, function (result) {
			staticData.allEntities = result;
			callback();
		});
	}

	function lookupNameInArray(arr, id) {
		for (var i = 0; i < arr.length; i++) {
			if (arr[i].id === id) {
				return arr[i].name;
			}
		}
		return null;
	}

	function lookupName(id) {
		var name = lookupNameInArray(staticData.allEntities.readers, id);
		if (name !== null) {
			return name;
		}
		name = lookupNameInArray(staticData.allEntities.processors, id);
		if (name !== null) {
			return name;
		}
		name = lookupNameInArray(staticData.allEntities.renderers, id);
		if (name !== null) {
			return name;
		}
		return '---';
	}

	function initLoginForm() {
		$('#login form').submit(function (event) {
			event.preventDefault();

			var username = $('#login_username').val();
			if (isEmpty(username)) {
				displayAlert('No username given');
				return;
			}

			var password = $('#login_password').val();
			if (isEmpty(password)) {
				displayAlert('No password given');
				return;
			}

			$('#login form').find('input[type="submit"]').attr('disabled', 'disabled').val('Logging In ...');
			postJson('auth/token', {
				username: username,
				password: password
			}, function (result) {
				$('#login form').find('input[type="submit"]').attr('disabled', null).val('Log In');
				if (result.token) {
					storeItem(TokenKey, result.token);
					checkToken();
				}
			}).fail(function () {
				$('#login form').find('input[type="submit"]').attr('disabled', null).val('Log In');
				displayAlert('Wrong username or password');
			});
		});
	}

	function initProjectControls() {
		$('a[data-id="create_project"]').click(function (event) {
			event.preventDefault();
			restCall('Creating a project', 'POST', 'api/project', null, function (result) {
				loadProjects();
			});
		});

		var root = $('div[data-page="project"]');
		root.find('a[data-id="close"]').click(function (evt) {
			evt.preventDefault();
		});

		root.find('a[data-id="project_items"]').click(function (evt) {
			evt.preventDefault();
			showTab(root, 'project_items');
		});

		root.find('a[data-id="back_to_project"]').click(function (evt) {
			evt.preventDefault();
			showTab(root, 'project');
		});

		root.find('a[data-id="save_project"]').click(function (evt) {
			evt.preventDefault();
			var config = root.data('template').getConfig();
			log("Project Config:\n" + JSON.stringify(config, null, "  "));
			postJson('api/project/' + encodeURIComponent(currentProjectId), config, function (result) {
				displayNotification('Project was successfully saved');
			});
		});

		root.find('a[data-id="maximize"]').click(function (evt) {
			evt.preventDefault();
			root.find('div.popup-box').addClass('popup-maximized');
		});

		root.find('a[data-id="close-popup"]').click(function (evt) {
			evt.preventDefault();
			root.find('div.popup-box').removeClass('popup-maximized');
		});

		root.find('a[data-id="run_project"]').click(function (evt) {
			evt.preventDefault();
			runToThisStep(null);
		});
	}

	function runToThisStep(idx) {
		var root = $('div[data-page="project"]');
		showTab(root, 'output');
		root.find('div[data-id="json-project-result"]').hide().empty();
		root.find('pre[data-id="text-project-result"]').show().text('Loading ...');

		var config = root.data('template').getConfig();
		var lastRenderer = null;
		var rendererIdx = config.steps.length - 1;
		if (idx !== null) {
			rendererIdx = idx;
		}
		if (rendererIdx >= 0) {
			if (config.steps[rendererIdx].renderer !== null) {
				lastRenderer = config.steps[rendererIdx].renderer.id;
			}
		}

		var runUrl;
		if (idx !== null) {
			runUrl = 'api/project/' + encodeURIComponent(currentProjectId) + '/run/' + encodeURIComponent(idx + 1);
		} else {
			runUrl = 'api/project/' + encodeURIComponent(currentProjectId) + '/run';
		}

		log("Project Config:\n" + JSON.stringify(config, null, "  "));
		restCall('Saving project', 'POST', 'api/project/' + encodeURIComponent(currentProjectId), config, function (result) {
			restCall('Running project', 'POST', runUrl, null, function (result) {
				displayNotification('Project was successfully run');
				autoResize(root.find('div[data-id="autoresize"]'));
				if (lastRenderer === 'dm:data:renderers:json') {
					root.find('pre[data-id="text-project-result"]').hide();
					var element = root.find('div[data-id="json-project-result"]').show().get(0);
					var allData = JSON.parse(result);
					for (var idx = 0; idx < allData.length; idx++) {
						var data = allData[idx];
						if (allData.length > 1) {
							var title = document.createElement('h4');
							title.textContent = data.name;
							if (idx > 0) {
								title.className = 'table-title table-next-title';
							} else {
								title.className = 'table-title';
							}
							element.appendChild(title);
						}

						var table = document.createElement('table');
						table.className = 'nice-table';
						var titleRow = document.createElement('tr');
						for (var i = 0; i < data.headers.length; i++) {
							var th = document.createElement('th');
							th.textContent = data.headers[i];
							titleRow.appendChild(th);
						}
						table.appendChild(titleRow);

						for (var i = 0; i < data.rows.length; i++) {
							var row = data.rows[i];
							var tr = document.createElement('tr');
							for (var j = 0; j < row.length; j++) {
								var td = document.createElement('td');
								td.textContent = renderValue(data.dataTypes[j], row[j]);
								td.className = getClassForDataType(data.dataTypes[j]);
								tr.appendChild(td);
							}
							table.appendChild(tr);
						}
						element.appendChild(table);
					}
				} else {
					root.find('pre[data-id="text-project-result"]').show().text(result);
					root.find('div[data-id="json-project-result"]').hide();
				}
			}, 'text').fail(function (xhr) {
				root.find('pre[data-id="text-project-result"]').show().text("Failed:\n" + xhr.responseText);
				root.find('div[data-id="json-project-result"]').hide();
			});
		});
	}

	function renderValue(dataTypeId, value) {
		switch (dataTypeId) {
			case 'dm:data:types:currency':
				return formatNumber(value, 2);
			case 'dm:data:types:integer':
				return formatNumber(value, 0);
			case 'dm:data:types:double':
				return formatNumber(value, null);
			case 'dm:data:types:decimal':
				return formatNumber(value, null);
			default:
				return value;
		}
	}

	function formatNumber(number, decimalPlaces) {
		var isNegative = false;
		var output = '';
		if (decimalPlaces !== null) {
			number = number.toFixed(decimalPlaces);
		}

		if (number < 0) {
			number = -number;
			isNegative = true;
		}

		var str = '' + number;
		var decimalPointIdx = str.indexOf('.');
		if (decimalPointIdx >= 0) {
			output = '.' + str.substring(decimalPointIdx + 1);
		} else {
			decimalPointIdx = str.length;
		}

		if (decimalPlaces !== null && decimalPlaces > 0) {
			for (var i = output.length; i < decimalPlaces + 1; i++) {
				if (i === 0) {
					output += '.';
				} else {
					output += '0';
				}
			}
		}

		var start = decimalPointIdx % 3;
		var intOutput = str.substring(0, start);
		for (var i = start; i < decimalPointIdx; i += 3) {
			if (i > 0) {
				intOutput += ',';
			}
			intOutput += str.substring(i, i + 3);
		}

		if (isNegative) {
			return '-' + intOutput + output;
		} else {
			return intOutput + output;
		}
	}

	function getClassForDataType(dataTypeId) {
		switch (dataTypeId) {
			case 'dm:data:types:currency':
			case 'dm:data:types:integer':
			case 'dm:data:types:double':
			case 'dm:data:types:decimal':
				return 'data-type-numeric';
			default:
				return '';
		}
	}

	function detectConfiguration(stepIdx, callback) {
		var root = $('div[data-page="project"]');
		var config = root.data('template').getConfig();
		log("Project Config:\n" + JSON.stringify(config, null, "  "));
		restCall('Detecting project configuration', 'POST', 'api/project/' + encodeURIComponent(currentProjectId) + '/detect/' + stepIdx, config, function (result) {
			displayNotification('Configuration was successfully detected');
			callback(result);
		}).fail(function () {
			displayNotification('Failed to detect configuration');
		});
	}

	function initUploadForm() {
		$('#upload_item_form').submit(function (event) {
			event.preventDefault();

			var itemName = $('#upload_item_name').val();
			if (isEmpty(itemName)) {
				displayAlert('No item name given');
				return;
			}

			var files = $('#upload_file').get(0).files;
			if (files.length !== 1) {
				displayAlert('No file added');
			}
			var file = files[0];

			var formData = new FormData();
			formData.append('name', itemName);
			formData.append('data', file, file.name);
			$('#upload_item_form').find('input[type="submit"]').attr('disabled', 'disabled').val('Uploading ...');

			uploadForm('api/project/' + encodeURIComponent(currentProjectId) + '/item', formData, function (result) {
				$('#upload_item_form').find('input[type="submit"]').attr('disabled', null).val('Upload');
				$('#upload_item_form').get(0).reset();
				displayNotification('Upload successfully finished');
				loadStoredItems(currentProjectId);
			}).fail(function () {
				$('#upload_item_form').find('input[type="submit"]').attr('disabled', null).val('Upload');
				displayNotification('Upload failed');
			});
		});
	}

	function autoResize(element) {
		var width = Math.min(document.body.clientWidth, 960);
		var height = document.body.clientHeight;

		var computedWidth = width - 2;
		var computedHeight = height - 276 - 2;

		element.css('width', computedWidth + 'px');
		element.css('height', computedHeight + 'px');
	}

	function initNotification() {
		$('#notification').click(hideNotification);
	}

	function onHashChange() {
		var hash = location.hash;
		if (!hash || hash === '') {
			pageChanged({});
			return;
		}
		if (hash.indexOf('#') === 0) {
			hash = hash.substring(1);
		}

		var params = {};
		var keyValuePairs = hash.split('&');
		for (var i = 0; i < keyValuePairs.length; i++) {
			var pair = keyValuePairs[i].split('=');
			if (pair.length === 1) {
				params[decodeURIComponent(pair[0])] = true;
			} else {
				params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
			}
		}
		pageChanged(params);
	}

	function initHashListener() {
		$(window).on('hashchange', onHashChange);
	}

	function initAppAfterLogin() {
		loadStaticData(function () {
			log('Init');
			onHashChange();
		});
	}

	function initDialog() {
		$('#dialog [data-id="close"]').click(hideDialog);
	}

	$.ajaxSetup({
		beforeSend: function (xhr) {
			var token = loadItem(TokenKey);
			if (token !== null) {
				xhr.setRequestHeader('Authorization', loadItem(TokenKey));
			}
		}
	});

	$(document).ajaxError(function (event, xhr, ajaxSettings, thrownError) {
		if (xhr.status === 401) { // unauthorized
			removeItem(TokenKey);
			showLogin();
		} else {
			log('Error: ' + xhr.status + ' - ' + xhr.statusText + ": " + thrownError);
		}
	});

	core.templates = {};

	var templateContext = {
		templates: core.templates,
		getCurrentProjectId: function () {
			return currentProjectId;
		},
		displayAlert: displayAlert,
		displayConfirm: displayConfirm,
		restCall: restCall,
		lookupName: lookupName,
		getAllEntities: function () {
			return staticData.allEntities; // mutable!!!
		},
		displayDialog: displayDialog,
		hideDialog: hideDialog,
		detectConfiguration: detectConfiguration,
		runToThisStep: runToThisStep
	};

	core.addTemplate = function (id, fcn) {
		core.templates.addTemplate(id, fcn, templateContext);
	};

	core.init = function () {
		initHashListener();
		initNotification();
		initLoginForm();
		initUploadForm();
		initProjectControls();
		initDialog();
		checkToken();
	};
})($DM);

$($DM.init);


text:
package org.ops4j.pax.web.extender.whiteboard.internal.tracker;

import org.ops4j.pax.web.extender.whiteboard.internal.ExtendedHttpServiceRuntime;
import org.ops4j.pax.web.extender.whiteboard.internal.ExtenderContext;
import org.ops4j.pax.web.extender.whiteboard.internal.element.HttpContextElement;
import org.ops4j.pax.web.service.whiteboard.HttpContextMapping;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Tracks {@link HttpContextMapping}s.
 *
 * @author Alin Dreghiciu
 * @since 0.4.0, April 06, 2008
 */
public class HttpContextMappingTracker extends AbstractHttpContextTracker<HttpContextMapping> {

	/**
	 * Constructor.
	 *  @param extenderContext
	 *            extender context; cannot be null
	 * @param bundleContext
	 * @param httpServiceRuntime
	 */
	private HttpContextMappingTracker(final ExtenderContext extenderContext, final BundleContext bundleContext,
									  ExtendedHttpServiceRuntime httpServiceRuntime) {
		super(extenderContext, bundleContext, httpServiceRuntime);
	}

	public static ServiceTracker<HttpContextMapping, HttpContextElement> createTracker(
			final ExtenderContext extenderContext, final BundleContext bundleContext, ExtendedHttpServiceRuntime httpServiceRuntime) {
		return new HttpContextMappingTracker(extenderContext, bundleContext, httpServiceRuntime).create(HttpContextMapping.class);
	}

	/**
	 * @see AbstractHttpContextTracker#createHttpContextElement(ServiceReference,
	 *      Object)
	 */
	@Override
	HttpContextElement createHttpContextElement(final ServiceReference<HttpContextMapping> serviceReference,
												final HttpContextMapping published) {
		return new HttpContextElement(serviceReference, published);
	}

}


text:
package org.mrgeo.mapalgebra

import java.io.{Externalizable, IOException, ObjectInput, ObjectOutput}

import org.apache.spark.{SparkConf, SparkContext}
import org.mrgeo.data.raster.{RasterUtils, RasterWritable}
import org.mrgeo.data.rdd.RasterRDD
import org.mrgeo.data.tile.TileIdWritable
import org.mrgeo.job.JobArguments
import org.mrgeo.mapalgebra.parser.{ParserException, ParserNode}
import org.mrgeo.mapalgebra.raster.RasterMapOp
import org.mrgeo.utils.SparkUtils

object BandExtractMapOp extends MapOpRegistrar {
  override def register: Array[String] = {
    Array[String]("bandextract", "extract", "be")
  }

  def create(raster:RasterMapOp, band:Int = 0) =
    new BandExtractMapOp(Some(raster), band)

  override def apply(node:ParserNode, variables: String => Option[ParserNode]): MapOp =
    new BandExtractMapOp(node, true, variables)
}

class BandExtractMapOp extends RasterMapOp with Externalizable {

  private var rasterRDD: Option[RasterRDD] = None
  private var input: Option[RasterMapOp] = null
  private var band: Int = 0

  private[mapalgebra] def this(raster:Option[RasterMapOp], band:Int) = {
    this()
    input = raster
    this.band = band
  }

  private[mapalgebra] def this(node:ParserNode, isSlope:Boolean, variables: String => Option[ParserNode]) = {
    this()

    if (node.getNumChildren != 2) {
      throw new ParserException(node.getName + " requires two arguments - a source raster and a band number")
    }

    input = RasterMapOp.decodeToRaster(node.getChild(0), variables)
    val inputName = node.getChild(0).getName
    val inputMapOp = input.getOrElse(throw new ParserException("Can't load raster! Ouch!"))
    band = MapOp.decodeInt(node.getChild(1)).getOrElse(
      throw new ParserException("Expected a numeric band number in the second argument instead of " +
        node.getChild(1).getName)) - 1
    if (band < 0) {
      throw new ParserException(s"The band must be between 1 and the number of bands in the input raster")
    }
  }

  override def rdd(): Option[RasterRDD] = rasterRDD

  override def execute(context: SparkContext): Boolean = {

    implicit val tileIdOrdering = new Ordering[TileIdWritable] {
      override def compare(x: TileIdWritable, y: TileIdWritable): Int = x.compareTo(y)
    }

    var zoom: Int = -1
    var tilesize: Int = -1
    var tiletype: Int = -1
    var totalbands: Int = 0

    input match {
      case Some(pyramid) =>
        val meta = pyramid.metadata() getOrElse(throw new IOException("Can't load metadata! Ouch! " + pyramid.getClass.getName))

        if (band >= meta.getBands) {
          throw new ParserException(s"The input raster has ${meta.getBands} bands. Cannot extract band $band")
        }
        // check for the same max zooms
        if (zoom < 0) {
          zoom = meta.getMaxZoomLevel
        }
        else if (zoom != meta.getMaxZoomLevel) {
          throw new IllegalArgumentException("All images must have the same max zoom level. " +
            "one is " + meta.getMaxZoomLevel + ", others are " + zoom)
        }

        if (tilesize < 0) {
          tilesize = meta.getTilesize
        }
        else if (tilesize != meta.getTilesize) {
          throw new IllegalArgumentException("All images must have the same tilesize. " +
            "one is " + meta.getTilesize + ", others are " + tilesize)
        }

        if (tiletype < 0) {
          tiletype = meta.getTileType
        }
        else if (tiletype != meta.getTileType) {
          throw new IllegalArgumentException("All images must have the same tile type. " +
            "one is " + meta.getTileType + ", others are " + tiletype)
        }
        val nodata = meta.getDefaultValue(band)
        val inputRDD = pyramid.rdd().getOrElse(throw new IOException("Can't load RDD! Ouch!"))
        rasterRDD = Some(RasterRDD(inputRDD.map(U => {
          val dst = RasterUtils.createEmptyRaster(tilesize, tilesize, 1, tiletype, nodata)
          val sourceRaster = RasterWritable.toRaster(U._2)
          var y: Int = 0
          while (y < sourceRaster.getHeight) {
            var x: Int = 0
            while (x < sourceRaster.getWidth) {
              val v = sourceRaster.getSampleDouble(x, y, band)
              dst.setSample(x, y, 0, v)
              x += 1
            }
            y += 1
          }
          (U._1, RasterWritable.toWritable(dst))
        })))

        metadata(SparkUtils.calculateMetadata(rasterRDD.get, zoom, meta.getDefaultValue(band),
          bounds = meta.getBounds, calcStats = false))
        true
      case _ =>
        throw new IOException("Can't work with input raster! Ouch!")
    }
  }

  override def readExternal(in: ObjectInput): Unit = {
    band = in.readInt()
  }

  override def writeExternal(out: ObjectOutput): Unit = {
    out.writeInt(band)
  }

  override def setup(job: JobArguments, conf: SparkConf): Boolean = true
  override def teardown(job: JobArguments, conf: SparkConf): Boolean = true

}


text:
X

X

X

X

X

X


text:
require 'spec_helper'

describe 'Core Extensions' do
  describe ActionView::Helpers::FormBuilder do
    it { ActionView::Helpers::FormBuilder.should include(Bootsy::FormBuilder) }
  end

  describe ActionView::Base do
    it { ActionView::Base.should include(Bootsy::FormHelper) }
  end
end


text:
require 'minitest/autorun'
require_relative '../lib/mongoid_encryption'

class MockModel
  include Mongoid::Document
end

Mongoid.configure do |config|
  config.connect_to 'test-mongoid_encryption'
end


text:
package com.rim.samples.device.openvgdemo;

import net.rim.device.api.ui.UiApplication;

/**
 * A sample application that demonstrates Open VG 1.1. The application draws
 * text, creates an image from an svg file containing paths, and creates an
 * animated image.
 */
public class OpenVGDemo extends UiApplication {
    /**
     * Entry point for the application
     *
     * @param args
     *            Command line parameters (not used)
     */
    public static void main(final String args[]) {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        final OpenVGDemo app = new OpenVGDemo();
        app.enterEventDispatcher();

    }

    /**
     * Creates a new OpenVGDemo object
     */
    public OpenVGDemo() {
        pushScreen(new OpenVGDemoScreen());
    }
}


text:
//! Tests for our own Debug implementation.
//!
//! The tests check against expected output. This may be a bit fragile, but it is likely OK for
//! actual use.

use prost::alloc::{format, string::String};

// Borrow some types from other places.
#[cfg(feature = "std")]
use crate::message_encoding::Basic;
use crate::message_encoding::BasicEnumeration;

/// Some real-life message
#[test]
#[cfg(feature = "std")]
fn basic() {
    let mut basic = Basic::default();
    assert_eq!(
        format!("{:?}", basic),
        "Basic { \
         int32: 0, \
         bools: [], \
         string: \"\", \
         optional_string: None, \
         enumeration: ZERO, \
         enumeration_map: {}, \
         string_map: {}, \
         enumeration_btree_map: {}, \
         string_btree_map: {}, \
         oneof: None \
         }"
    );
    basic
        .enumeration_map
        .insert(0, BasicEnumeration::TWO as i32);
    basic.enumeration = 42;
    assert_eq!(
        format!("{:?}", basic),
        "Basic { \
         int32: 0, \
         bools: [], \
         string: \"\", \
         optional_string: None, \
         enumeration: 42, \
         enumeration_map: {0: TWO}, \
         string_map: {}, \
         enumeration_btree_map: {}, \
         string_btree_map: {}, \
         oneof: None \
         }"
    );
}

/*
TODO(danburkert/prost#56):

/// A special case with a tuple struct
#[test]
fn tuple_struct() {
    #[derive(Clone, PartialEq, Message)]
    struct NewType(
        #[prost(enumeration="BasicEnumeration", tag="5")]
        i32,
    );
    assert_eq!(format!("{:?}", NewType(BasicEnumeration::TWO as i32)), "NewType(TWO)");
    assert_eq!(format!("{:?}", NewType(42)), "NewType(42)");
}
*/

#[derive(Clone, PartialEq, prost::Oneof)]
pub enum OneofWithEnum {
    #[prost(int32, tag = "8")]
    Int(i32),
    #[prost(string, tag = "9")]
    String(String),
    #[prost(enumeration = "BasicEnumeration", tag = "10")]
    Enumeration(i32),
}

#[derive(Clone, PartialEq, prost::Message)]
struct MessageWithOneof {
    #[prost(oneof = "OneofWithEnum", tags = "8, 9, 10")]
    of: Option<OneofWithEnum>,
}

/// Enumerations inside oneofs
#[test]
fn oneof_with_enum() {
    let msg = MessageWithOneof {
        of: Some(OneofWithEnum::Enumeration(BasicEnumeration::TWO as i32)),
    };
    assert_eq!(
        format!("{:?}", msg),
        "MessageWithOneof { of: Some(Enumeration(TWO)) }"
    );
}


text:
// ivector/ivector-extractor.h

// Copyright 2013-2014    Daniel Povey

// 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
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.

#ifndef KALDI_IVECTOR_IVECTOR_EXTRACTOR_H_
#define KALDI_IVECTOR_IVECTOR_EXTRACTOR_H_

#include <vector>
#include "base/kaldi-common.h"
#include "matrix/matrix-lib.h"
#include "gmm/model-common.h"
#include "gmm/diag-gmm.h"
#include "gmm/full-gmm.h"
#include "itf/options-itf.h"
#include "util/common-utils.h"
#include "thread/kaldi-mutex.h"
#include "hmm/posterior.h"

namespace kaldi {

// Note, throughout this file we use SGMM-type notation because
// that's what I'm comfortable with.
// Dimensions:
//  D is the feature dim (e.g. D = 60)
//  I is the number of Gaussians (e.g. I = 2048)
//  S is the ivector dim (e.g. S = 400)

// Options for estimating iVectors, during both training and test.  Note: the
// "acoustic_weight" is not read by any class declared in this header; it has to
// be applied by calling IvectorExtractorUtteranceStats::Scale() before
// obtaining the iVector.
struct IvectorEstimationOptions {
  double acoustic_weight;
  IvectorEstimationOptions() : acoustic_weight(1.0) {}
  void Register(OptionsItf *po) {
    po->Register("acoustic-weight", &acoustic_weight,
                 "Weight on part of auxf that involves the data (e.g. 0.2); "
                 "if this weight is small, the prior will have more effect.");
  }
};

class IvectorExtractor;
class IvectorExtractorComputeDerivedVarsClass;

/// These are the stats for a particular utterance, i.e. the sufficient stats
/// for estimating an iVector (if need_2nd_order_stats == true, we can also
/// estimate the variance of the model; these 2nd order stats are not needed if
/// we just need the iVector.
class IvectorExtractorUtteranceStats {
 public:
  IvectorExtractorUtteranceStats(int32 num_gauss, int32 feat_dim,
                                 bool need_2nd_order_stats)
      : gamma_(num_gauss), X_(num_gauss, feat_dim) {
    if (need_2nd_order_stats) {
      S_.resize(num_gauss);
      for (int32 i = 0; i < num_gauss; i++) S_[i].Resize(feat_dim);
    }
  }

  void AccStats(const MatrixBase<BaseFloat> &feats, const Posterior &post);

  void Scale(double scale);  // Used to apply acoustic scale.

 protected:
  friend class IvectorExtractor;
  friend class IvectorExtractorStats;
  Vector<double>
      gamma_;         // zeroth-order stats (summed posteriors), dimension [I]
  Matrix<double> X_;  // first-order stats, dimension [I][D]
  std::vector<SpMatrix<double> >
      S_;  // 2nd-order stats, dimension [I][D][D], if
           // required.
};

struct IvectorExtractorOptions {
  int ivector_dim;
  int num_iters;
  bool use_weights;
  IvectorExtractorOptions()
      : ivector_dim(400), num_iters(2), use_weights(true) {}
  void Register(OptionsItf *po) {
    po->Register("num-iters", &num_iters,
                 "Number of iterations in "
                 "iVector estimation (>1 needed due to weights)");
    po->Register("ivector-dim", &ivector_dim, "Dimension of iVector");
    po->Register("use-weights", &use_weights,
                 "If true, regress the "
                 "log-weights on the iVector");
  }
};

// Forward declaration.  This class is used together with IvectorExtractor to
// compute iVectors in an online way, so we can update the estimate efficiently
// as we add frames.
class OnlineIvectorEstimationStats;

// Caution: the IvectorExtractor is not the only thing required to get an
// ivector.  We also need to get posteriors from a GMM, typically a FullGmm.
// Typically these will be obtained in a process that involves using a DiagGmm
// for Gaussian selection, followed by getting posteriors from the FullGmm.  To
// keep track of these, we keep them all in the same directory,
// e.g. final.{ubm,dubm,ie}.

class IvectorExtractor {
 public:
  friend class IvectorExtractorStats;
  friend class OnlineIvectorEstimationStats;

  IvectorExtractor() : prior_offset_(0.0) {}

  IvectorExtractor(const IvectorExtractorOptions &opts, const FullGmm &fgmm);

  /// Gets the distribution over ivectors (or at least, a Gaussian approximation
  /// to it).  The output "var" may be NULL if you don't need it.  "mean", and
  /// "var", if present, must be the correct dimension (this->IvectorDim()).
  /// If you only need a point estimate of the iVector, get the mean only.
  void GetIvectorDistribution(const IvectorExtractorUtteranceStats &utt_stats,
                              VectorBase<double> *mean,
                              SpMatrix<double> *var) const;

  /// The distribution over iVectors, in our formulation, is not centered at
  /// zero; its first dimension has a nonzero offset.  This function returns
  /// that offset.
  double PriorOffset() const { return prior_offset_; }

  /// Returns the log-likelihood objective function, summed over frames,
  /// for this distribution of iVectors (a point distribution, if var == NULL).
  double GetAuxf(const IvectorExtractorUtteranceStats &utt_stats,
                 const VectorBase<double> &mean,
                 const SpMatrix<double> *var = NULL) const;

  /// Returns the data-dependent part of the log-likelihood objective function,
  /// summed over frames.  If variance pointer is NULL, uses point value.
  double GetAcousticAuxf(const IvectorExtractorUtteranceStats &utt_stats,
                         const VectorBase<double> &mean,
                         const SpMatrix<double> *var = NULL) const;

  /// Returns the prior-related part of the log-likelihood objective function.
  /// Note: if var != NULL, this quantity is a *probability*, otherwise it is
  /// a likelihood (and the corresponding probability is zero).
  double GetPriorAuxf(const VectorBase<double> &mean,
                      const SpMatrix<double> *var = NULL) const;

  /// This returns just the part of the acoustic auxf that relates to the
  /// variance of the utt_stats (i.e. which would be zero if the utt_stats had
  /// zero variance.  This does not depend on the iVector, it's included as an
  /// aid to debugging.  We can only get this if we stored the S statistics.  If
  /// not we assume the variance is generated from the model.
  double GetAcousticAuxfVariance(
      const IvectorExtractorUtteranceStats &utt_stats) const;

  /// This returns just the part of the acoustic auxf that relates to the
  /// speaker-dependent means (and how they differ from the data means).
  double GetAcousticAuxfMean(const IvectorExtractorUtteranceStats &utt_stats,
                             const VectorBase<double> &mean,
                             const SpMatrix<double> *var = NULL) const;

  /// This returns the part of the acoustic auxf that relates to the
  /// gconsts of the Gaussians.
  double GetAcousticAuxfGconst(
      const IvectorExtractorUtteranceStats &utt_stats) const;

  /// This returns the part of the acoustic auxf that relates to the
  /// Gaussian-specific weights.  (impacted by the iVector only if
  /// we are using w_).
  double GetAcousticAuxfWeight(const IvectorExtractorUtteranceStats &utt_stats,
                               const VectorBase<double> &mean,
                               const SpMatrix<double> *var = NULL) const;

  /// Gets the linear and quadratic terms in the distribution over iVectors, but
  /// only the terms arising from the Gaussian means (i.e. not the weights
  /// or the priors).
  /// Setup is log p(x) \propto x^T linear -0.5 x^T quadratic x.
  /// This function *adds to* the output rather than setting it.
  void GetIvectorDistMean(const IvectorExtractorUtteranceStats &utt_stats,
                          VectorBase<double> *linear,
                          SpMatrix<double> *quadratic) const;

  /// Gets the linear and quadratic terms in the distribution over
  /// iVectors, that arise from the prior.  Adds to the outputs,
  /// rather than setting them.
  void GetIvectorDistPrior(const IvectorExtractorUtteranceStats &utt_stats,
                           VectorBase<double> *linear,
                           SpMatrix<double> *quadratic) const;

  /// Gets the linear and quadratic terms in the distribution over
  /// iVectors, that arise from the weights (if applicable).  The
  /// "mean" parameter is the iVector point that we compute
  /// the expansion around (it's a quadratic approximation of a
  /// nonlinear function, but with a "safety factor" (the "max" stuff).
  /// Adds to the outputs, rather than setting them.
  void GetIvectorDistWeight(const IvectorExtractorUtteranceStats &utt_stats,
                            const VectorBase<double> &mean,
                            VectorBase<double> *linear,
                            SpMatrix<double> *quadratic) const;

  // Note: the function GetStats no longer exists due to code refactoring.
  // Instead of this->GetStats(feats, posterior, &utt_stats), call
  // utt_stats.AccStats(feats, posterior).

  int32 FeatDim() const;
  int32 IvectorDim() const;
  int32 NumGauss() const;
  bool IvectorDependentWeights() const { return w_.NumRows() != 0; }

  void Write(std::ostream &os, bool binary) const;
  void Read(std::istream &is, bool binary);

  // Note: we allow the default assignment and copy operators
  // because they do what we want.
 protected:
  void ComputeDerivedVars();
  void ComputeDerivedVars(int32 i);
  friend class IvectorExtractorComputeDerivedVarsClass;

  // Imagine we'll project the iVectors with transformation T, so apply T^{-1}
  // where necessary to keep the model equivalent.  Used to keep unit variance
  // (like prior re-estimation).
  void TransformIvectors(const MatrixBase<double> &T, double new_prior_offset);

  /// Weight projection vectors, if used.  Dimension is [I][S]
  Matrix<double> w_;

  /// If we are not using weight-projection vectors, stores the Gaussian mixture
  /// weights from the UBM.  This does not affect the iVector; it is only useful
  /// as a way of making sure the log-probs are comparable between systems with
  /// and without weight projection matrices.
  Vector<double> w_vec_;

  /// Ivector-subspace projection matrices, dimension is [I][D][S].
  /// The I'th matrix projects from ivector-space to Gaussian mean.
  /// There is no mean offset to add-- we deal with it by having
  /// a prior with a nonzero mean.
  std::vector<Matrix<double> > M_;

  /// Inverse variances of speaker-adapted model, dimension [I][D][D].
  std::vector<SpMatrix<double> > Sigma_inv_;

  /// 1st dim of the prior over the ivector has an offset, so it is not zero.
  /// This is used to handle the global offset of the speaker-adapted means in a
  /// simple way.
  double prior_offset_;

  // Below are *derived variables* that can be computed from the
  // variables above.

  /// The constant term in the log-likelihood of each Gaussian (not
  /// counting any weight).
  Vector<double> gconsts_;

  /// U_i = M_i^T \Sigma_i^{-1} M_i is a quantity that comes up
  /// in ivector estimation.  This is conceptually a
  /// std::vector<SpMatrix<double> >, but we store the packed-data
  /// in the rows of a matrix, which gives us an efficiency
  /// improvement (we can use matrix-multiplies).
  Matrix<double> U_;

  /// The product of Sigma_inv_[i] with M_[i].
  std::vector<Matrix<double> > Sigma_inv_M_;

 private:
  // var <-- quadratic_term^{-1}, but done carefully, first flooring eigenvalues
  // of quadratic_term to 1.0, which mathematically is the least they can be,
  // due to the prior term.
  static void InvertWithFlooring(const SpMatrix<double> &quadratic_term,
                                 SpMatrix<double> *var);
};

/**
   This class helps us to efficiently estimate iVectors in situations where the
   data is coming in frame by frame.
 */
class OnlineIvectorEstimationStats {
 public:
  OnlineIvectorEstimationStats(int32 ivector_dim, BaseFloat prior_offset);

  OnlineIvectorEstimationStats(const OnlineIvectorEstimationStats &other);

  void AccStats(const IvectorExtractor &extractor,
                const VectorBase<BaseFloat> &feature,
                const std::vector<std::pair<int32, BaseFloat> > &gauss_post);

  int32 IvectorDim() const { return linear_term_.Dim(); }

  /// This function gets the current estimate of the iVector.  Internally it
  /// does some work to compute it (currently matrix inversion, but we are doing
  /// to use Conjugate Gradient which will increase the speed).  At entry,
  /// "ivector" must be a pointer to a vector dimension IvectorDim(), and free
  /// of NaN's.  For faster estimation, you can set "num_cg_iters" to some value
  /// > 0, which will limit how many iterations of conjugate gradient we use to
  /// re-estimate the iVector; in this case, you should make sure *ivector is
  /// set at entry to a recently estimated iVector from the same utterance,
  /// which will give the CG a better starting point.
  /// If num_cg_iters is set to -1, it will compute the iVector exactly; if it's
  /// set to a positive number, the number of conjugate gradient iterations will
  /// be limited to that number.  Note: the iVectors output still have a nonzero
  /// mean (first dim offset by PriorOffset()).
  void GetIvector(int32 num_cg_iters, VectorBase<double> *ivector) const;

  double NumFrames() const { return num_frames_; }

  double PriorOffset() const { return prior_offset_; }

  /// ObjfChange returns the change in objective function per frame from
  /// using the default value [ prior_offset_, 0, 0, ... ] to
  /// using the provided value; should be >= 0, if "ivector" is
  /// a value we estimated.  This is for diagnostics.
  double ObjfChange(const VectorBase<double> &ivector) const;

  double Count() const { return num_frames_; }

  /// Scales the number of frames of stats by 0 <= scale <= 1, to make it
  /// as if we had fewer frames of adaptation data.  Note: it does not
  /// apply the scaling to the prior term.
  void Scale(double scale);

  // Use the default assignment operator
 protected:
  /// Returns objective function per frame, at this iVector value.
  double Objf(const VectorBase<double> &ivector) const;

  /// Returns objective function evaluated at the point
  /// [ prior_offset_, 0, 0, 0, ... ]... this is used in diagnostics.
  double DefaultObjf() const;

  friend class IvectorExtractor;
  double prior_offset_;
  double num_frames_;  // num frames (weighted, if applicable).
  SpMatrix<double> quadratic_term_;
  Vector<double> linear_term_;
};

// This code obtains periodically (for each "ivector_period" frames, e.g. 10
// frames), an estimate of the iVector including all frames up to that point.
// This emulates what you could do in an online/streaming algorithm; its use
// is for neural network training in a way that's matched to online decoding.
// Caution: this program outputs the raw iVectors, where the first component
// will generally be very positive.  You probably want to subtract PriorOffset()
// from the first element of each row of the output before writing it out.
// For num_cg_iters, we suggest 15.  It can be a positive number (more -> more
// exact, less -> faster), or if it's negative it will do the optimization
// exactly each time which is slower.
// It returns the objective function improvement per frame from the "default"
// iVector to
// the last iVector estimated.
double EstimateIvectorsOnline(const Matrix<BaseFloat> &feats,
                              const Posterior &post,
                              const IvectorExtractor &extractor,
                              int32 ivector_period, int32 num_cg_iters,
                              Matrix<BaseFloat> *ivectors);

/// Options for IvectorExtractorStats, which is used to update the parameters of
/// IvectorExtractor.
struct IvectorExtractorStatsOptions {
  bool update_variances;
  bool compute_auxf;
  int32 num_samples_for_weights;
  int cache_size;

  IvectorExtractorStatsOptions()
      : update_variances(true),
        compute_auxf(true),
        num_samples_for_weights(10),
        cache_size(100) {}
  void Register(OptionsItf *po) {
    po->Register("update-variances", &update_variances,
                 "If true, update the "
                 "Gaussian variances");
    po->Register("compute-auxf", &compute_auxf,
                 "If true, compute the "
                 "auxiliary functions on training data; can be used to "
                 "debug and check convergence.");
    po->Register("num-samples-for-weights", &num_samples_for_weights,
                 "Number of samples from iVector distribution to use "
                 "for accumulating stats for weight update.  Must be >1");
    po->Register("cache-size", &cache_size,
                 "Size of an internal "
                 "cache (not critical, only affects speed/memory)");
  }
};

/// Options for training the IvectorExtractor, e.g. variance flooring.
struct IvectorExtractorEstimationOptions {
  double variance_floor_factor;
  double gaussian_min_count;
  int32 num_threads;
  IvectorExtractorEstimationOptions()
      : variance_floor_factor(0.1), gaussian_min_count(100.0) {}
  void Register(OptionsItf *po) {
    po->Register(
        "variance-floor-factor", &variance_floor_factor,
        "Factor that determines variance flooring (we floor each covar "
        "to this times global average covariance");
    po->Register("gaussian-min-count", &gaussian_min_count,
                 "Minimum total count per Gaussian, below which we refuse to "
                 "update any associated parameters.");
  }
};

class IvectorExtractorUpdateProjectionClass;
class IvectorExtractorUpdateWeightClass;

/// IvectorExtractorStats is a class used to update the parameters of the
/// ivector extractor
class IvectorExtractorStats {
 public:
  friend class IvectorExtractor;

  IvectorExtractorStats()
      : tot_auxf_(0.0), R_num_cached_(0), num_ivectors_(0) {}

  IvectorExtractorStats(const IvectorExtractor &extractor,
                        const IvectorExtractorStatsOptions &stats_opts);

  void Add(const IvectorExtractorStats &other);

  void AccStatsForUtterance(const IvectorExtractor &extractor,
                            const MatrixBase<BaseFloat> &feats,
                            const Posterior &post);

  // This version (intended mainly for testing) works out the Gaussian
  // posteriors from the model.  Returns total log-like for feats, given
  // unadapted fgmm.  You'd want to add Gaussian pruning and preselection using
  // the diagonal, GMM, for speed, if you used this outside testing code.
  double AccStatsForUtterance(const IvectorExtractor &extractor,
                              const MatrixBase<BaseFloat> &feats,
                              const FullGmm &fgmm);

  void Read(std::istream &is, bool binary, bool add = false);

  void Write(std::ostream &os,
             bool binary);  // non-const version; relates to cache.

  // const version of Write; may use extra memory if we have stuff cached
  void Write(std::ostream &os, bool binary) const;

  /// Returns the objf improvement per frame.
  double Update(const IvectorExtractorEstimationOptions &opts,
                IvectorExtractor *extractor) const;

  double AuxfPerFrame() { return tot_auxf_ / gamma_.Sum(); }

  // Copy constructor.
  explicit IvectorExtractorStats(const IvectorExtractorStats &other);

 protected:
  friend class IvectorExtractorUpdateProjectionClass;
  friend class IvectorExtractorUpdateWeightClass;

  // This is called by AccStatsForUtterance
  void CommitStatsForUtterance(const IvectorExtractor &extractor,
                               const IvectorExtractorUtteranceStats &utt_stats);

  /// This is called by CommitStatsForUtterance.  We commit the stats
  /// used to update the M matrix.
  void CommitStatsForM(const IvectorExtractor &extractor,
                       const IvectorExtractorUtteranceStats &utt_stats,
                       const VectorBase<double> &ivec_mean,
                       const SpMatrix<double> &ivec_var);

  /// Flushes the cache for the R_ stats.
  void FlushCache();

  /// Commit the stats used to update the variance.
  void CommitStatsForSigma(const IvectorExtractor &extractor,
                           const IvectorExtractorUtteranceStats &utt_stats);

  /// Commit the stats used to update the weight-projection w_-- this one
  /// takes a point sample, it's called from CommitStatsForW().
  void CommitStatsForWPoint(const IvectorExtractor &extractor,
                            const IvectorExtractorUtteranceStats &utt_stats,
                            const VectorBase<double> &ivector, double weight);

  /// Commit the stats used to update the weight-projection w_.
  void CommitStatsForW(const IvectorExtractor &extractor,
                       const IvectorExtractorUtteranceStats &utt_stats,
                       const VectorBase<double> &ivec_mean,
                       const SpMatrix<double> &ivec_var);

  /// Commit the stats used to update the prior distribution.
  void CommitStatsForPrior(const VectorBase<double> &ivec_mean,
                           const SpMatrix<double> &ivec_var);

  // Updates M.  Returns the objf improvement per frame.
  double UpdateProjections(const IvectorExtractorEstimationOptions &opts,
                           IvectorExtractor *extractor) const;

  // This internally called function returns the objf improvement
  // for this Gaussian index.  Updates one M.
  double UpdateProjection(const IvectorExtractorEstimationOptions &opts,
                          int32 gaussian, IvectorExtractor *extractor) const;

  // Updates the weight projections.  Returns the objf improvement per
  // frame.
  double UpdateWeights(const IvectorExtractorEstimationOptions &opts,
                       IvectorExtractor *extractor) const;

  // Updates the weight projection for one Gaussian index.  Returns the objf
  // improvement for this index.
  double UpdateWeight(const IvectorExtractorEstimationOptions &opts,
                      int32 gaussian, IvectorExtractor *extractor) const;

  // Returns the objf improvement per frame.
  double UpdateVariances(const IvectorExtractorEstimationOptions &opts,
                         IvectorExtractor *extractor) const;

  // Updates the prior; returns obj improvement per frame.
  double UpdatePrior(const IvectorExtractorEstimationOptions &opts,
                     IvectorExtractor *extractor) const;

  // Called from UpdatePrior, separating out some code that
  // computes likelihood changes.
  double PriorDiagnostics(double old_prior_offset) const;

  void CheckDims(const IvectorExtractor &extractor) const;

  IvectorExtractorStatsOptions config_;  /// Caution: if we read from disk, this
  /// is not recovered.  Options will not be
  /// used during the update phase anyway,
  /// so this should not matter.

  /// Total auxiliary function over the training data-- can be
  /// used to check convergence, etc.
  double tot_auxf_;

  /// This mutex guards gamma_ and Y_ (for multi-threaded
  /// update)
  Mutex gamma_Y_lock_;

  /// Total occupation count for each Gaussian index (zeroth-order stats)
  Vector<double> gamma_;

  /// Stats Y_i for estimating projections M.  Dimension is [I][D][S].  The
  /// linear term in M.
  std::vector<Matrix<double> > Y_;

  /// This mutex guards R_ (for multi-threaded update)
  Mutex R_lock_;

  /// R_i, quadratic term for ivector subspace (M matrix)estimation.  This is a
  /// kind of scatter of ivectors of training speakers, weighted by count for
  /// each Gaussian.  Conceptually vector<SpMatrix<double> >, but we store each
  /// SpMatrix as a row of R_.  Conceptually, the dim is [I][S][S]; the actual
  /// dim is [I][S*(S+1)/2].
  Matrix<double> R_;

  /// This mutex guards R_num_cached_, R_gamma_cache_, R_ivec_cache_ (for
  /// multi-threaded update)
  Mutex R_cache_lock_;

  /// To avoid too-frequent rank-1 update of R, which is slow, we cache some
  /// quantities here.
  int32 R_num_cached_;
  /// dimension: [num-to-cache][I]
  Matrix<double> R_gamma_cache_;
  /// dimension: [num-to-cache][S*(S+1)/2]
  Matrix<double> R_ivec_scatter_cache_;

  /// This mutex guards Q_ and G_ (for multi-threaded update)
  Mutex weight_stats_lock_;

  /// Q_ is like R_ (with same dimensions), except used for weight estimation;
  /// the scatter of ivectors is weighted by the coefficient of the quadratic
  /// term in the expansion for w (the "safe" one, with the max expression).
  Matrix<double> Q_;

  /// G_ is the linear term in the weight projection matrix w_.  It has the same
  /// dim as w_, i.e. [I][S]
  Matrix<double> G_;

  /// This mutex guards S_ (for multi-threaded update)
  Mutex variance_stats_lock_;

  /// S_{i}, raw second-order stats per Gaussian which we will use to update the
  /// variances Sigma_inv_.
  std::vector<SpMatrix<double> > S_;

  /// This mutex guards num_ivectors_, ivector_sum_ and ivector_scatter_ (for
  /// multi-threaded
  /// update)
  Mutex prior_stats_lock_;

  /// Count of the number of iVectors we trained on.   Need for prior
  /// re-estimation.
  /// (make it double not int64 to more easily support weighting later.)
  double num_ivectors_;

  /// Sum of all the iVector means.  Needed for prior re-estimation.
  Vector<double> ivector_sum_;

  /// Second-order stats for the iVectors.  Needed for prior re-estimation.
  SpMatrix<double> ivector_scatter_;

 private:
  IvectorExtractorStats &operator=(
      const IvectorExtractorStats &other);  // Disallow.
};

}  // namespace kaldi

#endif