Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  HashSet  (Read 1776 times)
0 Members and 1 Guest are viewing this topic.
Offline counterp

Senior Devvie


Medals: 11



« Posted 2011-09-09 03:46:39 »

wrote in an hour, went through several changes, maybe there's some inconsistent/bad code, point it out for me if so.

it's a 'hashset'. it's pretty good. has add, remove, contains, clear, size, and iterator, let me know if you need anything else added

it's not really a set though, since i skipped duplicate checking to gain speed. it would be trivial to add it however, and usually i don't have a case where i am working with duplicate objects.. (well actually I've never had a case with this, having an unordered collection was the important bit for me) and  that's why i decided not to include

don't use negative hashes.


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  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
/*       DO WHAT THE F(U)CK YOU WANT TO PUBLIC LICENSE
 *               Version 2, December 2004
 *
 * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
 *
 * Everyone is permitted to copy and distribute verbatim or modified
 * copies of this license document, and changing it is allowed as long
 * as the name is changed.
 *
 *          DO WHAT THE F(U)CK YOU WANT TO PUBLIC LICENSE
 *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 *
 * 0. You just DO WHAT THE F(U)CK YOU WANT TO.
 */


import java.lang.reflect.Array;
import java.util.Iterator;

public class HashStruct<T> {

   public HashStruct() {
      this(1024);
   }

   @SuppressWarnings("unchecked")
   public HashStruct(int estimatedMaxElements) {
      elements = (Node[]) Array.newInstance(Node.class, estimatedMaxElements);
   }

   private Node[] elements;

   private int size = 0;

   public final boolean contains(T object) {
      if (size == 0)
         return false;

      Node node = elements[object.hashCode() % elements.length];

      if (node == null)
         return false;

      while (node.value != object) {
         node = node.next;
         if (node == null)
            return false;
      }

      return true;
   }

   public final int size() {
      return size;
   }

   public final void add(T object) {
      int n = object.hashCode() % elements.length;

      Node node = elements[n];

      size++;

      if (node == null) {
         elements[n] = new Node(object);
         return;
      }

      Node temp = new Node(object);
      temp.next = node;
      elements[n] = temp;
   }

   @SuppressWarnings("unchecked")
   public final void clear() {
      size = 0;
      elements = (Node[]) Array.newInstance(Node.class, elements.length);
   }

   public final void remove(T object) {
      int n = object.hashCode() % elements.length;

      Node node = elements[n];

      if (node == null)
         return;

      if (node.next == null) {
         elements[n] = null;
         size--;
         return;
      }


      Node previous = null;
      while (node.value != object) {
         previous = node;
         node = node.next;
         if (node == null)
            return;
      }

      if (previous != null)
         previous.next = node.next;
      else
         elements[n] = node.next;

      size--;
   }

   public final Iterator<T> iterator() {
      return new HashStructIterator();
   }

   private final class Node {

      public Node(T value) {
         this.value = value;
      }

      public final T value;

      public Node next;
     
   }

   private final class HashStructIterator implements Iterator<T> {

      public HashStructIterator() {
         initialSize = size;
         if (initialSize != 0)
            for (int i = 0; i < elements.length; i++)
               if (elements[i] != null) {
                  pos = i;
                  current = elements[i];
                  return;
               }
         pos = elements.length;
      }

      private final int initialSize;

      private int pos;
      private int read;

      private Node current;
      private Node last;

      @Override
      public final boolean hasNext() {
         if (!seek())
            return false;

         return pos != elements.length;
      }

      @Override
      public final T next() {
         if (!seek())
            return null;

         read++;

         T next = current.value;
         last = current;
         current = current.next;
         return next;
      }

      @Override
      public final void remove() {
         if (last == null || pos == elements.length)
            throw new IllegalStateException("no element to remove!");

         size--;

         if (current == null) {
            elements[pos] = null;
            return;
         }

         last.next = current.next;
      }

      private final boolean seek() {
         if (pos == elements.length)
            return false;
         if (read == initialSize) {
            pos = elements.length;
            return false;
         }

         if (current == null) {
            for (int i = pos + 1; i < elements.length; i++)
               if (elements[i] != null) {
                  pos = i;
                  current = elements[i];
                  return true;
               }
            pos = elements.length;
            return false;
         }

         return true;
      }

   }

}


add = O(1)
remove = best case O(1), worst case O(n)
contains = best case O(1), worst case O(n)
iterator = O(n)

(best case is no collisions, worst case is ONLY having collisions
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2011-10-04 16:28:09 »

Just curious, how is this compared to HashSet in the JDK?
Offline counterp

Senior Devvie


Medals: 11



« Reply #2 - Posted 2011-10-04 18:33:47 »

all methods included range from faster to much faster than HashSet included in API (don't remember how fast, benched right after making so it was a while ago)
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.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!