~zodiacsohma/armagetronad/vectron

« back to all changes in this revision

Viewing changes to src/orfaust/containers/ListBase.as

  • Committer: zodiacsohma1 at gmail
  • Date: 2014-01-22 02:46:23 UTC
  • Revision ID: zodiacsohma1@gmail.com-20140122024623-1dyhnemuhogi23gp
Converted all tab characters into spaces (4 gaps).

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
package orfaust.containers
27
27
{
28
 
        internal class ListBase
29
 
        {
30
 
                protected var _head:Object;
31
 
                protected var _tail:Object;
32
 
                protected var _size:uint = 0;;
33
 
 
34
 
                // SIZE
35
 
                public function get length():uint
36
 
                {
37
 
                        return _size;
38
 
                }
39
 
 
40
 
                // EMPTY
41
 
                public function get empty():Boolean
42
 
                {
43
 
                        return !_size;
44
 
                }
45
 
 
46
 
                // CHECK
47
 
                protected final function check(name:String = 'ListBase'):void
48
 
                {
49
 
                        if(!_head)
50
 
                                throw new Error(name + ' is empty');
51
 
                }
52
 
 
53
 
                // APPEND
54
 
                protected function append(data:*):void
55
 
                {
56
 
                        var node:Object = {data:data,prev:_tail,next:null};
57
 
 
58
 
                        if(!_tail)
59
 
                                _head = node;
60
 
 
61
 
                        if(_tail)
62
 
                                _tail.next = node;
63
 
 
64
 
                        _tail = node;
65
 
                        _size ++;
66
 
                }
67
 
 
68
 
                // PREPEND
69
 
                protected function prepend(data:*):void
70
 
                {
71
 
                        var node:Object = {data:data,prev:null,next:_head};
72
 
                        if(!_head)
73
 
                                _tail = node;
74
 
                        else
75
 
                                _head.prev = node;
76
 
 
77
 
                        _head = node;
78
 
                        _size ++;
79
 
                }
80
 
 
81
 
                // REMOVE NODE
82
 
                protected function removeNode(node:Object):void
83
 
                {
84
 
                        check();
85
 
                        if(node.prev)
86
 
                                node.prev.next = node.next;
87
 
 
88
 
                        if(node.next)
89
 
                                node.next.prev = node.prev;
90
 
 
91
 
                        if(node == _head)
92
 
                                _head = node.next;
93
 
 
94
 
                        if(node == _tail)
95
 
                                _tail = node.prev;
96
 
 
97
 
                        _size --;
98
 
                }
99
 
 
100
 
                // FIND NODE
101
 
                protected function findNode(data:*):Object
102
 
                {
103
 
                        //check();
104
 
                        if(_size == 0)
105
 
                                return null;
106
 
 
107
 
                        var tmp = _head;
108
 
                        while(tmp)
109
 
                        {
110
 
                                if(tmp.data == data)
111
 
                                        return tmp;
112
 
                                tmp = tmp.next;
113
 
                        }
114
 
                        return null;
115
 
                }
116
 
 
117
 
                // PRINT ALL
118
 
                public function printAll():void
119
 
                {
120
 
                        if(!_head)
121
 
                        {
122
 
                                trace('empty');
123
 
                                return;
124
 
                        }
125
 
                        trace('size: ' + _size);
126
 
                        var counter = 0;
127
 
                        var tmp = _head;
128
 
 
129
 
                        while(tmp)
130
 
                        {
131
 
                                counter ++;
132
 
 
133
 
                                if(tmp == _head)
134
 
                                        trace('head');
135
 
 
136
 
                                trace(counter + ': ' + tmp.data);
137
 
 
138
 
                                if(tmp == _tail)
139
 
                                        trace('tail');
140
 
 
141
 
                                tmp = tmp.next;
142
 
                        }
143
 
                }
144
 
        }
 
28
    internal class ListBase
 
29
    {
 
30
        protected var _head:Object;
 
31
        protected var _tail:Object;
 
32
        protected var _size:uint = 0;;
 
33
 
 
34
        // SIZE
 
35
        public function get length():uint
 
36
        {
 
37
            return _size;
 
38
        }
 
39
 
 
40
        // EMPTY
 
41
        public function get empty():Boolean
 
42
        {
 
43
            return !_size;
 
44
        }
 
45
 
 
46
        // CHECK
 
47
        protected final function check(name:String = 'ListBase'):void
 
48
        {
 
49
            if(!_head)
 
50
                throw new Error(name + ' is empty');
 
51
        }
 
52
 
 
53
        // APPEND
 
54
        protected function append(data:*):void
 
55
        {
 
56
            var node:Object = {data:data,prev:_tail,next:null};
 
57
 
 
58
            if(!_tail)
 
59
                _head = node;
 
60
 
 
61
            if(_tail)
 
62
                _tail.next = node;
 
63
 
 
64
            _tail = node;
 
65
            _size ++;
 
66
        }
 
67
 
 
68
        // PREPEND
 
69
        protected function prepend(data:*):void
 
70
        {
 
71
            var node:Object = {data:data,prev:null,next:_head};
 
72
            if(!_head)
 
73
                _tail = node;
 
74
            else
 
75
                _head.prev = node;
 
76
 
 
77
            _head = node;
 
78
            _size ++;
 
79
        }
 
80
 
 
81
        // REMOVE NODE
 
82
        protected function removeNode(node:Object):void
 
83
        {
 
84
            check();
 
85
            if(node.prev)
 
86
                node.prev.next = node.next;
 
87
 
 
88
            if(node.next)
 
89
                node.next.prev = node.prev;
 
90
 
 
91
            if(node == _head)
 
92
                _head = node.next;
 
93
 
 
94
            if(node == _tail)
 
95
                _tail = node.prev;
 
96
 
 
97
            _size --;
 
98
        }
 
99
 
 
100
        // FIND NODE
 
101
        protected function findNode(data:*):Object
 
102
        {
 
103
            //check();
 
104
            if(_size == 0)
 
105
                return null;
 
106
 
 
107
            var tmp = _head;
 
108
            while(tmp)
 
109
            {
 
110
                if(tmp.data == data)
 
111
                    return tmp;
 
112
                tmp = tmp.next;
 
113
            }
 
114
            return null;
 
115
        }
 
116
 
 
117
        // PRINT ALL
 
118
        public function printAll():void
 
119
        {
 
120
            if(!_head)
 
121
            {
 
122
                trace('empty');
 
123
                return;
 
124
            }
 
125
            trace('size: ' + _size);
 
126
            var counter = 0;
 
127
            var tmp = _head;
 
128
 
 
129
            while(tmp)
 
130
            {
 
131
                counter ++;
 
132
 
 
133
                if(tmp == _head)
 
134
                    trace('head');
 
135
 
 
136
                trace(counter + ': ' + tmp.data);
 
137
 
 
138
                if(tmp == _tail)
 
139
                    trace('tail');
 
140
 
 
141
                tmp = tmp.next;
 
142
            }
 
143
        }
 
144
    }
145
145
}
 
 
b'\\ No newline at end of file'