/*
Copyright (c) 2015 Timur Gafarov

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

// dimage is actually stripped out part of dlib - just to support reading PNG and JPEG
module dimage.array; //dlib.container.array

import dimage.memory;

/*
 * GC-free dynamic array implementation.
 * Very efficient for small-sized arrays.
 */

struct DynamicArray(T, size_t chunkSize = 32)
{
    T[chunkSize] staticStorage;
    T[] dynamicStorage;
    uint numChunks = 0;
    uint pos = 0;

    T* storage()
    {
        if (numChunks == 0)
            return staticStorage.ptr;
        else
            return dynamicStorage.ptr;
    }

    void addChunk()
    {
        if (numChunks == 0)
        {
            dynamicStorage = New!(T[])(chunkSize);
        }
        else
        {
            reallocateArray(
                dynamicStorage,
                dynamicStorage.length + chunkSize);
        }
        numChunks++;
    }

    void shiftRight()
    {
        append(T.init);

        for(uint i = pos-1; i > 0; i--)
        {
            storage[i] = storage[i-1];
        }
    }

    void shiftLeft(uint n)
    {
        for(uint i = 0; i < pos; i++)
        {
            if (n + i < pos)
                storage[i] = storage[n + i];
            else
                storage[i] = T.init;
        }
    }

    void append(T c)
    {
        if (numChunks == 0)
        {
            staticStorage[pos] = c;
            pos++;
            if (pos == chunkSize)
            {
                addChunk();
                foreach(i, ref v; dynamicStorage)
                    v = staticStorage[i];
            }
        }
        else
        {
            if (pos == dynamicStorage.length)
                addChunk();

            dynamicStorage[pos] = c;
            pos++;
        }
    }

    void appendLeft(T c)
    {
        shiftRight();
        storage[0] = c;
    }

    void append(const(T)[] s)
    {
        foreach(c; s)
            append(cast(T)c);
    }

    void appendLeft(const(T)[] s)
    {
        foreach(c; s)
            appendLeft(cast(T)c);
    }

    auto opCatAssign(T c)
    {
        append(c);
        return this;
    }

    auto opCatAssign(const(T)[] s)
    {
        append(s);
        return this;
    }

    uint remove(uint n)
    {
        if (pos == n)
        {
            pos = 0;
            return n;
        }
        else if (pos >= n)
        {
            pos -= n;
            return n;
        }
        else
        {
            n = pos;
            pos = 0;
            return n;
        }
    }

    uint removeLeft(uint n)
    {
        if (pos == n)
        {
            pos = 0;
            return n;
        }
        else if (pos > n)
        {
            shiftLeft(n);
            pos -= n;
            return n;
        }
        else
        {
            n = pos;
            pos = 0;
            return n;
        }
    }

    size_t length()
    {
        return pos;
    }

    T[] data()
    {
        return storage[0..pos];
    }

    T opIndex(size_t index)
    {
        return data[index];
    }

    T opIndexAssign(T t, size_t index)
    {
        data[index] = t;
        return t;
    }

    int opApply(int delegate(size_t i, ref T) dg)
    {
        foreach(i, ref v; data)
        {
            dg(i, v);
        }

        return 0;
    }

    int opApply(int delegate(ref T) dg)
    {
        foreach(i, ref v; data)
        {
            dg(v);
        }

        return 0;
    }

    void free()
    {
        if (dynamicStorage.length)
            Delete(dynamicStorage);
        numChunks = 0;
        pos = 0;
    }
}

void reallocateArray(T)(ref T[] buffer, size_t len)
{
    T[] buffer2 = New!(T[])(len);
    for(uint i = 0; i < buffer2.length; i++)
        if (i < buffer.length)
            buffer2[i] = buffer[i];
    Delete(buffer);
    buffer = buffer2;
}

