07-processor-post3up

07-processor-post3up - The processor model Readings:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
The processor model Readings: Readings from CP:AMA, as detailed in the document available on the Web site. Vectors and arrays The processor model Costs of Scheme and C primitives CS 136 Spring 2011 07: The processor model 1 Address calculations The machine instructions implementing Scheme or C structure accessors compute an offset from the first word of memory holding the structure. Given a Scheme structure with four fields whose first word is at memory address 200, the pointer to the value of the third field is stored in memory address 208, because 208 = 200 + (3 - 1) * 4 . This computation is valid even if the 3 is replaced by a larger number. This suggests a data structure with rapid access to the i th element (unlike lists). The corresponding Scheme (R5RS) data type is vector . Many other languages, including Java, C, and C++, use the word array . CS 136 Spring 2011 07: The processor model 2 To create a vector, the memory allocator must be asked for a set of consecutive memory locations of the desired size. This means that the size of a vector is fixed at the time of creation. ( define v1 ( vector 9 2 5 blue " test " )) ( define v2 ( make-vector n 0 )) ( define v3 ( list vector ( build-list 10 add1 ))) All of these functions have running time O ( n ) , where n is the number of elements involved. CS 136 Spring 2011 07: The processor model 3
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Vector representation in the memory model ( define v ( vector 5 8 3 1 4 )) <short int> 1048 5 <short int> 1032 8 1040 1056 1064 <short int> <vector, len 5> ... 1016 1032 1004 1036 1024 1012 1020 1048 1000 1008 1044 1028 1040 1008 v identifer location address contents v 5 8 3 1 4 CS 136 Spring 2011 07: The processor model 4 The function vector-length consumes a vector and produces its length. The function vector-ref consumes a vector v and a natural number i , and produces the i th element of the vector (the numbering starts from 0). ( vector-set! v i val ) mutates the i th element of a vector v so that it becomes val , and produces ( void ) . Each of these has O (1) running time. We will justify these claimed running times later in this module, when we discuss details of the processor. CS 136 Spring 2011 07: The processor model 5 Swap Elements in a Vector ( define v ( vector 0 10 20 30 40 50 )) ;; Purpose: swap the elements at indices i and j ( define ( swap v i j ) ( define temp ( vector-ref v i )) ( vector-set! v i ( vector-ref v j )) ( vector-set! v j temp )) CS 136 Spring 2011 07: The processor model 6
Background image of page 2
Maximum Value ;; v-max: (vectorof Int) Int ;; Purpose: Find the maximum value in a vector of numbers. ( define ( v-max v ) ( define to ( - ( vector-length v ) 1 )) ( define ( v-max from ) ( cond [( = from to ) ( vector-ref v to )] [ else ( define max-rest ( v-max ( + from 1 ))) ( cond [( > ( vector-ref v from ) max-rest ) ( vector-ref v from )] [ else max-rest ]) ])) ( v-max 0 )) CS 136 Spring 2011 07: The processor model 7 The Heap Data Structure The heap data structure is an effi- cient way to repeatedly find and re- move the minimum value from a col- lection of values.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

07-processor-post3up - The processor model Readings:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online