c interfaces and implementations techniques for creating reusable software

533 645 3
c interfaces and implementations techniques for creating reusable software

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Table of Contents Copyright Addison-Wesley Professional Computing Series Preface Acknowledgments 10 Chapter Introduction 12 Section 1.1 Literate Programs 13 Section 1.2 Programming Style 19 Section 1.3 Efficiency 22 Further Reading 23 Exercises 24 Chapter Interfaces and Implementations 26 Section 2.1 Interfaces 26 Section 2.2 Implementations 29 Section 2.3 Abstract Data Types 32 Section 2.4 Client Responsibilities 35 Section 2.5 Efficiency 41 Further Reading 41 Exercises 42 Chapter Atoms 44 Section 3.1 Interface 44 Section 3.2 Implementation 45 Further Reading 53 Exercises 53 Chapter Exceptions and Assertions 56 Section 4.1 Interface 58 Section 4.2 Implementation 64 Section 4.3 Assertions 70 Further Reading 74 Exercises 75 Chapter Memory Management 78 Section 5.1 Interface 80 Section 5.2 Production Implementation 84 Section 5.3 Checking Implementation 87 Further Reading 96 Exercises 97 Chapter More Memory Management 100 Section 6.1 Interface 101 Section 6.2 Implementation 103 Further Reading 109 Exercises 111 Chapter Lists 114 Section 7.1 Interface 114 Section 7.2 Implementation 119 Further Reading 124 Exercises 125 Chapter Tables 126 Section 8.1 Interface 126 Section 8.2 Example: Word Frequencies 129 Section 8.3 Implementation 136 Further Reading 143 Exercises 144 Chapter Sets 148 Section 9.1 Interface 149 Section 9.2 Example: Cross-Reference Listings 151 Section 9.3 Implementation 159 Further Reading 169 Exercises 169 Chapter 10 Dynamic Arrays 172 Section 10.1 Interfaces 173 Section 10.2 Implementation 176 Further Reading 180 Exercises 180 Chapter 11 Sequences 182 Section 11.1 Interface 182 Section 11.2 Implementation 185 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved Further Reading 191 Exercises 191 Chapter 12 Rings 194 Section 12.1 Interface 194 Section 12.2 Implementation 198 Further Reading 207 Exercises 208 Chapter 13 Bit Vectors 210 Section 13.1 Interface 210 Section 13.2 Implementation 213 Further Reading 224 Exercises 224 Chapter 14 Formatting 226 Section 14.1 Interface 227 Section 14.2 Implementation 235 Further Reading 249 Exercises 250 Chapter 15 Low-Level Strings 252 Section 15.1 Interface 254 Section 15.2 Example: Printing Identifiers 260 Section 15.3 Implementation 262 Further Reading 275 Exercises 276 Chapter 16 High-Level Strings 280 Section 16.1 Interface 280 Section 16.2 Implementation 287 Further Reading 304 Exercises 305 Chapter 17 Extended-Precision Arithmetic 308 Section 17.1 Interface 308 Section 17.2 Implementation 314 Further Reading 332 Exercises 333 Chapter 18 Arbitrary-Precision Arithmetic 334 Section 18.1 Interface 334 Section 18.2 Example: A Calculator 338 Section 18.3 Implementation 345 Further Reading 364 Exercises 365 Chapter 19 Multiple-Precision Arithmetic 368 Section 19.1 Interface 369 Section 19.2 Example: Another Calculator 376 Section 19.3 Implementation 384 Further Reading 413 Exercises 413 Chapter 20 Threads 416 Section 20.1 Interfaces 419 Section 20.2 Examples 429 Section 20.3 Implementations 442 Further Reading 474 Exercises 476 Interface Summary 480 AP 481 Arena 482 Arith 483 Array 483 ArrayRep 484 Assert 485 Atom 485 Bit 485 Chan 487 Except 487 Fmt 488 List 489 Mem 490 MP 491 Ring 494 Sem 495 Seq 496 Set 497 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved Stack 498 Str 498 Table 501 Text 502 Thread 504 XP 505 Bibliography 508 515 bvdindexIndex 515 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved Addison-Wesley Professional Computing Series Brian W Kernighan, Consulting Editor Ken Arnold/John Peyton, A C User’s Guide to ANSI C Tom Cargill, C++ Programming Style William R Cheswick/Steven M Bellovin, Firewalls and Internet Security: Repelling the Wily Hacker David A Curry, UNIX® System Security: A Guide for Users and System Administrators Erich Gamma/Richard Helm/Ralph Johnson/John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software David R Hanson, C Interfaces and Implementations: Techniques for Creating Reusable Software John Lakos, Large Scale C++ Software Design Scott Meyers, Effective C++: 50 Specific Ways to Improve Your Programs and Designs Scott Meyers, More Effective C++: 35 New Ways to Improve Your Programs and Designs Robert B Murray, C++ Strategies and Tactics David R Musser/Atul Saini, STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library John K Ousterhout, Tcl and the Tk Toolkit Craig Partridge, Gigabit Networking J Stephen Pendergrast Jr., Desktop KornShell Graphical Programming Radia Perlman, Interconnections: Bridges and Routers David M Piscitello/A Lyman Chapin, Open Systems Networking: TCP/IP and OSI Stephen A Rago, UNIX® System V Network Programming Curt Schimmel, UNIX® Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers W Richard Stevens, Advanced Programming in the UNIX® Environment W Richard Stevens, TCP/IP Illustrated, Volume 1: The Protocols W Richard Stevens, TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols Gary R Wright/W Richard Stevens, TCP/IP Illustrated, Volume 2: The Implementation C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved C INTERFACES AND IMPLEMENTATIONS Licensed by Techniques Frank for LiuCreating Reusable Software 1740749 David R Hanson Princeton University ▲ ▼▼ ADDISON-WESLEY An imprint of Addison Wesley Longman, Inc Reading, Massachusetts • Harlow, England • Menlo Park, California Berkeley, California • Don Mills, Ontario • Sydney Bonn • Amsterdam • Tokyo • Mexico City C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved This book was prepared from camera-ready copy supplied by the author Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book and Addison Wesley Longman, Inc was aware of a trademark claim, the designations have been printed in initial caps or all caps The authors and publishers have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers discounts on this book when ordered in quantity for special sales For more information, please contact: Corporate & Professional Publishing Group Addison Wesley Longman, Inc One Jacob Way Reading, Massachusetts 01867 Library of Congress Cataloging-in-Publication Data Hanson, David R C interfaces and implementations : techniques for creating reusable software / David R Hanson p cm –– (Addison-Wesley professional computing series) Includes bibliographical references and index ISBN 0-201-49841-3 (pbk.) C (Computer program language) Computer software– –Reusability I Title II Series QA76.73.C15H37 1996 005.13'3––dc20 96-28817 CIP Copyright © 1997 by David R Hanson All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher Printed in the United States of America Published simultaneously in Canada Text design by Wilson Graphics & Design (Kenneth J Wilson) Text printed on recycled and acid-free paper ISBN 0-201-49841-3 10-MA-00999897 Second printing, January 1997 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved PREFACE P rogrammers are inundated with information about application programming interfaces, or APIs Yet, while most programmers use APIs and the libraries that implement them in almost every application they write, relatively few create and disseminate new, widely applicable, APIs Indeed, programmers seem to prefer to “roll their own” instead of searching for a library that might meet their needs, perhaps because it is easier to write application-specific code than to craft welldesigned APIs I’m as guilty as the next programmer: lcc, a compiler for ANSI/ISO C written by Chris Fraser and myself, was built from the ground up (lcc is described in A Retargetable C Compiler: Design and Implementation, Addison-Wesley, 1995.) A compiler exemplifies the kind of application for which it is possible to use standard interfaces and to create interfaces that are useful elsewhere Examples include interfaces for memory management, string and symbol tables, and list manipulation But lcc uses only a few routines from the standard C library, and almost none of its code can be used directly in other applications This book advocates a design methodology based on interfaces and their implementations, and it illustrates this methodology by describing 24 interfaces and their implementations in detail These interfaces span a large part of the computing spectrum and include data structures, arithmetic, string processing, and concurrent programming The implementations aren’t toys — they’re designed for use in production code As described below, the source code is freely available There’s little support in the C programming language for the interfacebased design methodology Object-oriented languages, like C++ and Modula-3, have language features that encourage the separation of an interface from its implementation Interface-based design is independent of any particular language, but it does require more programmer willpower and vigilance in languages like C, because it’s too easy to pollute an interface with implicit knowledge of its implementation and vice versa xi C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved xii PREFACE Once mastered, however, interface-based design can speed development time by building upon a foundation of general-purpose interfaces that can serve many applications The foundation class libraries in some C++ environments are examples of this effect Increased reuse of existing software — libraries of interface implementations — reduces initial development costs It also reduces maintenance costs, because more of an application rests on well-tested implementations of general-purpose interfaces The 24 interfaces come from several sources, and all have been revised for this book Some of the interfaces for data structures — abstract data types — originated in lcc code, and in implementations of the Icon programming language done in the late 1970s and early 1980s (see R E Griswold and M T Griswold, The Icon Programming Language, Prentice Hall, 1990) Others come from the published work of other programmers; the “Further Reading” sections at the end of each chapter give the details Some of the interfaces are for data structures, but this is not a data structures book, per se The emphasis is more on algorithm engineering — packaging data structures for general use in applications — than on data-structure algorithms Good interface design does rely on appropriate data structures and efficient algorithms, however, so this book complements traditional data structure and algorithms texts like Robert Sedgewick’s Algorithms in C (Addison-Wesley, 1990) Most chapters describe one interface and its implementation; a few describe related interfaces The “Interface” section in each chapter gives a concise, detailed description of the interface alone For programmers interested only in the interfaces, these sections form a reference manual A few chapters include “Example” sections, which illustrate the use of one or more interfaces in simple applications The “Implementation” section in each chapter is a detailed tour of the code that implements the chapter’s interface In a few cases, more than one implementation for the same interface is described, which illustrates an advantage of interface-based design These sections are most useful for those modifying or extending an interface or designing related interfaces Many of the exercises explore design and implementation alternatives It should not be necessary to read an “Implementation” section in order to understand how to use an interface The interfaces, examples, and implementations are presented as literate programs; that is, the source code is interleaved with its explanation in an order that best suits understanding the code The code is extracted automatically from the text files for this book and assembled into the C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved ORGANIZATION xiii order dictated by the C programming language Other book-length examples of literate programming in C include A Retargetable C Compiler and The Stanford GraphBase: A Platform for Combinatorial Computing by D E Knuth (Addison-Wesley, 1993) Organization The material in this book falls into the following broad categories: Foundations Introduction Interfaces and Implementations Exceptions and Assertions Memory Management More Memory Management Data Structures 10 11 12 13 Lists Tables Sets Dynamic Arrays Sequences Rings Bit Vectors Strings 14 15 16 Atoms Formatting Low-Level Strings High-Level Strings Arithmetic 17 Extended-Precision Arithmetic 18 Arbitrary-Precision Arithmetic 19 Multiple-Precision Arithmetic Threads 20 Threads Most readers will benefit from reading all of Chapters through 4, because these chapters form the framework for the rest of the book The remaining chapters can be read in any order, although some of the later chapters refer to their predecessors Chapter covers literate programming and issues of programming style and efficiency Chapter motivates and describes the interfacebased design methodology, defines the relevant terminology, and tours two simple interfaces and their implementations Chapter describes C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright © 1997 by David R Hanson This download file is made available for personal use only and is subject to the Terms of Service Any other use requires prior written consent from the copyright owner Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws All rights reserved ... 497 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright... 515 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright... 1997 C Interfaces and Implementations: Techniques for Creating Reusable Software C Interfaces and Implementations: Techniques for Creating Reusabl Prepared for frliu@microsoft.com, Frank Liu Copyright

Ngày đăng: 04/09/2013, 22:04

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan