source: xssim/trunk/src/simulator/lists/slotList/TimeOptIterator.java @ 104

Revision 104, 2.1 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.lists.slotList;
2
3import schedframe.scheduling.TimeResourceAllocation;
4import schedframe.scheduling.utils.ResourceParameterName;
5
6
7
8/**
9 *
10 * @author Marcin Krystek
11 * 1. Sloty jedno processorowe, to pewnie nie jest najlepiej
12 *
13 */
14public class TimeOptIterator implements SlotsGanttIterator{
15
16        protected SlotsGantt list;
17        protected int cpuCnt[];
18       
19        public void visit(SlotsGantt list) {
20                this.list = list;
21                this.cpuCnt = new int[list.size()];
22               
23                for(int i = 0; i < cpuCnt.length; i++){
24                        try {
25                                cpuCnt[i] = list.get(i).getAllocatedResource().
26                                                getResourceUnit(ResourceParameterName.CPUCOUNT).getUsedAmount();
27                        } catch (NoSuchFieldException e) {
28                                e.printStackTrace();
29                        }
30                }
31        }
32
33        public boolean hasNext() {
34                boolean ret = false;
35               
36                for(int i = 0; i < cpuCnt.length && ret == false; i++){
37                        if(cpuCnt[i] != 0)
38                                ret = true;
39                }
40               
41                return ret;
42        }       
43
44        public TimeResourceAllocation next() {
45                TimeResourceAllocation retSlot = null;
46               
47                int i = -1;
48                while(i < cpuCnt.length && cpuCnt[++i] == 0);
49               
50                cpuCnt[i]--;
51                TimeResourceAllocation slot = list.get(i);
52                retSlot = lengthenSlot(i + 1, new TimeResourceAllocation(slot));
53               
54                return retSlot;
55        }
56
57       
58        public void remove() {
59                throw new RuntimeException("Removing object by "+this.getClass() +" is not supported.");
60        }
61       
62
63        /**
64         *
65         * @param i index of the next element in the list
66         * @param currentSlot the one which will be lengthened if it is possible.
67         * @return
68         */
69        protected TimeResourceAllocation lengthenSlot(int i, TimeResourceAllocation currentSlot){
70                if(i >= cpuCnt.length || cpuCnt[i] == 0)
71                        return currentSlot;
72               
73                TimeResourceAllocation slot = list.get(i);
74               
75                if(currentSlot.getEnd().compareTo(slot.getStart()) == 0){
76                        currentSlot.setEnd(slot.getEnd());
77                        cpuCnt[i]--;
78                } else {
79                        return currentSlot;
80                }
81               
82                return lengthenSlot(++i, currentSlot);
83        }
84       
85       
86        public void reset(){
87                for(int i = 0; i < cpuCnt.length; i++){
88                        try {
89                                cpuCnt[i] = list.get(i).getAllocatedResource().
90                                                getResourceUnit(ResourceParameterName.CPUCOUNT).getUsedAmount();
91                        } catch (NoSuchFieldException e) {
92                                e.printStackTrace();
93                        }
94                }
95        }
96
97}
Note: See TracBrowser for help on using the repository browser.