Array Int Set : Array Collections « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Array Collections 




Array Int Set
   

/*
 * Copyright (c) 2002-2008 "Neo Technology,"
 *     Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

// use array for first few properties to decrease memory footprint (and
// to some extent boost performance) for nodes/rels with few properties
public class ArrayIntSet
{
    private int maxRelSize = 256;
    private int[] rels = new int[2];

    // TODO: figure out if we need volatile here?
    private int arrayCount = 0;

    private Set<Integer> relationshipSet = null;

    public boolean addint id )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if rels[i== id )
            {
                return false;
            }
        }
        if arrayCount == rels.length && rels.length * <= maxRelSize )
        {
            int newRels[] new int[rels.length * 2];
            System.arraycopyrels, 0, newRels, 0, rels.length );
            rels = newRels;
        }
        if arrayCount != -)
        {
            if arrayCount < rels.length )
            {
                rels[arrayCount++= id;
                return true;
            }
            relationshipSet = new HashSet<Integer>();
            for int i = 0; i < arrayCount; i++ )
            {
                relationshipSet.addrels[i] );
            }
            arrayCount = -1;
        }
        return relationshipSet.addid );
    }

    public Iterator<Integer> iterator()
    {
        if arrayCount == -)
        {
            return relationshipSet.iterator();
        }
        return new ArrayIntIteratorrels, arrayCount );
    }

    public boolean removeint id )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if rels[i== id )
            {
                int[] dest = rels;
                if arrayCount - < rels.length / )
                {
                    dest = new int[rels.length / 2];
                    System.arraycopyrels, 0, dest, 0, arrayCount );
                }
                if i + < dest.length && (arrayCount - i - 1)
                {
                    System.arraycopyrels, i + 1, dest, i, arrayCount - i - );
                    rels = dest;
                }
                arrayCount--;
                return true;
            }
        }
        if arrayCount == -)
        {
            return relationshipSet.removeid );
        }
        return false;
    }

    public Iterable<Integer> values()
    {
        if arrayCount == -)
        {
            return relationshipSet;
        }
        return new ArrayIntIteratorrels, arrayCount );
    }

    private static class ArrayIntIterator implements Iterator<Integer>,
        Iterable<Integer>
    {
        private int[] intArray;
        private int pos = -1;
        private int arrayCount;

        ArrayIntIteratorint[] array, int count )
        {
            this.intArray = array;
            this.arrayCount = count;
        }

        public boolean hasNext()
        {
            return pos + < arrayCount;
        }

        public Integer next()
        {
            return intArray[++pos];
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }

        public Iterator<Integer> iterator()
        {
            return this;
        }
    }

    public boolean containsint id )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if rels[i== id )
            {
                return true;
            }
        }
        if arrayCount == -)
        {
            return relationshipSet.containsid );
        }
        return false;
    }

    public int size()
    {
        if arrayCount != -)
        {
            return arrayCount;
        }
        return relationshipSet.size();
    }
}

   
    
    
  














Related examples in the same category
1.Array Iterator
2.Array MapArray Map
3.Array SetArray Set
4.Remove duplicate element from array
5.Convert an Array to a List
6.Converting an Array to a Collection
7.Converting a Collection of user objects to an Array
8.Create an array containing the elements in a set
9.Convert an array to a Map
10.Converting a Collection of String to an ArrayConverting a Collection of String to an Array
11.Treating an Array as an Enumeration
12.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
13.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
14.Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
15.Converts array into a java.util.Map.
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.