Data.Structures.For.Game.Programmers.PART2.Basics.3.Array

1.It is a long one-dimensional structure containing numerous cells.Each cell can contain one item, and an index number is used to access each cell.(the first cell has an index of 0).
  Accessing items within an array is an O(c) algorithm.

2.MS VC++ sets all byte memory it uses to the hex value 0xcd when it is in debug mode.

3.int size = sizeof(array) / sizeof(int);

4.Dynamic Arrays
  by using malloc/calloc or new, both of them allocate memory on the heap.
array = (int*)malloc(sizeof(int) * 10);
  calloc goes one step further and resets every byte that it allocates to 0.
array = (int*)calloc(10, sizeof(int));

int* array = 0;
array = new int[10];

free(array);
delete[] array; // delete calls the destructor of every item in the array

array = (int*)realloc(array, 20 * sizeof(int));

Resizing a dynamic array created with new is a 3-step process:
1)Create a new array with the new size.
2)Copy over all possible data.
3)Delete the old array.

5.An array class
[The Data] think about the things you want in the array class.

template<class Datatype>
class Array
{
public:
 Datatype* m_array;
 int m_size;
};


[The Constructor] initialize the array.

Array(int p_size)
 {
  m_array = new Datatype[p_size];
  m_size = p_size;
 }

eg. Array<int> intarray(10);
[The Destructor]

~Array()
 {
  if (m_array != 0)
   delete[] m_array;
  m_array = 0;
 }

[Resize]

void Resize(int p_size)
 {
  Datatype* newarray = new Datatype[p_size];
  if (newarray == 0)
   return;
  int min;
  if (p_size < m_size)
   min = p_size;
  else
   min = m_size;
  int index;
  for (index = 0; index < min; index++)
   newarray[index] = m_array[index];
  m_size = p_size;
  if (m_array != 0)
   delete[] m_array;
  m_array = newarray;
 }

[Access Operator]

Datatype& operator[] (int p_index)
 {
  return m_array[p_index];
 }
// It returns a reference to a Datatype so that you can do sth like this:
// intarray[5] = 42;

[Conversion Operator]

operator Datatype* ()  // (Datatype*)Array
 {
  return m_array;
 }

[Inserting an Item]

void Insert(Datatype p_item, int p_index)
 {
  int index;
  for (index = m_size-1; index > p_index; index--)
   m_array[index] = m_array[index-1];
  m_array[p_index] = p_item;
 }

[Removing an Item]

void Remove(int p_index)
 {
  int index;
  for (index = p_index + 1; index < m_size; index++)
   m_array[index-1] = m_array[index]; 
 }


[A Faster Removal Method] when regardless of orders
count --;
intarray[3] = intarray[count];

[Size] return m_size

eg:

int main()
{
 Array<int> intarray(10);
 Array<float> floatarray(5);

 intarray[0] = 10;
 floatarray[0] = 3.1415f;

 int i = intarray[0];
 float f = floatarray[0];

 intarray[1] = 12;
 floatarray[1] = 6.28f;

 intarray.Insert(11, 1);
 floatarray.Insert(4.2f, 1);

 intarray.Remove(0);
 floatarray.Remove(0);

 intarray.Resize(3);
 floatarray.Resize(4);

 return 0;
}


 

[Writing an Array to Disk]

bool WriteFile(const char* p_filename)
{
 FILE* outfile = 0;
 int written = 0;
 outfile = fopen(p_filename, "wb");
 if (outfile == 0)
  return false;
 written = fwrite(m_array, sizeof(Datatype), m_size, outfile);
 fclose(outfile);
 if (written != m_size)
  return false;
 return true;
}


[Reading an Array to Disk]

bool ReadFile(const char* p_filename)
{
 FILE* infile = 0;
 int read = 0;
 infile = fopen(p_filename, "rb");
 if (infile == 0)
  return false;
 read = fread(m_array, sizeof(Datatype), m_size, infile);
 fclose(infile);
 if (read != m_size)
  return false;
 return true;
}


[A Better Insertion]

bool AddMonster()
{
 if (g_monsters == g_monsterarray.Size())
  g_monsterarray.Resize(g_monsterarray.Size() + 32);
 ...
}


[Cache Issues]
Looping through them in a single loop is nice and fast...However, an algorithm that randomly jumps to different cells that are far away from each other will be slow because you're causing the processor to move around a great deal of memory.

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值