Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  The Bag (Fast Object Collection)  (Read 2340 times)
0 Members and 1 Guest are viewing this topic.
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Posted 2011-05-09 21:53:07 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
/**
 *   Collection type a bit like ArrayList but does not preserve the order
 *   of its entities, speedwise it is very good, especially suited for games.
 */


public class Bag {
   
   private Object[] data;
   private int size = 0;
   
   /**
    * Constructs an empty Bag with an initial capacity of ten.
    *
    */

   public Bag() {
      this(10);
   }
   
   /**
     * Constructs an empty Bag with the specified initial capacity.
     *
     * @param capacity the initial capacity of Bag
     */

   public Bag(int capacity) {
      data = new Object[capacity];
   }
   
   /**
    * Removes the element at the specified position in this Bag.
    * does this by overwriting it was last element then removing
    * last element
    *
    * @param index the index of element to be removed
    * @return element that was removed from the Bag
    */

   public Object remove(int index) {
      Object o = data[index]; // make copy of element to remove so it can be returned
     data[index] = data[--size]; // overwrite item to remove with last element
     data[size] = null; // null last element, so gc can do its work
     return o;
   }
   
   /**
    * Removes the first occurrence of the specified element from this Bag,
     * if it is present.  If the Bag does not contain the element, it is
     * unchanged. does this by overwriting it was last element then removing
    * last element
    *
    * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
    */

   public boolean remove(Object o) {
      for (int i = 0; i < size; i++) {
         if (o == data[i]) {
            data[i] = data[--size]; // overwrite item to remove with last element
           data[size] = null; // null last element, so gc can do its work
           return true;
         }
      }
     
      return false;
   }
   
   /**
     * Removes from this Bag all of its elements that are contained in the
     * specified Bag.
     *
     * @param bag Bag containing elements to be removed from this Bag
     * @return {@code true} if this Bag changed as a result of the call
     */

    public boolean removeAll(Bag bag) {
       boolean modified = false;
       
       for (int i = 0; i < bag.size(); i++) {
          Object o1 = bag.get(i);
         
          for (int j = 0; j < size; j++) {
             Object o2 = data[j];
             
             if (o1 == o2) {
                remove(j);
                j--;
                modified = true;
                break;
             }
          }
       }
       
       return modified;
    }
   
    /**
     * Returns the element at the specified position in Bag.
     *
     * @param  index index of the element to return
     * @return the element at the specified position in bag
     */

   public Object get(int index) {
      return data[index];
   }

   /**
     * Returns the number of elements in this bag.
     *
     * @return the number of elements in this bag
     */

    public int size() {
       return size;
    }

    /**
     * Returns true if this list contains no elements.
     *
     * @return true if this list contains no elements
     */

    public boolean isEmpty() {
       return size == 0;
    }

    /**
     * Adds the specified element to the end of this bag.
     * if needed also increases the capacity of the bag.
     *
     * @param o element to be added to this list
     */

   public void add(Object o) {  
      // if size greater than data capacity increase capacity
     if(size == data.length) {
         grow();
      }
     
      data[size++] = o;
   }
   
    private void grow() {
      Object []oldData = data;
      int newCapacity = (oldData.length * 3) / 2 + 1;
      data = new Object[newCapacity];
      System.arraycopy(oldData, 0, data, 0, oldData.length);
    }
   
   /**
     * Removes all of the elements from this bag. The bag will
     * be empty after this call returns.
     */

    public void clear() {
       // null all elements so gc can clean up
      for (int i = 0; i < size; i++) {
          data[i] = null;
       }

       size = 0;
    }
}
Offline pjt33
« Reply #1 - Posted 2011-05-09 21:57:27 »

Nice trick. Why not implement Collection?
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #2 - Posted 2011-05-09 22:15:36 »

yeh good idea, but would add a bit of bloat to the class as its not needed in most cases where you'd use a bag. Having said that when you do need it, its really simple to just add.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (30 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (43 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!