sharray.cpp
来自「Shorthand是一个强大的脚本语言」· C++ 代码 · 共 114 行
CPP
114 行
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/sharray.cpp 2 2/08/03 6:40a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// sharray.cpp: ShortHand array class - implementation
///////////////////////////////////////////////////////////////////////////////
#include "sharray.h"
#include "except.h"
#include <stdlib.h>
ShhArray::ShhArray(ShhModule& module, const YYLTYPE& location)
: ShhObject(module, "ARRAY", location)
{
m_count = 0;
m_capacity = 0;
m_items = NULL;
}
ShhArray::~ShhArray()
{
if (m_items != NULL)
{
free(m_items);
}
}
void ShhArray::constructor(ShhValueList* args)
{
}
bool ShhArray::hasMethod(const char* name)
{
return false;
}
ShhValue ShhArray::executeMethod(const char* name, ShhValueList* args)
{
return ShhValue::null();
}
ShhValue& ShhArray::getProperty(const char* name)
{
if (stricmp(name, "count") == 0)
{
m_count_property = m_count;
return m_count_property;
}
else
return ShhObject::getProperty(name);
}
void ShhArray::growTo(int n)
{
if (n > m_capacity)
{
int oldCount = m_count;
m_capacity = (n/32+1)*32;
if (m_items == NULL)
m_items = (ShhValue*) malloc(sizeof(ShhValue)*m_capacity);
else
{
m_items = (ShhValue*) realloc(m_items, sizeof(ShhValue)*m_capacity);
}
for(int i=oldCount; i<m_capacity; i++)
{
m_items[i] = ShhValue::EMPTY;
}
}
}
ShhValue& ShhArray::get(int index)
{
if (index >= 0 && index < m_count)
return m_items[index];
else
{
return m_null;
}
}
void ShhArray::set(int index, ShhValue value)
{
if (index >= 0)
{
growTo(index);
m_items[index] = value;
m_count = i_max(m_count, index+1);
}
}
int ShhArray::size() const
{
return m_count;
}
int ShhArray::setCount(int n)
{
if (n > m_count)
{
growTo(n);
}
else
{
for(int i=n; i<m_count; i++)
m_items[i] = ShhValue::EMPTY;
}
m_count = n;
return m_count;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?