Skip to content

Commit

Permalink
Added more efficient constructor to array-based sets
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed May 20, 2020
1 parent 879ff17 commit 31c13ab
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
was allowing a load factor of 1. Thanks to Andy Clegg for
reporting this bug.

- The copy constructor of array-based maps are now more efficient.
- The copy constructor of array-based containers are now more efficient.
Thanks to Vladimir Krivosheev for proposing this enhancement.

8.3.1
Expand Down
27 changes: 27 additions & 0 deletions drv/ArraySet.drv
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package PACKAGE;

import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.Set;

/** A simple, brute-force implementation of a set based on a backing array.
*
Expand Down Expand Up @@ -72,6 +73,32 @@ public class ARRAY_SET KEY_GENERIC extends ABSTRACT_SET KEY_GENERIC implements j
addAll(c);
}

/** Creates a new array set copying the contents of a given collection.
* @param c a collection.
*/
public ARRAY_SET(SET KEY_GENERIC c) {
this(c.size());
int i = 0;
for(KEY_TYPE x : c) {
a[i] = x;
i++;
}
size = i;
}

/** Creates a new array set copying the contents of a given set.
* @param c a collection.
*/
public ARRAY_SET(final Set<? extends KEY_GENERIC_CLASS> c) {
this(c.size());
int i = 0;
for(KEY_GENERIC_CLASS x: c) {
a[i] = KEY_CLASS2TYPE(x);
i++;
}
size = i;
}


/** Creates a new array set using the given backing array and the given number of elements of the array.
*
Expand Down
32 changes: 32 additions & 0 deletions test/it/unimi/dsi/fastutil/ints/Int2IntArrayMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package it.unimi.dsi.fastutil.ints;

/*
* Copyright (C) 2020 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import static org.junit.Assert.assertEquals;

import java.util.HashMap;

import org.junit.Test;

public class Int2IntArrayMapTest {
@Test
public void testCopyConstructor() {
final Int2IntOpenHashMap m = new Int2IntOpenHashMap(new int [] {1, 2}, new int[] {3, 4});
assertEquals(new Int2IntArrayMap(m), m);
assertEquals(new HashMap<>(m), m);
}
}
22 changes: 15 additions & 7 deletions test/it/unimi/dsi/fastutil/ints/IntArraySetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@
* limitations under the License.
*/

import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.io.BinIO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

import org.junit.Test;

import static org.junit.Assert.*;
import it.unimi.dsi.fastutil.io.BinIO;

public class IntArraySetTest {

@Test
public void testCopyConstructor() {
final IntOpenHashSet s = new IntOpenHashSet(new int[] { 1, 2 });
assertEquals(new IntArraySet(s), s);
assertEquals(new HashSet<>(s), s);
}

@SuppressWarnings("boxing")
@Test
public void testNullInEquals() {
Expand Down Expand Up @@ -70,7 +78,7 @@ public void testSet() {

@Test
public void testClone() {
IntArraySet s = new IntArraySet();
final IntArraySet s = new IntArraySet();
assertEquals(s, s.clone());
s.add(0);
assertEquals(s, s.clone());
Expand All @@ -86,8 +94,8 @@ public void testClone() {

@Test
public void testSerialisation() throws IOException, ClassNotFoundException {
IntArraySet s = new IntArraySet();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final IntArraySet s = new IntArraySet();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(s);
oos.close();
Expand Down

0 comments on commit 31c13ab

Please sign in to comment.