a comparison of data structures in c++ · pdf filea comparison of data structures in c++ wemg...

64
A COMPARISON OF DATA STRUCTURES IN C++ WEmG ZHOU A MAJOR FEPORT IN THE DEPARTMENT OF COMPUTER SCIENCE PRESENTED IN PARTIAL FULFaLMENT OF THE REQUlREMENTS FOR THE DEGREE OF MASTER OF COMPUTER SCIENCE CONCORDIA UMVERSITY MONTREAL, QUEBEC, CANADA

Upload: lelien

Post on 16-Mar-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

A COMPARISON OF DATA STRUCTURES IN C++

W E m G ZHOU

A MAJOR FEPORT

IN

THE DEPARTMENT

OF

COMPUTER SCIENCE

PRESENTED IN PARTIAL FULFaLMENT OF THE REQUlREMENTS

FOR THE DEGREE OF MASTER OF COMPUTER SCIENCE

CONCORDIA UMVERSITY

MONTREAL, QUEBEC, CANADA

Page 2: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Nationai Library 1+1 ,canada Biblioth ue nationale % du cana

Acquisiîions and Acquisitions et Bibliagraphic S e ~ b services bibliographiques

The author has granteci a non- L'auteur a accordé mie licence non exclusive licence dowhg the exchisive pennettant à la Nationai L i of Canada to Biblothèque nationale du Canada de reproduce, loan, distri'bute or seIl reproduire, prêter, distniuer ou copies of this thesis in inicrofonn, vendre des copies de cette thése sous papa or electronic formats. la forme de microfiche/nlm, de

reproduction sur papier ou sur foimat électroniqpe.

The a d o r retains ownership of the L'auteur conserve la propriété du copyright in this thesis. Neither the droit d'auteur qui protège cette thèse. thesis nor substantial extracts nom it Ni Ia thèse ni des extraits substantiels may be printed or otherwise de celle-ci ne doivent être imprllnés reproduced without the author's ou autrement reproduits sans son pemksion. autorisation.

Page 3: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

ABSTRACT

A Cornparison of Data Structures in C++

Weining Zhou

Since many C++ Libraries have become widely-used by programers. Pointers are one of

the rnost powerful and fi exible feahires used in CH. Pointers are paticularly important

for the andysis and design of data structures. The intention of this project is to compare

the two implementation methods, "Closed" and "Open" by using two Ctç Libmries. In

this project, I designed and implemented a phonebook application by using a doubly

Iinked list and a binary search m e as common data structures in the C++ libmrïes: Ctç

Standard Template Libnry (STL) and C++ Data Object Libnry (OrgC++). In order to

explore the performance of the two styles of building data structure librvies in C++, I

performed an objective cornparison of two implementation rnethods in tems of ease of

coding, time and space efficiency as well as the reliability. The applications were coded

in C+k

iii

Page 4: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Acknowledgments

Through many face-to-face discussions as well as exchmges of electronic mail, a number

of individucils have shaped my ideas on the project. For their contributions, fmt and

foremost, 1 gratehilly acknowledge the encoungement, assistance and valuabte opinions

of my supervisor, Dr. Peter Grogono. whose enthusiastic support and guidance enabbled

me to successfully understand and cornpiete the project on time. 1 dso thank Yang

Wang, Weidong Sun, Xieming Hao and, Zhm Zhang for their valuable feedback and

their help in finding ways to present the ideas more clearly. Thanks to Chunbo Yang for

helping to stnighten out the installation problems with the libnries. Thanks also to my

hiends David Wilkins and Alan Yu for theu encoungement dunng this project Finally,

thanks to my dog, MuEy, who kept me Company on many days and iate nights of doing

the project.

Page 5: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Table of Contents

1 . 1 OB JE^ ...........................................................................................................~.. 1

............................................................................................................ 1.2 MOTIVATION 2

........................................................................................ 1 -3 OUTLM OF THE ~ O J E C T 2

3 . EVALUA~ON CRITER& D ~ N ~ N .........................g......................................*........ 7

3.2 THE APPLICATION .................................................................................................... 9

3.3 THE DATA STRUCTURES .................................................. ............ !O

3.4 THE LIBRAR[ES ...........~.....................................................................~.....~..~............ I f

3.4.1 Standard Template Libtary (STL) ................................................................ 12 3.4.2 CH Data Object Library (OrgCtç) ............................................................ 13

35 TESTCORRECTNESS ............................................................................................... 14

3.6 CO~PARLNG TIME AMI SPACE EFFICIENCY ............................................................ 15

3.6.1 Caculating Time ........................................................................................... 16

3.6.2 Calculating Space .......................................................................................... 16

.............................................................. 3.6.3 Definition of Time and Space Ratio 16

4.1 ENVIRONMENT .............................................................g.................................. 1 8

4.2 ' r O ~ w m IMP~EUENTA~ON METHoD - ........,.................... .................................O... 19 4.2.2.1 Irnplementing a Daub1y Linked List ..........,........ ....................................... 20

Page 6: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

.............................................................. 4.2. 1 Implementing a Binary Search Tree 22

................................................................................. 4.3 l r ~ o ~ ~ ~ ' l ~IPLEMENTATION 23

............................................................ 4.3.1 Implementïng a Doubly Linked List 23

.............................................................. 4.3.2 Implementing a Binary Search Tree 26

4.4 USER INTERFACE .................................................................................................... 30

4.4.1 Suspected ProbIems with OrgCtk .............................................................. 30

4.4.2 U? for "Open" Implementation in S TL. ......................................................... 31

................................................. 4.4.3 Ui for 'Closeci" Implementation in OrgC ++ 35

5 . 1 USAEHLITY . EASE OF CODNG ................................................................................ 36

5.2 TlME AND SPACE .......................................................~.............~.........~~................... 38

5.2.1 Build Time Eficiency .........................~......*...........................*...................... 38

5 2.2 Access Time Efficiency ........................*....................................................... 39

5.2.3 Space Efficiency ............................................................................................ 42

5.3 LVTERA~ON w r r ~ MFC ..................................................................... 43

5.4 OTHER COMPAR~SONS ..........................~......................*~......................................... 43

6.1 THE ADVANTAGES OF STL AND ORGC tt .............................................................. 45

6.2 FUTURE WORK ....................................................................................................... 47

A . BOOKS AND ARTICLES ....................................................................*...................*... a B . ~ F O R M . T ~ O N ON THE WEB .................................................................................... 49

Page 7: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment
Page 8: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 1 Introduction

As we b o w , there are rnany C t t libraries that are used by programmers. But each

library works better in different situations. Data structures are of intenst since there are

some important performance problems in designing scientiftc class libdes. The

expenence of working with various data structures may offer some usefui insights about

performance of these data structures. In this section, 1 describe objectives of the project

and why 1 chose this topic. I aIso introduce the structure of this report.

1.1 Objectives

This project is an attempt to find out whether pointers should be inside objects or in

separate nodes. For example, with a Iinked list, there are two ways to implement it. In the

'lclosed" implernentation, each node ha a pointer to the next node. in the "open"

implementation, the Iist nodes are sepanted fiom the data nodes; the data nodes have no

pointers. In order to explore the performance of two styles of building data structure

Iibraries in Ci+, present an objective cornparison of two methods of implementation in

tems of ease of coding, size of node, security, timelspace eficiency and reliability. I

designed several applications using Linked List and Tree as the common data smictmes

on base cIûss libraries such as STL and OrgC++ and not on UI Iibnries (such as

Interviews, StarViews, etc,).

Page 9: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

1.2 Motivation

When Dr. Peter Grogono asked me in May 2000 whether 1 would feel like writing the

project to compare data structures in some C + i libnries, my first thought was: is this a

very interesting topic? Many books about data structures and algonthms were already

out at that time, but you could hardly find my nfennces about cornparison of ihese data

structures. L simply questioned the need for doing this project. 1 tried to look at this aspect

in different 1ibr;uies to see if there was any related material, mostly in vain.

Unfortunately, it is very hard to find any books that compare data structures with CU

libraries. About two weeks Iater, I must admit, he convinced me. This project will be very

interesting and new to us. In sumrnary, I enjoyed the project and hope it cm provide

some valuable reference to people who are interested in further reseûrch of data structures

in C++ Iibtan'es.

1.3 Outiine of the Project

The project starts off with an induction in Chapter 1. It describes the objectives and

motivation of the project. Chapter 2 describes the background of the project, and what the

challenges are to the cumnt technology as welI as describing the existing problems with

software and the concepts of the "Open" and "Ciosed" implementation methods. In

Chapter 3, I present details about experimental design h.om mmy aspects including: the

application design, selection of data stnicains, and libraries. In addition, I define the

evduation criteria which are used to compare the performance of the two methods later in

Page 10: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

chapter 5. Chapter 4 gives the details of the irnplementation of the phonebook in the

different Iibraries and Chapter 5 presents the test resulis of the different impIementations.

To compare the performance of each irnplementation I used the predefined criterie 1 ais0

present the User Interface in Chpter 5. At the end 1 conciude the project and sumarize

the two irnplementation methods in Chapter 6 and. 1 also give sorne recommendations for

further work on this project References are listed in the Chapter 7.

Page 11: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 2 Background

In recent years there has been a proliferation of C++ libcaries used by prognmmers but

there are few pubtished cornparisons of data structures with them. With moôt class

libraries today data structures are using "Open" implementation, such as STL. There are

few libraries using '%losedl' implementation, such as OrgC++ Fcom the Code F m .

Definitions of "Open" and "Closed" implementation methods are in section 2.3.

2.1 Problems with existing software

Most of the problems with today's software stem from poor management of intemal data,

Data related enors such as dmgling pointers may stay in the code unnoticed for a long

iime and are often dificult to find There ;ire no generic, fully typed libmies for data

structures in C, and container based C++ Iibraries hide the critical data orgmization (data

structures) thus making the resulting code difficult to debug. They also cause

heffciencies at mn-time. Some libmries offer persistent objects but nquire custom

coded IO functions for every new class. Even highly recognized libraries such as the STL

or tools.h++ do not protect the programmer h m accidentally destroyed objects or

messed up linked lists. For example, you cannot addldelete objects from a list whiIe

aversing it with an iterator.

Page 12: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

2.2 Challenges

The major challenge for present technology is to generate efficient software more

efficientiy. Computen are king used in essentiaily dl areas of human activities; even

products that seemingly have nothing to do with cornputers contain a significant portion

of cornputer related cost.

In general, more efficient software means three things:

Coding and debugging programs fater;

Generating programs with less errors,

Producing cleaner, more readable code, which is eûsier to maintain.

2.3 The Basic Concepts

Here, 1 present a brief explannation of some concepts 1 used in this project.

With "Open" impiementation method, as the nme implies, the Iist nodes are

separated fiom the data nodes; the data nodes have no pointers to substructures. The data

structures are formed by auxiliary objects, usually cailed links, that point to the

appkation objects but do not add any pointers or other data to then Each Link has a

pointer that le& to the object that participates in the Iist

Page 13: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

With "Closed" implementation method, the data structures are formed by

pointers stored inside the appIication objects. For exampie, in a Iinked List, each node has

a pointer to the next node.

Ail this seems so trivial that you may wonder why I are spending so much time

discussing it but there is more here than meets the eye. The problem is that the choice of

implementation has numerous consequences which are often negiected. For example, a

"closed" implementation is compu tationally more efficient, and permits bi-directional

access between the objecis involved. On the other hand, lists using "open"

implementation method are easy to implement using templates, and they form the basis of

almost every existing container class libtiuy.

Page 14: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 3 Design

In this project my major interest is to explore the performance of two styles of building

data structure libraries in Ctt. I present an objective cornparison of two methods of

implemenîation in terms of ease of coding, size of node, security, timdspace effciency

and reliability. Fustiy, 1 need to choase the kind of application and what Iibrnries and

common data structures are to be coasidered in this project. h addition, 1 also need to

consider two other important aspects which, are the comctness of the implementation

and cornparison of the tirne and the space of each data structure in the two tibraries. In

order to evaluate the performance of each implernentation, 1 define evaluation criteria for

the experiments.

3.1 Evaluation Criteria ~efidtion

The critena defined are applicable to a class Iibnry in generai or to its component classes

in particular to this project. The following have been defined:

Usabüity

By this term I mem several aspects which, although difficult to quanti@, are important,

such as the set of classes provided, the ease of use of a class, the kind of services

provided, and the simplicity and clarity of the interface. Of course, the more the interface

is simple and clear, the bener.

Page 15: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Time and Space

For a given data structures such set, iist, tree, gnph, etc., the prognms could check the

time needed to build a list and search elements or the space occupied to build a k t .

a Others

in deciding which C++ class library to use. other criteria cm be defined, such as the

following:

is it shareware or commercial (do we have to pay for it, is it provided in source or

object format)?

0 if it is provided in source format, are there problems to compile it with the

compiler I have ?

is the class library supported by the vendor/developer ?

Of course, without vendoddeveloper support I have to take into account the nsk of

finding bugs 1 then have to fix by myself.

is it easy to install ?

is there a suite of tests to be run in order to make sure that no problems will occur

when using the Iibnry ?

is it reliable? 1s it easy to crash when you do a large search?

Then is something to add to the discussion made so fx. The criteria definecl till now are

somehow context-independent, in the sense that they are gened criteria against which a

clas libnry can be evaiuated and found to be more or l e s 'tdesinble".

Page 16: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

However, other criteria could be defined which are more context-dependent, and could

arise from paaicula. needs related to the kind of application that has to use the class

library- The definition of such criteria is not uicluded in the scope of this project but

ought to be considered when making the final decision on which iibrary (libraries) to use.

3.2 The Application

The application selected was a phone book. The phone book includes fint names, 1st

names and phone numbers of people. 1 designed this application specifically for the

purpose of testing and cornpûnng the performance of the selected Ci-t libmries.

I t is not overly cornplex. 1 chose this application for two reasons:

9 Fint, simplicity made it possible to write implementations in sevenl C++ libraries in

a reasonable time. - Second, the simplicity enhances data structures cornparison because al1 the libmies

considered provide support for the set of featum needed by the application.

The application can carry out the following basic functions:

uisert: input a record which includes f î t nme, last name and telephone number of a

prson hto the Phone Book.

Search: Given a selected phone number, semh for a person's record (Fust n m e &

Last name.)

Page 17: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Delete: Given a selected phone number, delete mord that contains a person's f i t

name, last name and th is phone number. The phone number is used as a key to

associate with the fust name and last name of a person from the phonebook.

3.3 The Data Structures

The application is implemented in different libraries using Double Linked List and binary

me.

The rasons why 1 choose the above structures are:

(1) Both data structures are supported by both STL and Org CH.

(2) They are very common data stnictures in any class Iibrary. In other words, they c m

represent many data structures.

(3) Due to tirne concerns. they are easy to impiement.

(4) A Iinked list and a binary semh me require pointers for allocating their nodes

dynamicali y.

(5) I also considered other data stnicnires, but found that the implementation of these

data strucnires do not add significmt new aspects over the data structures 1 have

chosen. For example, the implementation of a single linked list is similac to a double

Iinked list; the implementation of a hash taie or a graph can use a single or double

iiked kt.

Page 18: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

3.4 The Libraries

There are severai libraries for containers and algorithrns in CH. Each of them has

different chûracteristics of its data structures in the implementation. After searching and

studying many references about different libcaries from different resources, I selected the

libraries considering the foliowing factors:

Availability: The libraries can be easily obtained. They can be either part of most

ment C++ implementations or cm be downloaded free from web-sites.

Common data structures: Al1 libraries have the common support data structures such

as k t , tree, etc.

Different irnplementation: The libraries are able to represent "Open" and "Closed"

implementation methods.

addition, 1 considered other criterie In the end, based on these factors, I decided to

use STL and Or&++ libraries in the project to do the cornparison. You may wonder

why I selected STL and OrgC++. I describe them in the following sections.

Here, 1 wiii mention another Libnry, LEDA (Libnry of Efficient Data types and

Algorithms). LEDA is a library of the data types and algorithms of combinatorial

computing. At the beginning of the library selection, I considered LEDA. It is similar to

STL Data structures of LEDA also are implemented using 'Y)penl' Implementation. It is

not public domain, for this reason and dso concem about tirne, 1 dropped it h m the

study.

Page 19: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

It took quite a long time for me to get a complete ''feeling" of the different and multifold

aspects of each Library, aithough a 'Ylavour" of it may be had in a shorter time.

3.4.1 Standard Template Library (STL)

As is well known, the C* Standard TempIate Library (STL) is provided as a part of

most ment C++ irnplementations. It is a C++ libnry of container classes. algorithms,

and itemtors. It provides many of the basic dgorithms and data smictures of cornputer

science. As its name suggests, the Standard Template Libnry is based on the

comparatively new subject of ternplates. The STL is a genenc libnry, as impiied by the

name, meaning that iis components are heavily parameterized. Almost every component

in the STL is a template. The basic idea of templates is that they allow us to write

functions and classes in a very general way and then specialize them when they are

actually put to use.

The Standard Template Libnry is defined as a set of class interfaces. Individual vendoa

provide their own impiementritions of the STL. Strictly speaking, the cornparison I

describe in this report is between a particular imp&ementafion of STL (narnely, the

implementation provided by Microsoft) and OrgC++.

The orpankation and design of the STL differs in aùnost dl respects from the design of

most other C* Iibraries. The most important diffemce between STL and al1 other C++

container class iibraries is that most STL algorithms are generic; they work on a variety

of containers and even on ordinary CH m y s . A key factor in the Iibnry design is the

Page 20: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

consistent use of iterators, which generalize C t t pointers, as intemediaries between

aigonthms and containers. Containers make iterators available, algorithms use them, and

this leads to a separation which dlows an exceptionally clear design.

The~fore, iterators are central to genenc programming because they are an interface

between containers and algorithms. Algonthms typically take iterators as arguments, so a

container need only provide a way to access its elements using iterators. This makes it

possible to write a generic algorithm that operates on many different kinds of containers,

even containers as different as a vector and a doubly linked list.

3.4.2 C++ Data Object Library (OrgC++)

CM Data Object Library inciudes the most extensive data structures on the market for

fast, automatic peaistence and are ideal for large, complex Ci+ projects.

The Ci-+ Data Object Library (OrgCu), is much mon than just a class library. It is

based on the new concept of hyper-objects which are objects that store their data in other

objects, cailed carriers, carriers passively keep the data, but hyper-objects provide the

methods. Or&* provides an additional absinct Iayer for the management of data. We

do not have to assemble the organization from Iibr;uy objects. The organhtion is

automaticaily generated in a way which provides optimum run-time performance.

Combmed with automatic persistence and version control, Orge++ pmvides a set of

Page 21: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

powemil tools which are usehl for a variety of tasks ranging from the mangagement of

intemal data to the design of fast memory resident databases.

Compared to the C++ Iibrary, STL.,, OrgC* is a library of intrusive data structures which

are formed by pointers stored inside the application objects, unlike container where

auxilüary objects fom the required data structure and point only to the application

objects without adding any pointers or other data to them. Therefore, OrgC+c uses the

Wosed" implementation and STL uses the "open" implementation.

3.5 Test Correctness

The main prognrn of this project is the phonebookcpp. A phonebook is defined as a list

and binary search tree in STL and as Double - R h g and Double - Tree in OrgC++. It is

used to test a11 the data structures implemented in this project, which include:

Person: Defines a record of a penon that includes the peaon's firstnume, lastname,

and phonenumber. It is used as a user defined data type Iater.

Functor Defines a random number generator. It is used to initialize a stream by a

given seed In other words. it is used to generate each random data stream of

firstname, Imarne and phonenumber of persons.

PersontiasIt: is used to get a peaonS record h m a given phonenumber.

The following functionaiity is tested in the applications using STL:

a To search a record of a person in the existing phonebook. It retums an iterator to that

object if the given phonenumber is found thcn I cm get the reconi of that person.

Page 22: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

O To insert a record of a person ïnto the phonebook and provide confirmation whether

the record is inserted. 1 can also later use search to verify if the record is correctiy

inserted,

To delete a record of a person from the existed phonebook and check whether it cm

be conectly removed nom the phonebook.

3.6 Cornparhg Time and Space Efficiency

In order to anaiyze the time and space performance of data structures when building and

accessing a large tree and a list in both STL and OrgCu, 1 compare the results in STL to

equivalent results in OrgC*. I used the foliowing code to create a random number

genentor. It genentes some large random numbers. I use them as records for the

phonebook. The large number is also used as the number of nodes for building mes and

lists and the numkr of times to access trees and lists.

long unsigned Functoc:randorn-number() C

ran - (m * multipIier)%moduh; if (m c 1000000) ran +- 30000w,

retum m;

Page 23: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

3.6.1 Cdculating Time

I use the funcùon _itime() to get the cunent time of the system: to rneasure the time for

building and accessing trees and lists.

To get the build time of a tree or a List, 1 cal1 ftme(() right More and right afier building

the tree or the list. Then I calculate the time difference between the two cdls of m e ( ) .

Similarly, I get the access time of a tree or a List by calling m e ( ) right before and right

afier accessing the tree or the list for n times.

3.6.2 Calculating Space

I use sizeoA*this) to rneasure the space needed for each tree node or list node. I

implement a sice() hnction in each full node class and calculate the space needed for an

entire tree or list.

3.6.3 Dennition of Time and Space Ratio

In order to compare time and space performance of data structures in each library easily

and cleariy, 1 define time and space ration as following:

Given a nurnber n, I define

Time to build a data structure with nodes using STL BuiidTimeRatio (n) =

Time to buitd a datri structure with nodcs using Or*

Time to access a data structure with n nodes 10000 times using STL

AccessTimeRatio (n) - Tnaé to access a &ta structure with n nodes 10000 using O r e

S p a ueeded to buad a data structure with n n& using STL SpaceRatio (n) -

Space needed to build a data structure with a nodes using OrgCtt

Page 24: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

I use BuildTirneRatio (n), AccessTirneRatio (n), and SpaceRatio (n) to compare the time

and space efficiency of using STL and Or&++. If the ratio is Iess than 1, it means that

using STL requues less time or space than using OrgCu.

I use IOOOO ioop iterations because 1 wmt to make the cornpaison of the performance of

two implementations in the same Ievel. Although it takes a long time to access data in the

"Open" implementation, it is almost instant in "Closed" implementation if the number of

the loop times used is small.

Page 25: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 4 Implementation

In this section. 1 will give the implementation details of the phonebook in different

lib&es. In order to show how the implementation cm be generaiized to work with any

data type without conflicts between STL (Standard Template Library) and OrgC++ ( Ci+

Data object Library) in this project, al1 the classes head file names in STL start with 'stl'

and in OrgCtt- start with the brgc'.

'ïhere are three main classes de fined through this application.

Class Functoor: It defines a random number generator. It is used to initialize a Stream

by a given seed.

Class Person: It defines a personk Jirstname, lustnarne and phonenumber. C h s

ORGCPerson is used as a template parameter type in the file ORGCphonebookcpp.

a Ciass PersonHask It defines the function that is used to get a person3 record by a

given phonenumber.

For more implementation details of the classes, please d e r to finctor.h. person.h and

personhusit.h on page 50 to 57.

The diffemt libraries have k e n Ulstalled on Wmdows 95. The tests have been carried

out on Wmdows95 using Microsoft Visual C++ 6.0.

Page 26: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

4.2 " Open" Implementation Method

The Standard Template Library is a C++ library of container classes, dgorithms, and

iterators. Container classes are very usefuI classes of data abstractions in the STL. A

container contains values of some kind or references to values of some kind. For

example, list is a kind of container and set is another kind of container. The difference

between a list and a set is that a list imposes a physical, though not necessarily a Iogical

ordeMg of the elements it contains, and a set imposes nothing on the values it contains

other than the fact of containment. STL also includes a large collection of algo&hms that

manipulate the data stored in containers.

Another important data absuaction provided by the Standard Template Library are called

itemtors. They are used to refer to the individual elements of containers and to provide

the means of applying operations to the contents of containers.

From the previous chapter 3.4.1,I know that a key factor in STL design is the consistent

use of itentoa. Pointers themselves are iterators. So what is the purpose of itentoa? in

the STL, very few of the algorithms needed to manipulate an abstraction are implemented

in the cl= corresponding to thnt abstraction. Instead, the class defuies functions that

make certain information about the abstraction - here a iist - available in such as way that

1 can implement any needed algorithm without modiwng the structure itself. In fact, it is

possible to build such dgorithms in such as way that they work with other data

abstractions as well as the one for which they were orïginally designed. This is the

purpose of iterators.

Page 27: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

In terms of inheritance, the Standard Template Library does not depend heavily on

object-oriented features of C++. Relatively IittIe inheritance is involved in this Library. In

contras& other libraries use ïnheritance extensively, some to the extent that every class is

derived fiom a common base class. In fact, some other object-oriented languages

(Smalliaik, Modula-3, Java) make this a quirement.

Implementing a Doubly Linked List

In this section I shail look at an implementation of a doubly linked list that uses two

pointers in each node as shown in figure 1. A doubly linked list, is a sequence that

supports both forward and backward traversai, and provide constant time insertion and

removal

Figure 1

of elements at the beginning or the end, or in the middle.

shows the structure of Lists in the STL

Figure 1 : Lists in STL

20

Page 28: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

In the project, when a phonebook is created, I use container clûss List:

iist<Person> phoneBook;

list<Person>::iterator phoneBookiteratur;

1 also define phonebuokherator.

As we know iterators are the mechmism that rnakes it possible to decouple algorithms

fkom containers; algorithms are templates, and are panmetenzed by the type of itentor,

so they are not restricted to a single type of container.

Hence, in the project, when I search a record of a peaon in the existing phonebook, 1 use

the STL5Jnd-fl) aIgorithm to perfom this search through the phonebook list. It shows:

Q(phoneBook(terator==phoneBoukend()) cour CC "Record not found N1 list' << endl;

else cou[ << *phoneBookiterator cc endl;

It completes the first time PersonHaFII() retums true for any object It retums an iterator

to that object, or if PersonHmIt() never renirned mie,/ind_ifO returns end().

To insert a record of a person into the phonebook and to provide confmation to check

whether the record is inserted, an Insert iterator is used to implement this. Here is the

definition of inseaion into a container by means OF.

phoneBookimert(ph~ook.end(). @erson);

Page 29: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Its effect is to insert the record of aPerson immediately before the iterator

To delete a record of a person from the existing phonebook and check whether it c m be

correctly removed nom the phonebook. 1 use STL's algorithm remove() to perform this

Remove removes from the range [phoneBookbegin(), phoneBookend()l all elements that

are equal to the value of PerHasl(pn).

Implementing a Binary Search Tree

A tree structure is used to store data in sorted order. It consists of nodes that contain data.

Nodes have links to other nodes. Any tree has a single mot node to which a11 other nodes

are linked. A node data must have a key which cm be compared and sorted. A binary tree

is the simplest type of a tree structure; a b h r y search tree is û pûnicular kind of binary

tree. Each node in a binary search tree contains data and two links. The Iefi link connects

to al1 nodes with fesser data values; and the right link connects to al1 nodes with greater

&ta values. if one datum is greater or less than another datum it can be determined by

cornparhg the key values of the two data.

In STL, I use Set to represent tree to compare to the Tree organization in OrgC*. Set is

one of the associative Containers in STL. Aiso STL sets are always sorted and they are

impiemented using tree structures. In this project, 1 use a binary search tree. It hoids data

Page 30: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

that c m be compared with something Iike operutor c. The build-in types of C* have

such a comparison, though the comparison for pointers to strings (or indeed any pointers)

is quite meaningless. The~fore, the STL p e d t s the user to define alternate cornparison

operations using function objects. A comparison object is either a binary function

renuning bool, or an object in a class that has such an operator() defined.

In the project, I defmed the following comparison function to compare two ndes:

class PersortLess { 6001 operator() (const Person& x, const Persondi y) consr {

return rgetPhonenumber() < y.getPhonenurnber(); }

1

4.3 " Closed " Implementation

In the C++ Data Object Libnry (OrgC++), al1 organizations are based on a ring-type

arrangement, not on a NULL-ending Iist.

4.3.1 Implementing a Doubly Linked List

In order to make a comparison to STL list, 1 can use double-ring organization to

represent doubly linked list because they behave simiiarly.

Figure 2: DOUBLE-RING in O r g h

Page 31: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

As 1 descnbed in the previous chapter 3.4.2,OrgC++ is based on new concepts of Hyper-

objects which are objects that store theù data in other objects, called carriers. Carriers

passively keep the data but hyper-objects provide the methods.

OrgC++ uses genenc functions. For example, add() c m be used to add an object to a

ring. to a me, or to a graph, without loosing the advantage of niII type checking, dei() for

delemg (disconnecting) an object. In OtgCu, a ring (or circular k t ) is a structure

existing on a set of objects without any s W t d pointer encapsulated in a special class.

Since the entry to the ring is not encapsulated I have to keep it extemally, otherwise the

ring would be there, but 1 would not know how to get to it. The entry is also important if 1

am concened about the order of the objects in the ring. The entry to the ring wil1 be

mtumed as the last element when tmvening the ring.

Here I defined a phonebook using double-ring orgrniration.

22 - HYPER - DOUBLE - RING{phoneBook,Person); // declare a DOUBLE-RING

Before 1 start to use this ring. I have to set entry=NUU.

Person *nstart=NLILL; f i ini tialize start be fore using the DOU BLE-RING.

I also define Persun* phoneBook-p~. OrgC++ doesnt provide operationfind-ifl) in

DOUBLE-RING. in order to compare to STL algorithrnfind-fl), here I also define it as

the following:

Page 32: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Person*find-flPersonHasIt& anotherqerson) { Person *temp; int count = 0;

ZZZZAATRAVERSE(phoneBook,mtart,tempK if(anotherqerson(*templ) break;

)ZZ - A - END; return temp;

3

Then when 1 search a record of a person in the existed phonebook, 1 defined:

if (!p honeBookqtr) cout CC "Record not fourzd in livr" << end!:

else cour CC *phoneBookptr << endl;

To insert a record of a person into the phonebook and provide confirmation to check

whether the record is inserted. I use O r g W generic hinction addo:

Adds new object, a record of aPerson to the ring with entry point mart and returns a

new entry.

1 also use OrgCtç generic function del0 to move a record of a person fiom the existing

phonebook and check whether if it cm be correctiy removed €rom the phonebook.

Page 33: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

In OrgC++, 1 use DOUBLE - TREE to compare to the STL Sets as illustrated in figure 3

L

A

D parerit

L L

E b

ba sibling

child

Figure 3: DOUBLE-TREE in OrgC++

Here I defined a phonebook using DOUBLE-TREE organization:

Because OrgCtç doesnt contain find - iff) aigorithrn, as 1 described in 4.3.1, 1 therefore,

have to de fine find - in) here to semh for a person's record.

Page 34: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

while f mtpgarent) { if (anothergerson(*mrpgarent)) break,-

P No chiZd Y i f ( ! ~ r ~ t ~ c h i l d = p i t o n ~ k . c h i l d ( ~ p ~ a r e n t ) ) ) {

mpgarent = NUU; break,- 1

P One child */ else if((second~child=phoneBookfwd~rsr~child))==first~child) { if((anuther~erson.getPhonenwnber()> =)))

&&~r~t~child->getPhonenumber()<hp~~ent->getPhonenmber())) f

tmpyarent = N U ; break;

) else ~((another~erson.gcrPhonenmber()~mp~are~t->getPhonenumbe~))

& &Lfirsr-chiid->gerPhonenumbeN)> =nnp~arent->getPhoneniimber())) {

mipgarent = N U U ; break:

) else mrpgurent = first-child;

/* Two chikiren */ c k {

if(anoihergerson.gerPhonenumber()> =mrpgarent->gctPhonenitmbed)) tmpjarent = scconti-chiid;

else mtpgarenr = first-child;

1 ) /* while loup */ return mrp9arent;

1

Then I can usejnd - ifto search for a person record of the phonebook using:

if(!phoneBuokytr) )i cout < c "Record nos fouad in list' << endl: 1 eke {

cout <C *phoneBookgtr < < endl: 1

Page 35: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

To remove a record of a person from phonebook, hen I also dehed phoneBookdei():

void phoneBookdei(Person* delperson) {

As seen in figure 3, all the children of any node fom a RMG. The child pointer fiom the

piirent represents the start pointer of the RMG. Repeated use of add() loads the nodes in

reverse order. When I insert a record of a person into the phonebook, the following

OrgC++ contained operations are used to define phoneBookadd() .

Here I defincd phoneBookadd() to add a node into the phonebook.

. Addo A P P ~ Inso

adds a new child; appends a new sibling to the right of a given node. inserts a new sibling to the left of a given node

Page 36: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

I* Na child *1 if(!@rsr-chiId=phoneBookchiid(mip~~rent))) {

phoneBookadd(mtpyarent. newgemon); remrn;

)

If One child */ e k if((second~child=ph~neBmk.fwd~rst~~hild))==frr~t~~hild) {

if((new~on->gerPhonentmber()> =mp~urent->gerPhonenitntber()] Le & ( J r s t - c h i i d - > g e t P h o n e n w n b e r ( ) c t n i p ~ ) }

phoneBook.app(Frstkchifd, newgerson); / / appends a new sibling ro the righr of a given node. retirrn; Ise ~((n~vgcrson->gerPhonenum&er()cnnp~are~t-~getPf~o~e~umbe~)) & &Grsr-child->gerPhonenmbeq)> =mtp9arenr->getPhane~t~rmber())) f

phoneBwk.i~rsttchi1d. nctvgerson): return;

) else mtpgarenr = ftrst-chiid:

l* T\vo chiidren */ eke {

if(navyerson->getPhonenwnber()> =tmpgarent->gerPhonenurnber()] mpgcuent = second_child;

eke mtpgarent = fitsr-chi&

1

) r+ whife loop */

Page 37: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

4.4 User Interface

In order to compare the selected data structures emily in STL and OrgCtr; 1 ongindly

wanted to use Micmsofi Foundation CIass (MFC) as an "application hnework" to

design and implement Windows Style User Interface for the project Because MFC is

accepted and used by many professionai programmen, it's very naturai for Windows to

have a C t t programming interface. MFC Library apptication fnmework inciudes many

genenl-purpose classes such as collection classes for tists, arrays and maps.

1 tried using MFC to design üI for both implementations in the project, but because of

dificulties with OrgCtç class genentor. 1 decided against it although a lot of time was

spent in the considention. From the perspective of the main purpose of this project. the

interface is not a criticai factor in the completion of the project to compare data structures

in STL and OrgCcç. Therefore, I used MFC Library to implement UI for "Open"

implernentation only for STL. and UI for Or@+ interactive windows under MS-DOS

was used,

4.4.1 Suspected Problems with OrgC++

The problem is that OrgC++ consists of a clss generator and a speciai Library. The class

generator typicdly just rcads the header füe with the declatations of objects and

organization, and it creates two files: an include file and a source file with ai1 the access

routines required by the program. These two fiIes are used to compile and Iink the

originai source code. Hence, when modifyuig data objects, I have to cal1 the class

Page 38: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

generator every t h e before re-compilation. AU used classes have to be modified when

using OrgCti.; and in addition, might involve changes to the MFC base classes.

4.4.2 UI for " Open" Implementation in STL

Figure 4 below shows the main interface of the Phone Book system using STL.

Figure 4: The Main Window of PhoneBook in STL

As show m Figure 4 1 can select a data structure using either doubly Linked iist or set

and dso select a number of records (nodes) to generate a phonebook

This main window also shows two resuIts:

Page 39: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

The time used to build this phonebook in miIlisecouds

The space required to build this phonebook in bytes

In addition there are four buttons on the right side of the screen. Any one of the following

options cm be selected :

Semh: to search a record of a person in the phonebook.

0 Insert to insert a record of a person into the phonebook.

Remove: to delete a record of a person nom the phonebook.

Exit: to exit the phonebook system.

The following sub-window will be popped up after pressing the Search bunon.

FIGURE 5: UI for Searching a record in STL

This sub-window aIIows us to input a phone numkr to seairh for a record of a person

whose phone number is the input phone number. It also shows the loop times when I

perform a search firnction. The seacch m 1 t will dso show in the box after 1 press the

Page 40: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

start button. In each sub-window 1 have three buttons to select Start, Reset and Cancel.

Staa and Reset buttons are disable in the initial sub-screen but are activated if you input a

phone number Ui the box field:

Start to stiirt search 1 insert 1 nmove a record in / to / fiom a phonebook

Reset: to m e t al1 data in the screen if I have done a search.

Close: to exit this screen and go back to the main window of the application

If I press the insert button, the foUowing window is shown. It is similar to the su b-screen

of the search screen except the= are three box fields for us to input a new penonS first

nme, 1st name, and phone number into the existing phonebook.

Figure 6: UI for inserüng a record in STL

if 1 press the Remove button, it will bring us to this screen:

Page 41: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Figure 7: UI for deleting a record in STL

It is very similar to the Search sub-screen. (See Figure 5) It also shows the remove resdt

after 1 press the Start bunon on this screen.

Page 42: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

4.4.3 UI for " Closed" Implementation in OrgC++

1 described the problems with OrgC++ in the section 4.2.1 and, therefore, only show the

interactive wÏndows for this irnplementation. See Figure 8.

Figure 8: UI in OrgC++

As we cm see, four selection options are provided I can key in the number of the

selected option and press Enter key to show the nsult Actually it is simi1ar to the four

buttons shown on the main window of the "Opentt implementation (See figure 4).

Page 43: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 5 Results

In this section I evaluate and analyze the performance of both implementations of data

structures considered. 1 also present the results of each experimentd test, It is important

to note that the results in this section reflect the performance of a particular

implementation of the data structures and do not uecessarily reflect performance

limitations that exist in the data structures independent of these implementations in each . .

Iibrary. . . ,

According -to the evduation critena defined in the section 3.1, hen 1 present the results

for each item.

5.1 Usability - Ease of Coding

After I expenenced implementation of the data structures in both "Open" and "Closed"

implementations, 1 found both STL and OrgCtt Libmies provide sets of classes. There

are some differences.

Difference in clas declantion

In S W c m define a class just as I do in C++ to define ail data and member functions

of a clas.

In OrK* when you declare an object (STL called cIass), oniy the attributes that do

not relate to the orgdation of data are declared. Objects which will be involved in

the automatic handling of data mua contain a Line of the form

Page 44: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

ZZ-EXT-<objectTypee>. This statement marks the place where the transparent

pointers will be inserted.

Here is how 1 defmed a class Person in both implementations.

In STL I In orgctc

class PersonHasIt f public..

PersonHasIt(); // constmctor PersonH~~l~It(Ult& phoneMunber); // person h a the phonenwnber.

boo l operator() (Persun & operson); 1

private: int numbec 1:

class PersonHasft f

Public: PersonHarft(); Il construcfor PersonHarIt(int& phoneMunber); I/ person hm the phonenwuber.

bool operator() (Person& aPerson );

private: inr numbcr:

a STL contains more genenc algonthms.

STL provides finci - ifl) algorithm 1 can easily use it to semh for r record in the

phonebook. Or@+ doesn\ contain this function so 1 have to define a function called

End-if() in the program. Then 1 c m use it to perfom a search as 1 would with STL. To

see the implementation of this. please refer to section 4.3, on page 23.

0 STL and 0rgC-w have very similar interface attributes.

They both have similar iteratoa and similar functions that mûnipulate the data structures

but they are not identical. In STL, data and their relations are interwoven with the classes

that carry the data. h Or@+, data and their relations are orthogonaI.

Page 45: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

The following shows the declaration of a phonebook in each hi-

5.2 Time and Space

In STL

In this section 1 show the test results of building time ratio, access tirne ratio, and space

ratio for "Open" and "Closedrr implernentations as 1 defmed earlier in section 3.6.3. Each

time the test results comsponding to different node numbers are genented.

In OrgC*

Build Tirne Efficiency

The build time ratios 1 got frorn both implementations are show in Table I . From the

table, we can see that the results of build time ratio for doubly linked Iist and binary

search tree smicture are similar. They indicate that the time used to build a phonebook in

the Tlosedn implementation is greater than in the "Open" impkmentation no matter

what daîa structure is selected,

The time range of building doubly Iinked list with nodes from 8000 to 140000:

"Open1 implementation: 440 - 10 1 10 milliseconds

"Closed" implementation: 6040 - 121390 miiliseconds

Page 46: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Table 1: Build Time ratio

- . - . - . -. . -

B inary Tree

0.088792 0,056995 B u ild

Tlm eRatio

I

Build T ime Ratio Char t

Access Time Efficiency

Test No .

1

I 2 3 4 5 6 7

L

8 9

In this section, 1 show the results of time ratios in the following table:

No.of Node

8000 10000 20000 40000 60000 80000 100000 120000 140000

Double Linked L M

0.072848 0.044534 0,055423 0,062898 0.141 103 0,091810 0,078645 0,077346 0,083285

Page 47: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Access Time Ratio

Access Time Ratio

I Test I No. I Double I Binary I

Access Time Ratio Chart for Double Linked List

I No. 1 2 3 4 5 6 7 8 9

Access Time Ratlo Chart for Binary Search Tree

Search Tree 0.350649 . 0.1 82796 0.1 87500 2.941 176 0.940299 2.640884 14.060000

of Nodes I Linked List 8000 12870.400000 10000 20000 40000 60000 80000 100000

3555.800000 5959,500000 1 1923.500000 21 983,600000 34925.000000 29763.1 66667

120000 35703.500000 1 8.227273 140000 15 1 062.000000 3.638498

Page 48: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

From the previous section 5.2.1, we know that it tiikes more time to build a phonebook

with Tlosed" implementation. You may then wonder about access time. The test results

are surprishg as shown in TabIe 2.

The nw data, that is what I actually meûsured, is shown in Table 3.

Table 3: Access Time

Access T / m

S7 list

From the test results we see it quired much longer to search for a record in the

phonebook using doubly linked Iisi in the "Open" implementation. It appem that

accessing data is very efficient in the 'Closed" implementation. The results of access

time ratio for the binary search tree are very close. The "Open" implementation method

is stightly slower thm the "Closed" implementation method. Why are the results so

different? The exact cause is unclear, but it is probably due to ciifferences in the

organization of STL and OrgC++ data smictures. A11 organizations in OrgC* are based

on a riug-type arrangement, not on a NUUrtenninated List. Another possibility is that

OrgCw reorgarrizes the List so that, when a node is accessed, it becomes the first node of

the Iist. in addition, the allocation of links for STL lists may mate lot of overhead

Page 49: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

because travershg Iists in STL requires an additional pointer jump and periodic

redIocation and copying of the Links.

5.2.3 Space Efficiency

1 show the test m l t s of space ratio in this section. From the fofiowing table 4, we can

see that the space ratios of ail the data structures 1 implemented are 15. The required

space to build a phonebook in each implementation with both selected data structures are

the same. hence, the space efficiency of the two implementations are the sane.

Table 4: Space Ratio

Space Ratio Chart

- 1 -c- Double Linked List - - - Binary Tree

. -. - - .. - . - - - --- -- -.

I test No.

No. of Nodes

8000 10000 20000 40000 60000 80000 1 00000 120000 140000

Space

1 2 3

Double Linked Iist

1.5 11.5 1.5 1.5 1 -5 1.5 1 *5 1.5 1 -5

(Set) Binaty Tree

1 -5 1 -5 1.5 1 -5 1 -5 1.5 1 .5 1 -5 1 -5

Ratio

I 4 5 6 7 8 9

Page 50: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

From the table, it dso shows more space was recpired in the "Open" implementation than

in the "Closed" irnplementation.

5.3 Interaction with MFC

As described in section 4.4, at the beginning of design phase, 1 wanted to use MFC for

both implementations to mate UI but there are some problems with OrgCtt so 1 had to

give up using MFC for "C1osedV implementation.

5.4 Other Cornparisons

I dso did some cornparisons of other cnteria which 1 defined in the evaluation criteria in

Chapter 3. Hem, 1 show the summarized results of cornparison on the two Librarîes in

the following table.

Page 51: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Table 5: Cornparison of the two Librarles in other criteria

Compare criteria

Is it shareware or

commercial?

Are there problem to

compile on PC?

Is it supported &y the

vendorf developer ?

fs if a suite of tests?

Is it easy tu crash when

doing a large search?

Standard Template Library

The standard Template L i b w

is one of the standardized

components of the C++

language.

It's quite easy to compile.

Yes.

No worry about instailation.

It cornes with Visual C*.

Yes.

CH Data Object Library

It is commercial, Full source

of Data Object Libmy is

not available fkee at this

time.

It's not easy to compile. I

had a aot of problems to

compile it on my PC at the

beginn ing.

No. Free binary version of

the libnry does not include

any support or guidance.

No. It took a quite of time to

install it on my PC.

Yes

It cnshed a few times when

doing a large search.

Page 52: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 6 Conclusions

In this chapter, I conclude the experimental results and summarize the advantages of

using STL and OrgCtc; I dso discuss the research of cornparison performance of data

s m i c ~ s for fiirther directions in which the project cou1d be taken.

ui summûry, this project is an experiment of explorhg the performance of data structures

in different implementation methods. AIthough the application and data structures

selected are not considered perfect, it may provide us some interesting findings on data

structures for future research since there is not much published in this m a .

Through the project, I know the "Closed" implementation provides a new way to look at

pointer problerns. It uses a code generator to build a meta mode1 of al1 application classes

and it inserts the required pointers using generated macros, not inheritance, This is a dirty

but effîcient method which results in fûster code but is not as elegant as templates.

Compaml to the "Open" implementation method, the phonebooks are easy to implement

using templates, and they form the basis of aimost every existing container class library.

6.1 The advantages of STL and OrgC++

1 have implemented our Phonebook in both l ibdes, there are some daferent fwhires in

each of them:

Page 53: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Standard Template Librarv

Itk easy to use. It has an elegant, consistent, and easy to comprehend architecture.

The different components of the STL can be plugged together which rnakes the

programmer more productive.

STL algorithms are geaenc. They work on a variety of containers and even on

ordinary C++ amys.

The use of STL is likely to make software more reliable, more portable and more

general and will reduce the cost of producing it.

It intencts weli with other libraries such as MFC*

C++ Data Obiect Librarv

Data structures cm be protected against most pointer errors by initializing al1 pointers

to NULL, and setting them back to NULL when the object is disconnected fiom the

data structure, because a11 organintions arr based on a ring-type arrangement, not on

a NULL-terrninated list An object cannot be destroyed before a11 its pointers are

NULL*

It makes the data structures autornaticaily persistent by taking Code generator to find

the locations of al1 the pointers, and without the user coding and supporting

seridization functions for every clw.

It is possible to perform a f&st nin-time check for the integrity of the list.

It is more efficient sometimes to do random semch, insert or remove operations from

a doubly linked list

Page 54: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

6.2 Future Work

There is not much work published in this area. The study of data structures in different

C* implementation methods has generally been neglected. Therefore, there is the

opportunity for much more research in this area

In this project, I presented the design and Unplementation of a phonebook system with

both '%Iosedtr and "Open'implementations in Standard Template Library and Ctç Data

Object Library. Although the implementation was for only two Libraries and for a specific

application, there are some findings recommendûtions for future work to expand the

researc h.

Select more libracies other thm STL and OrgCtt such as LEDA, Rogue Wave, etc.

Design and implement other applications to explore the performance of data

structures.

Select more! data structures other than doubly link lists and mes.

Implement the application on other pla~orms such as UNIX, Windows NT, etc.

Make cornparisons using different criteria than were used here.

Page 55: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Chapter 7 Bibliography

Books and articles

Joseph Bergin. Data Structure Progrmnming ivith The Standard Template Libraty

in Ci+. Sp~ger-VerIag New York Berlin, Inc, 1998

Ulrich Breymann. Designing Components with the CC+ +: A New Approach to

Progranzming. Addison-Wesley, 1 998

Timothy A. Budd. Multiparadigm Programming in LEDA. Addison-Wesley

Publishing Company, Inc., 1 995

Robert Robson. Using the Sn: The C++ Standard Template Library. Spnnger-

Verlag New York, Inc., 1998

Leen Ammeraa1. Sn for C++ Programrners. John Wiley & Sons Ltd., 1997

Gndy Booch. Object Oriented Design with Applications. The BenjaminlCummings

Publishing Company, Inc., 199 1

Mitchell L Model. Data Sfrttctures, Data Abstraction - A Contemporary

Introduction Clsing C++. Rentice Hall, hc. Englewood CIiffs, New Jersey. 1994

Glenn W . ROI. Introduction to Data S~mctures and Afgorithrns with C++. hntice

Hdi Europe, 1997

ian Sommerville. Sofnuare Engineering. Fiifth edition Addison-WcsIey, 1995

[IO1 Cameron Hughes and Tracey Hughes. Collection and Container Classes in Ci+.

John Wiley & Sons, hc., 1996

Page 56: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

[11] Frank M. Carrano. Data Abstraction And P r o b h Solving With C++. The

Benjamin/Cummings Publishing Company, Inc., 1995

[12] J. P. Pardoe and M. I. King. Object Oriented Programming Ushg Ci+. Macmillan

Press Ltd., 1997

B. Information on the web

[ I l Jui Soukup. CH Data Object Libnry and Persistent Data.

http:llwww.codefms.com/

[2] LEDA. hnp:!/www.m~i-sb.moede/LEDMedâ.htm1

133 Standard Template Library Programmer's

h ttp:l/ w IV w .sgi .com/Tec hnoIogyf STU

[4] Standard Template Library. http:/l//www.cs.rpi.edu/-musser/stl.htmI

Guide.

Page 57: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Appendix

I/stlfunctor.h: nit declurution of clars Functor in SIZ, it defnes a random number Ilgenerator. It is used to initilize a stream by a given seed.

Il constants used in nndom number genentor. const unsigned long int module - IOOOOOOOLU; const unsigned long int multiplier - 169874;

Ilclass Functor defines a random number genentor. class Functor { Ilprivate:

unsigned long ran; //used to represent the mdom data stream.

unsigned long random-numbem; Ildefine function random-number().

public: // constnictor initilizes a data stream by a seed.

Functor(int i): ran(i){>

//Get a random number. unsigned long getRandom0 { return random-nurnbea; )

//operator() overloading. unsigned long int operator()() ( retum nadom-numbetf);)

Page 58: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

// st1person.h: the declararion of class Person in STL. It defines a person's firstname, Il lutname und phonenumber. C las ORGCPerson

class Person { private:

char *firstNme; //define person's fintname char *lastNûme; ildefine person's Iastname

int phoneNumber; //define person k phonenumber

public: Person(); il Constructor -Person(); Il Destnictor Person(char * f, char *1, int i-O);

cons ch@ gethmame() const { retum IastName; ) const ch& getFirsmme() const { ceturn fmtName; )

If void setLastname(l); l/ void setFinmame(f) Il void setPhonenumber(i);

int sizeo; int getPhonenumber() const; // Get a person's phonenurnber. void setPerson(char *s 1, char *s2, int i d ) ; // Set a person's record.

//operators friend ostrearn& operator<<(ostnam& out, cons Person& p);

bool operator-(const Person& p2); bool operator!-(const Person& p2); bool operator<(const Person& p2); bool operato~(const Person& pz);

Page 59: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

class PersonLess .( public:

bool operator0 (const Person& x, const Person& y) const ( #ifdef ND-NAME

int temp - strcmp(x.getLastname0. y.getL.ashiame0); if(tempc0)

return tme; else if (temp > O) nturn fiilse;

temp - strcmp(x.getFitname(), y.getFmtnme0); if(tempC0)

retum tme; else retum false;

#else retum x.getPhonenumber() < y.getE%onenumber();

#endif }

k

Page 60: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Il st1personhasit.h: contains dedarution of class ORGCPersonHasIt in OrgC++. It II dejhes thefinetion that is used to get a person's record by a given phonenumber.

class PersonHasIt { public:

PersonHasItO; 11 constnictor PersonHasIt(int& phoneNumber); // person has the phonenumber. bool operator() (Person& aPerson);

private: int number;

1;

Page 61: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

II orgcjùnctor. hi The dedaration of class class Functor in Orge++, it defines a random Il ntaber generator. It is used to initilize a Stream by a given seed.

Il constants used in random number generator. const unsigned long int module = 10000000LU; const unsigned long int multiplier - 169874;

Ilclass Functor defines a random number generator. c1;tss Functor {

ZZ_EXT_Func tor

Ilprivate: unsigned long ran; Ilused to represent the random data Stream.

unsigned Iong random-number(); //define function mdom-number().

public: Il coastructor initilizes a &ta Stream by a seed.

Functor(int i): m(i) { > IiGet a random number,

unsigned long getR;uidom() ( return random-nurnbem; )

Ilopentor() overloading. unsigned Iong int operator()() ( retum random-number();)

Page 62: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

Ilorgcperson.h: the dedaration of class Person in O@+ +. I t defines a person's I f firstnume, l a s t m e and phonenumber.

#indude ciostreamb #indude "zzinc 1.h"

class PersonhasIt;

ciass Person

fnend int phoneBooksize(); Fnend Person* findif(PersonHasIt& another-person);

pnvate: char *firstNme; Ildefine person 's fmtname char * lasName; //define person's iastnme

int phoneNumber, Ildefine person's phonenumber

public: Person(); // Constructor -Person(); // Destructor Person(char *f, char *I, int i-O);

const char* getLastname() const { cemm IastName; ) const char* getFirstname0 const { r e m fhtName; )

int GetS izeo; int getPhonenurnber() const; Il Get a person's phonenumber. void setPerson(char *SI, char *s2. int i d ) ; // Set a person's record.

Iloperators friend ostream& operator<c(ostmm& out* const Person& p);

bool operator-(const Person& p2); bool opcrator!-(const Person& p2); bool operator<(cwst Person& pz); bool operato~const Person& p2);

Page 63: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

public: bool operator0 (const PersonBr x, const PersonBr y) const {

#ifdef IND-NAME int temp - strcmp(x.getLastname0, y.getlastname()); if ( temp < 0)

return tme; else if (temp > O) return fdse;

temp - strcmp(x.ge~mtname(), y.ge@mtname()); if ( temp < 0)

retum me; else return false;

#else cetum x.getPhonenumber() < y.getPhonenumber();

#endif }

1;

Page 64: A COMPARISON OF DATA STRUCTURES IN C++ · PDF filea comparison of data structures in c++ wemg zhou a major feport in the department of computer science presented in partial fulfalment

II orgcpersonhasit.h: contains deciaralion of class ORGCPersonHasIt in Or&++. Zt II deftnes thejùnction t h t is used to get a person's record &y a given phonenumber.

public: PersonHask(); // constructor PersonHasit(int& phoneNumber); Il person has the phonenumber. bool operator() (Person& aPerson);

private: int number; Il defme phonenumber as integer. k