A%20Generic%20Vector%20Class - PowerPoint PPT Presentation

About This Presentation
Title:

A%20Generic%20Vector%20Class

Description:

namespace rcl { // class definition #include 'tvector.cpp' // class ... using namespace rcl; Tvector int intVector(); Class TVector T template typename T ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 36
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: A%20Generic%20Vector%20Class


1
A Generic Vector Class
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Contact Information
  • Andy Wang (awang_at_cs.fsu.edu)
  • LOV 264
  • Office hours M 130pm 230pm
  • Th 200pm 300pm
  • Class website
  • http//www.cs.fsu.edu/awang/courses/cop4530_f200
    3

3
Idea A Generic Array
  • Of an arbitrary type (generic container)
  • A template class
  • Set and change the array size at runtime
  • Index bounds checking
  • Automatic memory management

4
TVectorltTgt
  • TVectorltintgt intVector(10)
  • TVectorltchargt charVector(30)
  • TVectorltpairltchargt gt charPairVector(20)
  • intVector.set_size(50)

5
TVectorltTgt Interface
  • Constructor(s)/destructor
  • Operators
  • Accessor functions
  • Set vector size
  • Set storage capacity (reserve memory)

6
TVectorltTgt Implementation
  • A protected C array to store the array content
  • Protected data members
  • Size // the number of used array elements
  • Capacity // total storage capacity
  • A protected method that allocates memory
  • Manage memory and data when appropriate

7
Code Layout
  • tvector.h
  • tvector.cpp

8
tvector.h
  • ifndef _TVECTOR_H
  • define _TVECTOR_H
  • include ltstdlib.hgt
  • include ltiostreamgt
  • namespace rcl
  • // class definition
  • include tvector.cpp // class implementation
  • endif // _TVECTOR_H

9
tvector.cpp
  • ifndef _TVECTOR_CPP // now a part of the
    tvector.h
  • define _TVECTOR_CPP
  • static const unsigned int default_capacity 10
  • // operator overloads
  • // public methods
  • // protected methods
  • endif // _TVECTOR_CPP
  • Scope file
  • Need to be initialized at the time of
    declaration
  • Cannot be changed

10
main.cpp
  • include tvector.h // which includes
    tvector.cpp
  • using namespace rcl
  • Tvectorltintgt intVector()

11
Class TVectorltTgt
  • template lttypename Tgt
  • class Tvector
  • public
  • // constructors/deconstructor
  • // operators
  • // accessor functions
  • protected
  • // data
  • // method
  • // operator overloads

12
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const

13
Class TVectorltTgt
  • // container class protocol
  • int Empty() const // return 1 if the array is
    empty
  • int PushBack(const T) // add an element to
    arraysize int PopBack() // remove the
    element from arraysize - 1
  • void Clear()
  • T Front() const
  • T Back() const
  • // generic display methods
  • void Display(ostream os, char ofs \0)
    const
  • void Dump(ostream os) const
  • protected
  • size_t size, capacity // array size and
    storage capacity
  • T content // pointer to array elements
  • static T newarray(size_t) // allocate memory
  • // operator overloads

14
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const
  • Implicit copies
  • value f()
  • f(value)

15
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const

16
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const

17
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const

Explicit copies
18
Class TVectorltTgt
  • template lttypename Tgt
  • class TVector
  • public
  • // constructors/deconstructor
  • TVector() // default constructor
  • TVector(size_t)
  • TVector(const TVectorltTgt) // copy constructor
  • TVector()
  • // operators
  • TVectorltTgt operator (const TVectorltTgt)
  • T operator (size_t) const
  • // sizing
  • int SetSize(size_t, const T)
  • int SetCapacity(size_t)
  • size_t Size() const
  • size_t Capacity() const

Returns a reference, so the element can be
modified on the left side of an assignment
19
Class TVectorltTgt
  • // container class protocol
  • int Empty() const // return 1 if the array is
    empty
  • int PushBack(const T) // add an element to
    arraysize int PopBack() // remove the
    element from arraysize - 1
  • void Clear() // clear the array
  • T Front() const // returns array0
  • T Back() const // returns arraysize 1
  • // generic display methods
  • void Display(ostream os, char ofs \0)
    const
  • void Dump(ostream os) const
  • protected
  • size_t size, capacity // array size and
    storage capacity
  • T content // pointer to array elements
  • static T newarray(size_t) // allocate memory
  • // operator overloads

Debugging Functions
20
Class TVectorltTgt
  • // container class protocol
  • int Empty() const // return 1 if the array is
    empty
  • int PushBack(const T) // add an element to
    arraysize int PopBack() // remove the
    element from arraysize - 1
  • void Clear() // clear the array
  • T Front() const // returns array0
  • T Back() const // returns arraysize 1
  • // generic display methods
  • void Display(ostream os, char ofs \0)
    const
  • void Dump(ostream os) const
  • protected
  • size_t size, capacity // array size and
    storage capacity
  • T content // pointer to array elements
  • static T newarray(size_t) // allocate memory
  • // operator overloads

Scope tvector.h and tvector.cpp
21
Operator Overloads

template lttypename Tgt ostream operatorltlt
(ostream os, const TVectorltTgt a) template
lttypename Tgt int operator (const TVectorltTgt,
const TVectorltTgt) template lttypename Tgt int
operator! (const TVectorltTgt, const
TVectorltTgt)
22
TVectorltTgt newarray()
  • template lttypename Tgt
  • T TVectorltTgtnewarray(size_t newcapacity)
  • T Tptr 0
  • if (newcapacity gt 0)
  • Tptr new Tnewcapacity
  • if (Tptr 0)
  • cerr ltlt memory allocation failed ltlt endl
  • return Tptr

A reusable routine called by constructor and size
functions
23
Constructors
  • template lttypename Tgt
  • TVectorltTgtTVector() size(0),
    capacity(default_capacity), content(0)
  • content newarray(capacity)
  • template lttypename Tgt
  • TVectorltTgtTVector(unsigned int sz) size(sz),
    capacity(sz)
  • content newarray(capacity)
  • template lttypename Tgt
  • TVectorltTgtTVector(const TVectorltTgt source)
    size(source.size), capacity(source.capacity)
  • content newarray(capacity)
  • for (size_t j 0 j lt size j) contentj
    source.contentj

24
Destructor
  • template lttypename Tgt
  • TVectorltTgtTVector()
  • delete content

25
Debugging Routines
  • template lttypename Tgt
  • void TVectorltTgtDisplay(ostream os, char ofc)
    const
  • size_t j
  • if (ofc \0)
  • for (j 0 j lt size j) os ltlt contentj
  • else
  • for (j 0 j lt size j) os ltlt contentj ltlt
    ofc

26
Debugging Routines (2)
  • template lttypename Tgt
  • void TVectorltTgtDump(ostream os) const
  • size_t j
  • if (size 0)
  • os ltlt ()
  • else
  • os ltlt (
  • for (j 1 j lt size j)
  • os ltlt , ltlt contentj
  • os ltlt )

27
Read Accessor Functions
  • template lttypename Tgt
  • size_t TVectorltTgtSize() const
  • return size
  • template lttypename Tgt
  • size_t TVectorltTgtCapacity() const
  • return capacity

28
Read Accessor Functions (2)
  • template lttypename Tgt
  • T TVectorltTgtFront() const
  • // report error and exit if size is zero
  • return content0
  • template lttypename Tgt
  • T TVectorltTgtBack() const
  • // report error and exit if size is zero
  • return contentsize - 1

29
Write Accessor Functions
  • template lttypename Tgt
  • int TVectorltTgtSetCapacity(size_t newcapacity)
  • if (newcapacity 0)
  • // delete content and set internal states to
    zero
  • return 1
  • if (newcapacity ! capacity)
  • T newcontent newarray(newcapacity)
  • if (newcontent 0) return 0
  • if (size gt newcapacity) size newcapacity
  • for (size_t j 0 j lt size j)
    newcontentj contentj
  • capacity newcapacity
  • delete content
  • content newcontent
  • return 1

30
Write Accessor Functions (2)
  • template lttypename Tgt
  • int TVectorltTgtSetSize(size_t newsize)
  • if (newsize gt capacity)
  • if (SetCapacity(newsize) 0)
  • return 0
  • size newsize
  • return 1
  • template lttypename Tgt
  • int TVectorltTgtClear()
  • SetSize(0)

31
Write Accessor Functions (3)
  • template lttypename Tgt
  • int TVectorltTgtPushBack(const T Tval)
  • // expand the content array when size gt capacity
  • // expand the content array by doubling
  • // call SetCapacity() to expand content
  • // special case when capacity is zerodoubling
    zero is zero!
  • // return 1 on success, 0 on failure
  • template lttypename Tgt
  • int TVectorltTgtPopBack()
  • if (size 0) return 0
  • --size return 1

32
Operators
  • template lttypename Tgt
  • TVectorltTgt TVectorltTgtoperator (const
    TVectorltTgt source)
  • if (this ! source)
  • if (source.capacity 0)
  • if (capacity gt 0) delete content
  • // set private data members to zero
  • return this
  • if (capacity ! source.capacity)
  • if (capacity gt 0) delete content
  • capacity source.capacity
  • content newarray(capacity)
  • size source.size
  • // copy content from source.content
  • return this

33
Operators (2)
  • template lttypename Tgt
  • T TVectorltTgtoperator (size_t j) const
  • if (j gt size)
  • cerr ltlt vector index out of range! ltlt endl
  • if (j gt capacity)
  • exit(EXIT_FAILURE)
  • return contentj

34
Operator Overloads
  • template lttypename Tgt
  • ostream operatorltlt (ostream os, const
    TVectorltTgt v)
  • v.Display(os)
  • return(os)
  • template lttypename Tgt
  • int operator!(const TVectorltTgt v1, const
    TVectorltTgt v2)
  • return !(v1 v2)

35
Operator Overloads (2)
  • template lttypename Tgt
  • int operator(const TVectorltTgt v1, const
    TVectorltTgt v2)
  • if (v1.Size() ! v2.Size()
  • return 0
  • for (size_t j 0 j lt v1.Size() j)
  • if (v1j ! v2j)
  • return 0
  • return 1
Write a Comment
User Comments (0)
About PowerShow.com