/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2010 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstd
 * Version: 1.07
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstd library
 * 
 * mzstd library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#ifndef __MZSTD_ARRAY_LIST_H__
#define __MZSTD_ARRAY_LIST_H__

#include "mzstdCommon.h"
#include "mzObject.h"
#include "mzString.h"


class DECLSPEC_MZSTD CArrayList : public CObject {
public:
    WINAPI CArrayList(ULONG capacity = 100);
    virtual WINAPI ~CArrayList();

    VOID WINAPI append(LONG data);
    inline VOID WINAPI push(LONG data) { this->append(data); };
    VOID WINAPI addAsFirst(LONG data);
    VOID WINAPI insert(ULONG pos, LONG data);
    ULONG WINAPI del(LONG data);
    BOOL WINAPI delFirst(LONG data);
    BOOL WINAPI delAt(ULONG pos);
    LONG WINAPI pos(LONG data);
    BOOL WINAPI contains(LONG data);
    LONG WINAPI getMinValue();
    LONG WINAPI getMaxValue();
    // Bubble-sort (default in ascending order)
    VOID WINAPI sort(BOOL asc = TRUE);
    VOID WINAPI insertRandomValues(ULONG count, ULONG minValue, ULONG maxValue);
    // Makes all values are set randomed
    VOID WINAPI randomizeAll();
    VOID WINAPI randomizeAll(ULONG minValue, ULONG maxValue);
    // as methods: randomizeAll() and randomizeAll(ULONG minValue, ULONG maxValue) but first
    // makes this->_size = this->_capacity
    VOID WINAPI randomizeAllCapacity();
    VOID WINAPI randomizeAllCapacity(ULONG minValue, ULONG maxValue);
    // Finds min value that is greater than the this->getMinValue() and is not in the array
    LONG WINAPI getFirstAvailableValue();
    inline LONG WINAPI getAt(ULONG pos) { if (pos < this->_size) return this->_items[pos]; return 0; };
    VOID WINAPI setAt(ULONG pos, LONG data);
    inline LONG WINAPI getFirst() { if (this->_size > 0) return this->_items[0]; return 0; };
    inline LONG WINAPI getLast() { if (this->_size > 0) return this->_items[this->_size - 1]; return 0; };
    inline LONG WINAPI pop() {
        LONG retval;
        if (this->_size < 1) { return 0; }
        retval = this->getAt(this->_size - 1);
        this->delAt(this->_size - 1);
        return retval;
    }
    inline ULONG WINAPI size() { return this->_size; };
    inline ULONG WINAPI capacity() { return this->_capacity; };
    virtual VOID WINAPI clear();
    inline LONG* WINAPI getItemsBuf() { return this->_items; }

    CArrayList* WINAPI clone();
    virtual CString WINAPI toString();

protected:
    LONG* _items;
    ULONG _size;
    ULONG _capacity;

    LONG* WINAPI _createNullArray(ULONG capacity);
};

typedef CArrayList* PArrayList;

#endif
