ARRAYLIST's C ++ template simple implementation in DOTNET

xiaoxiao2021-03-06  36

ARRAYLIST's C template simple implementation in DOTNET

The implementation of ArrayList in the DOTNET is that we are very convenient when using arrays. He provided us with a compiled array. This way we don't have to know the length of the array when you create an array. The following is a simple ArrayList that I implemented with C template.

The code is compiled with DEV-CPP4990 and KDevelop (Linux).

/ ************************************************** **********************************

ArrayList.h - Description

-------------------

Begin: Mon Jan 31 2005

Copyright: (c) 2005 by Silver Wang

Email:

*********************************************************** ************************ /

/ ************************************************** **********************************

* *

* This Program Is Free Software; You Can Redistribute IT and / or Modify *

* It Under The Terms of The GNU General Public License As Published By *

* The Free Software Foundation; Either Version 2 of the license, or *

* (At Your Option) Any Later Version. *

* *

*********************************************************** ************************ /

#ifndef arraylist_h

#define arraylist_h

#include

Using namespace std;

/ **

* @ Author Silver WANG

* /

// define a class to import a array list.

Template

Class arraylist

{

Public: // construcotrs

// Initialize the instance of the arraylist class.

ArrayList ()

{

_List = 0;

_size = 0;

}

// Initialize the instance of the arraylist class with a special count.

ArrayList (int Count)

{

_size = count;

_List = INITARRAY (_SIZE);

}

Public: // deStructors

// uninitialize the installation of the arraylist class.

~ Arraylist ()

{

IF (_list! = 0)

Delete [] _List;

_List = 0;

_size = 0;

}

Public: //Methods

// Gets the size of the array.

int size ()

{

Return_size;

}

// sets the value at specail index.

Bool SetItem (int index, object value)

{

// Index Out of Range

IF (INDEX <0 || index> = _size)

Return False;

// SET VALUE

_List [index] = value;

Return True;

}

// Gets The Value At Special Index.

Object GetItem (int INDEX)

{

// Index Out of Range

IF (INDEX <0 || index> = _size)

Return 0;

Return_List [index];

}

// Add a item at the tail of array with a special value.

Void Add (Object Value)

{

// if Array Has Been Created.

IF (_list! = 0)

{

// CREATE A TEMP Array Which size is _size 1.

Object * tmplist = INITARRAY (_Size 1);

// copy the data from _list to temp Array Memory.

Memcpy (TMPLIST, _LIST, _SIZE);

// set Special Value at the end of arrrey.

TMPLIST [_SIZE] = Value;

// Clear Old Array Memory.

Delete [] _List;

// Point _List to TMPList's Memory.

_List = TMPLIST;

// modify the size of the current array.

_size = 1;

}

Else // Array Has Not Been Created.

{

// Create The Array Which Only Have ONE ITEM.

_List = INITARRAY (1);

// SET VALUE.

_List [0] = Value;

// set array size.

_size = 1;

}

}

// Remove The item from Special Index.

Bool Removeat (Int index)

{

Return RemoveRange (INDEX, 1);

}

// INSERT A ITEM AT Special Index.

BOOL INSERTAT (Int Index, Object Value)

{

Bool succeed = true;

// INSERT A Empty item to array.

Succeed = INSERTRANGE (INDEX, 1);

// if INSERT FAILED, RETURN FALSE.

IF (! successmed)

Return False;

// SET VALUE

_List [index] = value;

Return True;

}

// INSERT A Continue Space Into Array At Special Index.Bool InsertRange (int index, int count)

{

// Index Out of Range

IF (INDEX <0 || index> _size)

Return False;

IF (_list! = 0)

{

// Create a new array whose count equals to _count count

Object * tmplist = initarray (_size count);

IF (INDEX == _size)

{

// if index == _count, add the == _count, add the Range to the tail of arr

Memcpy (TMPLIST, _LIST, _SIZE);

}

Else

{

// if index> = 0 || Index <_count, insert the Range to Array

Memcpy (TMPLIST, _LIST, INDEX);

Memcpy (TMPLIST INDEX COUNT,

_List index,

(_SIZE - INDEX) * SIZEOF (OBJECT));

}

// Clear the memory of the old array.

Delete [] _List;

// Point the _List to the new memory of current array.

_List = TMPLIST;

// modify the array count.

_size = count;

}

Else

{

// CRAETE A New Array with special count.

_List = INITARRAY (country);

_size = count;

}

Return True;

}

Bool RemoveRange (int instex, int count)

{

IF (_list == 0 || index <0 || index> = _size)

Return False;

// Create the New Array Whose Count Equals to _Count - Count

Object * tmplist =initaray (_size - count);

// Copy The items of front of index to new array memory.

Memcpy (TMPLIST, _LIST, INDEX);

// if the part of removed is include in Array, Copy The items of behind of index count

// TO New Array Memory.

IF (INDEX Count <_size)

Memcpy (TMPLIST INDEX, _LIST INDEX Count, (_Size - INDEX - Count) * sizeof (Object));

// Clear the memory of old array.

Delete [] _List;

// Point the _list to new array memory.

_List = TMPLIST;

// if Index Count Is More Than Length of old array, size of new array is index.if (Index Count> = _size)

_size = index;

Else // OtherWise, Size Equals Old Size Sub Count.

_size - = count;

Return True;

}

Private: // Instance Data

// define the pointer which point to memory of arrrey.

Object * _list;

// save the size of arrrey.

INT _SIZE;

PRIVATE: //Methods

// Create a New Array Which Type is Object and Initialize Each Item With 0.

Object * InitaRray (int COUNT)

{

// CRAETE New Array

Object * array = new object [count];

// Initialize Each Item.

For (int index = 0; index

Array [INDEX] = 0;

Return array;

}

}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-62036.html

New Post(0)