Linear table (smart pointer implementation)

Source: Internet
Author: User

# Ifndef list_h
# Define list_h

# Include <exception>
Using namespace STD;

// Exception class. This exception is thrown when the queried index is incorrect.
Class badindex: Public exception
{
Public:
Virtual const char * What () const throw ()
{
Return "the index location you entered is incorrect ";
}
};

Template <typename T>
Class list;

// Counting class pointer, used in list. If no replication control is defined, the default one meets the requirements.
Template <typename P>
Class listrep
{
PRIVATE:
P * m_rep;
Int m_usecount;
Friend class list <p>;
Listrep (p * REP );
~ Listrep ();
// Increase the count by one
Void increment ();
// Reduce the Count usage by one. When the Count usage is reduced to 0, delete the resource pointed to by the pointer.
// The object itself cannot be deleted, and an error will occur.
Void decrement ();
// Copy the resource pointed to by one pointer to another
// This is mainly used to insert, delete, and add data to the list.
// When performing these operations, you cannot change the unit with multiple pointers.
// A pointer points to it, so you need to copy a single copy for operation.
Void copy (const listrep * LR, int size );
};

Template <typename P>
Void listrep <p>: Copy (const listrep * LR, int size) // do not check the size of the target range
{
For (INT I = 0; I! = Size; ++ I)
(This-> m_rep) [I] = (LR-> m_rep) [I];
}
Template <typename P>
Listrep <p>: listrep (p * REP): m_rep (REP), m_usecount (1) // you cannot declare the pointer of a parameter as Const.
{}

Template <typename P>
Inline void listrep <p >:: increment ()
{
++ M_usecount;
}

Template <typename P>
Inline void listrep <p>: decrement ()
{
If (-- m_usecount = 0)
Delete [] m_rep;
}

Template <typename P>
Listrep <p> ::~ Listrep () // The template parameter is missing
{
Delete [] m_rep;
}

Template <typename T>
Class list
{
PRIVATE:
Int m_maxsize;
Listrep <t> * m_ptr;
Int m_size;

// Check whether an index is the correct position in the table. If the table is empty, any position is incorrect.
Bool rightpo (INT po) const;
// Insert the data Val at a certain position in the table. This function is also used in push.
Void sort list (INT Po, const T & Val );

Public:
// Construct a linear table with a specified length. If the length is not given, the table has a default value.
Explicit list (INT maxsize = 5 );

// Copy data from the source table to the target table
List (const list & L );

// You do not need to specify a parameter in the template class to treat the class name as a type name.
// However, you must specify the class parameter as the return value.
Const list <t> & operator = (const list & L );

// The two tables have the same length and contain the same data.
// And the data position in the table has a one-to-one correspondence. The two tables are the same.
Bool operator = (const list & L) const;

// Use =
Bool Operator! = (Const list & L) const;

// Clear the table and delete its original resources
Void clear ();

// Whether the table is empty
Bool empty () const;

// Return the table length
Int listlengh () const;

// Return the data at a certain position. If the location is correct, use t to return the data and return true. If the location is incorrect
// The value of T is defined and false is returned.
T getelem (INT po) const;

// Insert a data entry before a certain position. If this position is not the correct position in the table
// No data is inserted into the table.
Void insert (INT Po, const T & Val );

// Delete the data at a location. If the location is not a correct location, no deletion operation is performed.
Void erase (INT po );

// Press data into the table
Void push (const T & Val );
};

Template <typename T>
Bool list <t>: rightpo (INT po) const
{
If (m_size = 0)
Return false;
Else
{
If (PO <0 | Po> = m_size)
Return false;
Else
Return true;
}
}

Template <typename T>
Void list <t >:: unknown list (INT Po, const T & Val)
{
If (m_size> m_maxsize)
M_maxsize * = 2;
Listrep <t> * P = new listrep <t> (New T [m_maxsize]);
P-> copy (m_ptr, m_size-1 );
M_ptr-> decrement ();
M_ptr = P;
 
T * FP;
T * sp;
Fp = (m_ptr-> m_rep) + m_size-1;
SP = FP-1;

For (; FP! = (M_ptr-> m_rep) + Po; -- SP, -- FP)
* Fp = * sp;
* Fp = val;
}

Template <typename T>
List <t>: List (INT maxsize): m_maxsize (maxsize), m_ptr (New listrep <t> (New T [m_maxsize])
{
M_size = 0;
}

Template <typename T>
List <t>: List (const list & L)
{
M_maxsize = L. m_maxsize;
M_size = L. m_size;
M_ptr = L. m_ptr;
M_ptr-> increment ();
}

Template <typename T>
Const list <t> & list <t>: Operator = (const list & L)
{
If (this! = & L)
{
M_size = L. m_size;
M_maxsize = L. maxsize;
M_ptr-> decrement ();
M_ptr = L. m_ptr;
M_ptr-> increment ();
}
Return * this;
}

Template <typename T>
Bool list <t>: Operator = (const list & L) const
{
If (m_size = L. m_size)
{
If (m_ptr = L. m_ptr)
Return true;
}
Return false;
}

Template <typename T>
Bool list <t >:: Operator! = (Const list & L) const
{
Return! (* This = L );
}

Template <typename T>
Void list <t>: clear ()
{
M_size = 0;
M_maxsize = 5;
M_ptr-> decrement ();
M_ptr = new listrep <t> (New T [m_maxsize]); // when using the template class for Dynamic Allocation
// Remember to include parameters
}

Template <typename T>
Inline bool list <t >:: empty () const
{
Return m_size = 0;
}

Template <typename T>
Inline int list <t >:: listlengh () const
{
Return m_size;
}

Template <typename T>
T list <t >:: getelem (INT po) const
{
If (rightpo (PO ))
Return (m_ptr-> m_rep) [po];
Else
Throw badindex ();
}

Template <typename T>
Void list <t >:: insert (INT Po, const T & Val)
{
If (rightpo (PO ))
{
+ M_size;
Sorted list (PO, Val );
}
Else
Throw badindex ();
}

Template <typename T>
Void list <t>: erase (INT Po)
{
If (rightpo (PO ))
{
Listrep <t> * P = new listrep <t> (New T [m_size]);
P-> copy (m_ptr, m_size );
M_ptr-> decrement ();
M_ptr = P;
T * FP;
T * sp;
Fp = (m_ptr-> m_rep) + Po;
SP = FP + 1;
-- M_size;

For (; FP! = (M_ptr-> m_rep) + m_size; ++ FP, ++ SP)
* Fp = * sp;
}
Else
Throw badindex ();
}

Template <typename T>
Void list <t>: Push (const T & Val)
{
If (m_size + 1 <= m_maxsize)
{
(M_ptr-> m_rep) [m_size] = val;
+ M_size;
}
Else
{
+ M_size;
Struct list (m_size, Val );
}
}
# Endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.