Mercurial > hg > blitz_condensed
comparison src/org/dancres/struct/LinkedInstances.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.struct; | |
2 | |
3 import java.io.Serializable; | |
4 import java.util.List; | |
5 import java.util.ArrayList; | |
6 | |
7 /** | |
8 In most general situations one might store a number of instances in a | |
9 java.util.LinkedList or similar but this has an overhead as we create | |
10 a holder object for each instance we add - i.e. we're delegating the | |
11 management of being in the list to this holder object. This has two | |
12 disadvantages: | |
13 | |
14 <OL> | |
15 <LI> Memory overhead - we have two objects per object we wish to hold. </LI> | |
16 <LI> Lack of direct addressability - having obtained a reference to an | |
17 object we might like to use that directly to access members of the list. | |
18 </LI> | |
19 </OL> | |
20 | |
21 User is expected to provide suitable multi-threaded protection. | |
22 */ | |
23 public class LinkedInstances implements Serializable { | |
24 private LinkedInstance theHead; | |
25 private LinkedInstance theTail; | |
26 | |
27 private int theTotalInstances = 0; | |
28 | |
29 public LinkedInstances() { | |
30 } | |
31 | |
32 public LinkedInstance getHead() { | |
33 return theHead; | |
34 } | |
35 | |
36 public LinkedInstance getTail() { | |
37 return theTail; | |
38 } | |
39 | |
40 public List copy() { | |
41 ArrayList myMembers = new ArrayList(); | |
42 | |
43 LinkedInstance myInstance = theHead; | |
44 while (myInstance != null) { | |
45 myMembers.add(myInstance); | |
46 | |
47 myInstance = myInstance.getNext(); | |
48 } | |
49 | |
50 return myMembers; | |
51 } | |
52 | |
53 public void insert(LinkedInstance aLink) { | |
54 aLink.setNext(null); | |
55 aLink.setPrev(null); | |
56 | |
57 if (theHead == null) { | |
58 theHead = theTail = aLink; | |
59 } else { | |
60 theHead.setPrev(aLink); | |
61 aLink.setNext(theHead); | |
62 theHead = aLink; | |
63 } | |
64 | |
65 ++theTotalInstances; | |
66 } | |
67 | |
68 public void add(LinkedInstance aLink) { | |
69 aLink.setNext(null); | |
70 aLink.setPrev(null); | |
71 | |
72 if (theHead == null) { | |
73 theHead = theTail = aLink; | |
74 } else { | |
75 theTail.setNext(aLink); | |
76 aLink.setPrev(theTail); | |
77 theTail = aLink; | |
78 } | |
79 | |
80 ++theTotalInstances; | |
81 } | |
82 | |
83 public void remove(LinkedInstance aLink) { | |
84 LinkedInstance myPrev = aLink.getPrev(); | |
85 LinkedInstance myNext = aLink.getNext(); | |
86 | |
87 if (myPrev != null) | |
88 myPrev.setNext(myNext); | |
89 else | |
90 theHead = myNext; | |
91 | |
92 if (myNext != null) | |
93 myNext.setPrev(myPrev); | |
94 else | |
95 theTail = myPrev; | |
96 | |
97 --theTotalInstances; | |
98 | |
99 aLink.setPrev(null); | |
100 aLink.setNext(null); | |
101 } | |
102 | |
103 public LinkedInstance removeLast() { | |
104 LinkedInstance myLast = theTail; | |
105 theTail = myLast.getPrev(); | |
106 | |
107 if (theTail != null) | |
108 theTail.setNext(null); | |
109 else | |
110 theHead = null; | |
111 | |
112 myLast.setPrev(null); | |
113 myLast.setNext(null); | |
114 | |
115 --theTotalInstances; | |
116 | |
117 return myLast; | |
118 } | |
119 | |
120 public int getSize() { | |
121 return theTotalInstances; | |
122 } | |
123 } |