⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 array.h

📁 Shorthand是一个强大的脚本语言
💻 H
字号:
#ifndef __array_h
#define __array_h
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/array.h 7     2/14/03 4:20a Arm $
//---------------------------------------------------------------------------
// This file is part of "libAndrix" library - a collection of classes
// and functions developed by Andrei Remenchuk.
//---------------------------------------------------------------------------
// While you may own complete copyright on the project with which you have
// received this file, the author reserves the right to use code contained
// in this very file for any purposes, including publishing and usage in
// any free or commercial software.
//
// You may re-distribute this file or re-use it in your own free or
// commercial software provided that this text is included in the file.
// If you change this file you must include clear notice stating that
// you changed this file and the date of change.
//
// This statement doesn't apply to other files that are part of the same
// package unless otherwise noted.
//---------------------------------------------------------------------------
// (c) 1998-2002 Andrei Remenchuk <andrei@remenchuk.com>
//---------------------------------------------------------------------------
// array.h - array template
/////////////////////////////////////////////////////////////////////////////


/**
 * Simple array that allows only insertion of objects (no deletion).
 * If do_delete is true, elements are destroyed when the array is destroyed
 * or array is cleared.
 */
template <class T, bool do_delete = true> 
class array
{
protected:
    int m_size;
    int m_capacity;
    T** m_items;
    bool m_delete;

public:

    array(int initial_capacity = 8)
    {
        m_size = 0;
        m_delete = do_delete;
        m_capacity = initial_capacity;
        if (m_capacity > 0)
            m_items = (T**) malloc (m_capacity * sizeof(T*));
        else
            m_items = (T**)0;
    }

    /**
     * adds item to the array
     */
    int add(T* item)
    {
        if (m_size >= m_capacity) {
            m_capacity += 10;
            if (m_items == (void*) 0) m_items = (T**) malloc (m_capacity * sizeof(T*));
            else m_items = (T**) realloc(m_items, m_capacity * sizeof(T*));
        }
        int index = m_size;
        m_items[m_size++] = item;
        return index;
    }

    /** 
     * Returns number of items in the array 
     */
    int size() const {  return m_size; }
    
    /** 
     * Reports whether or not the array is empty.
     */
    bool is_empty() { return m_size == 0; }

    /** 
     * Returns item at the specified index.
     * Return value is NULL if index is invalid.
     */
    T* get(int index) const
    {
        return (m_items) ? m_items[index] : 0;
    }

    /** 
     * Returns index of the specified item in this array by 
     * comparing physical pointers.
     * 
     * Return value is (-1) if item is not part of this array.
     */
    int index_of(T* item) const
    {
        for(int i=0; i<m_size; i++) {
            if (m_items[i] == item) return i;
        }
        return -1;
    }

    /**
     * Returns true if array contains specified item.
     */
    bool contains(T* item) { return (index_of(item) != -1); }

    /**
     * Replaces item in the array.
     */
    T* set(int index, T* item)
    {
        T* oldItem = m_items[index];
        m_items[index] = item;
        return oldItem;
    }

    /**
     * Resets item count and deletes non-null elements 
     * (if delete option was specified at construction time).
     * Doesn't free storage.
     */
    void clear()
    {
        // AR 02/13/2003: reversed deletion order to make sure
        // that interdependent objects are deleted in the
        // correct sequence
        for(int i=m_size-1; i>=0; i--) 
        {
            if (m_items[i] != (T*) 0) {
                if (m_delete) { delete (m_items[i]); }
                m_items[i] = (T*) 0;
            }
        }
        m_size = 0;
    }

    ~array()
    {
        clear();
        if (m_items) free(m_items);
    }

};

#endif // __array_h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -