List Representation Simulated Pointers - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

List Representation Simulated Pointers

Description:

May be used for internal data structures only. ... Can free a chain of nodes in O(1) time when first and last nodes of chain are known. ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 28
Provided by: poste74
Category:

less

Transcript and Presenter's Notes

Title: List Representation Simulated Pointers


1
List Representation Simulated Pointers
  • Fall 2004
  • CSE, POSTECH

2
Limitations Of Java Pointers
  • May be used for internal data structures only.
  • Data structure backup requires serialization and
    deserialization.
  • No arithmetic.

3
Simulated-Pointer Memory Layout
  • Data structure memory is an array, and each array
    position has an element field (type Object) and a
    next field (type int).

4
Node Representation
  • package dataStructures
  • class SimulatedNode
  • // package visible data members
  • Object element
  • int next
  • // constructors not shown

5
How It All Looks
c
a
e
d
b
  • 14

next
14
0
11
8
-1
c
a
e
d
b
element
0
1
2
3
4
5
8
11
14
firstNode 4
6
Still Drawn The Same
firstNode
-1
a
b
c
d
e
7
Memory Management
  • Linked system (Java or simulated pointer)
    requires
  • a way to keep track of the memory that is not in
    use (i.e., a storage pool)
  • way to allocate a node
  • Java has the method new
  • way to free a node that is no longer in use
  • Java uses garbage collection

8
Garbage Collection
  • The system determines which nodes/memory are not
    in use and returns these nodes (this memory) to
    the pool of free storage.
  • This is done in two or three steps
  • Mark nodes that are in use.
  • Compact free space (optional).
  • Move free nodes to storage pool.

9
Marking
  • Unmark all nodes (set all mark bits to false).
  • Start at each program variable that contains a
    reference, follow all pointers, mark nodes that
    are reached.

10
Marking
c
a
e
d
b
c
a
e
d
e
firstNode
Repeat for all reference variables.
11
Compaction
Free Memory
c
b
e
d
b
a
e
d
firstNode
  • Move all marked nodes (i.e., nodes in use) to one
    end of memory, updating all pointers as necessary.

12
Put Free Memory In Storage Pool
  • Scan memory for unmarked nodes (if no compaction
    done), otherwise put single free block (unless no
    free memory) into pool.

13
Advantages Of Garbage Collection
  • Programmer doesnt have to worry about freeing
    nodes as they become free.
  • However, for garbage collection to be effective,
    we must set reference variables to null when the
    object being referenced is no longer needed.

14
Advantages Of Garbage Collection
  • Applications may run faster when run on computers
    that have more memory.

15
Disadvantage Of Garbage Collection
  • Garbage collection time is linear in memory size
    (not in amount of free memory).

16
Alternative To Garbage Collection
  • Provide a method to free/deallocate a node.
  • e.g., delete method of C
  • Now free nodes are always in storage pool.

17
Advantage Of Alternative
  • Time to free nodes is proportional to number of
    nodes being freed and not to total memory size.

18
Disadvantages Of Alternative
  • User must write methods to free data structure
    nodes.
  • Time is spent freeing nodes that may not be
    reused.
  • Application run time does not improve with
    increase in memory size.

19
Storage Pool Organization When All Nodes Have
Same Size
  • Maintain a chain of free nodes
  • Allocate from front of chain
  • Add node that is freed to chain front

20
Simulated-Pointer Memory Management
  • / memory management for simulated pointer
    classes /
  • package dataStructures
  • import utilities.
  • public class SimulatedSpace1
  • // data members
  • private int firstNode
  • SimulatedNode node // package visible
  • // constructor and other methods come here

21
Constructor
  • public SimulatedSpace1(int numberOfNodes)
  • node new SimulatedNode numberOfNodes
  • // create nodes and link into a chain
  • for (int i 0 i lt numberOfNodes - 1 i)
  • nodei new SimulatedNode(i 1)
  • // last node of array and chain
  • nodenumberOfNodes - 1 new
    SimulatedNode(-1)
  • // firstNode has the default initial value
    0

22
Allocate A Node
  • public int allocateNode(Object element, int next)
  • // Allocate a free node and set its fields.
  • if (firstNode -1)
  • // double number of nodes, code omitted
  • int i firstNode //
    allocate first node
  • firstNode nodei.next // firstNode
    points to next free node
  • nodei.element element
  • nodei.next next
  • return i

23
Free A Node
  • public void deallocateNode(int i)
  • // Free node i.
  • // make i first node on free space list
  • nodei.next firstNode
  • firstNode i
  • // remove element reference so that space
    can be garbage
  • // collected
  • nodei.element null

24
Simulated Pointers
  • Can allocate a chain of nodes without having to
    relink.
  • Can free a chain of nodes in O(1) time when first
    and last nodes of chain are known.

25
Simulated Pointers
  • Dont use unless you see a clear advantage to
    using simulated pointers over Java references.

26
Union-Find Problem
  • Given a set 1, 2, , n of n elements.
  • Initially each element is in a different set.
  • 1, 2, , n
  • An intermixed sequence of union and find
    operations is performed.
  • A union operation combines two sets into one.
  • Each of the n elements is in exactly one set at
    any time.
  • A find operation identifies the set that contains
    a particular element.

27
Using Arrays And Chains
  • See Section 7.7 for applications as well as for
    solutions that use arrays and chains.
  • Best time complexity obtained in Section 7.7 is
    O(n u log u f), where u and f are,
    respectively, the number of union and find
    operations that are done.
  • Using a tree (not a binary tree) to represent a
    set, the time complexity becomes almost O(n
    f) (assuming at least n/2 union operations).
Write a Comment
User Comments (0)
About PowerShow.com