240 likes | 394 Views
Representing Structure and Behavior. CS1316: Representing Structure and Behavior. Story. Techniques for representing data structure Arrays, linked lists, circular linked lists, trees, graphs, stacks, queues, event queues Techniques for representing behavior
E N D
Representing Structure and Behavior CS1316: Representing Structure and Behavior
Story • Techniques for representing data structure • Arrays, linked lists, circular linked lists, trees, graphs, stacks, queues, event queues • Techniques for representing behavior • Functions (in Python), methods (in Java) • Techniques for representing both • Objects (what they know, and what they know how to do) • Simulations • Functional branches in a tree • Messages in a simulation • Techniques for describing structure and behavior outside a program • UML class diagrams, Abstract Data Types (ADTs) • What’s a program?
The Point of this Course • Real computer-based media developers rarely work in terms of pixels and samples • Computer musicians deal in terms of notes, instruments, patches, and other structures. • Computer animators deal in terms of characters, movement, scenes, and other structures • Bottom-line: They structure their media.
Driving Questions of the Course • How did the wildebeest’s charge over the ridge in The Lion King? • How did the villages wave in The Hunchback of Notre Dame?
The answer: Modeling and Simulation • These are two of the (rare) times that Disney stepped away from their traditional drawn cel animation. • Instead they: • Modeled the structure of wildebeests and villagers, • Modeled the behavior of wildebeests (how they stampede) and villagers (how they wave), • Then started a computer simulation that executed the models…and basically filmed the screen.
Data structures and their properties: Arrays • Arrays are efficient in memory • Everything is packed together • Uses no additional memory • Very fast for accessing any individual element • They are hard to insert and delete with. • Have to move everything around • They can’t be grown. • They have a fixed size.
Linked Lists • Uses extra memory to record next • Simple to insert and delete elements within • Reconnect, disconnect • Have to find elements to access them • Can grow as large as memory allows
Circular linked lists • Good for describing data that loops • But don’t try to traverse them using any normal traversal!
Trees • Uses branch nodes that represent both next but children. • In binary trees, every node represents left and right. • All the advantages of linked lists, but can also representing hierarchy or clustering.
Graphs • Like trees, but arbitrary linking. • Anything can link to anything else, including loops. • Good for representing things like highway systems and blood paths and other real things.
Stack • A LIFO (Last In First Out) list. • We described it as an ADT (Abstract Data Type) • What methods it understands (push, pop, peek, size, empty) • What those methods do. • We used it for reversing a list.
Queue • A FIFO (First In First Out) list • We described it as an ADT, too. • We used it to represent agents lining up for a resource in a simulation
Event Queue • It’s a queue where elements are kept in a sorted order. • For us, it was in order of event times. • We needed the earliest time to be at the top of the queue.
Representing behavior • Functions in Python describe behavior. • They take data in as input and act upon it • Sometimes returning other data • Methods in Java describe behavior • They can act just like functions, but they can also manipulate this—the object that the method is part of.
ADT’s • Describing the methods • Push, pop, peek, size, empty • Describing how they work • Pop off the head • Push onto the tail • If you write your programs expecting any implementation of a given ADT, your program will work even when the ADT implementation changes.
Representing both • Objects • Keep instance (and class “static”) data and its structure—what they “know” • Keep methods for describing behavior on that data—what they “know how” • Objects represent both structure and behavior • Objects were originally invented in order to do simulations. • All programs can be thought of as simulations.
Embedding behavior in our structures • Branches in trees that manipulate their children • Scaling in sound trees • Moving and vertical/horizontal placement in picture trees • Messages in simulations are data that trigger behavior • “Now, you’re at the factory”
How do we describe this stuff? • Describing structure and behavior is modeling • Obviously, a program can, but that’s a very detailed description. • Anything easier to grasp, to see without so much implementation detail?
What’s a program? • As we started with in CS1315: • A program is a specification of process, so exact that it can be executed by a machine. • It’s the same thing now, at the end of CS1316. • But…
Where’s the program? • The program is embedded in representations of structure and behavior. • It’s in that tree that holds your hierarchy. • And in those messages that tell your agents what to do. • And in how that queue works. • And throughout the insertAfter(), and add(), and last() and other methods of your data structures. • And in ALL those objects that you create and use. • Programs are distributed across your representations of structure and behavior.