List of usage examples for java.lang Integer bitCount
@HotSpotIntrinsicCandidate public static int bitCount(int i)
From source file:com.cloudera.oryx.app.serving.als.model.LocalitySensitiveHashTest.java
@Test public void testCandidateIndices() { int features = 10; LocalitySensitiveHash lsh = new LocalitySensitiveHash(0.5, features, 32); assertEquals(3, lsh.getMaxBitsDiffering()); assertEquals(7, lsh.getNumHashes()); float[] oneVec = new float[features]; Arrays.fill(oneVec, 1.0f);/* w w w. j av a2 s .c o m*/ int[] candidates = lsh.getCandidateIndices(oneVec); assertEquals(64, candidates.length); // 1 + 7 + 21 + 35 for (int i = 1; i < 8; i++) { assertEquals(1, Integer.bitCount(candidates[0] ^ candidates[i])); } for (int i = 8; i < 29; i++) { assertEquals(2, Integer.bitCount(candidates[0] ^ candidates[i])); } for (int i = 29; i < 64; i++) { assertEquals(3, Integer.bitCount(candidates[0] ^ candidates[i])); } }
From source file:spark_example.matrix_decomposition.LocalitySensitiveHash.java
LocalitySensitiveHash(double sampleRate, int numFeatures, int numCores) { // How many hash functions to use? use as few as possible that still achieve the desired sample // rate or less, approximately. int numHashes = 0; int bitsDiffering = 0; for (; numHashes < MAX_HASHES; numHashes++) { // For a given number of hashes, consider partitions differing from the target hash in how many bits? // Choose enough such that number to test is as large as possible while <= the number of cores bitsDiffering = 0;// www.jav a 2s . c om // Number of different partitions that are examined when allowing the given number of bits to differ long numPartitionsToTry = 1; // Make bitsDiffering as large as possible given number of cores while (bitsDiffering < numHashes && numPartitionsToTry < numCores) { // There are numHashes-choose-bitsDiffering ways for numHashes bits to differ in // exactly bitsDiffering bits bitsDiffering++; numPartitionsToTry += CombinatoricsUtils.binomialCoefficient(numHashes, bitsDiffering); } // Note that this allows numPartitionsToTry to overshoot numCores by one step if (bitsDiffering == numHashes && numPartitionsToTry < numCores) { // Can't keep busy enough; keep going continue; } // Consider what fraction of all 2^n partitions is then considered, as a proxy for the // sample rate // Stop as soon as it's <= target sample rate if (numPartitionsToTry <= sampleRate * (1L << numHashes)) { break; } } log.info("LSH with {} hashes, querying partitions with up to {} bits differing", numHashes, bitsDiffering); this.maxBitsDiffering = bitsDiffering; hashVectors = new double[numHashes][]; RandomGenerator random = RandomManager.getRandom(); for (int i = 0; i < numHashes; i++) { // Pick the most-orthogonal next random vector double bestTotalDot = Double.POSITIVE_INFINITY; double[] nextBest = null; // Try, like, lots of them int candidatesSinceBest = 0; while (candidatesSinceBest < 1000) { double[] candidate = VectorMath.randomVectorD(numFeatures, random); // measure by total (absolute) dot product double score = totalAbsCos(hashVectors, i, candidate); if (score < bestTotalDot) { nextBest = candidate; // Stop if best possible score if (score == 0.0) { break; } bestTotalDot = score; candidatesSinceBest = 0; } else { candidatesSinceBest++; } } hashVectors[i] = nextBest; } log.info("Chose {} random hash vectors", hashVectors.length); // Contains all 2^numHashes integers from 0. The first element has 0 bits set. The next numHashes elements // are all such integers with 1 bit sets. Then 2 bits, and so on. This is used as a "mask" on top of an // initial candidate index in order to construct results in getCandidateIndices() candidateIndicesPrototype = new int[1 << numHashes]; int[] offsetPerBitsActive = new int[numHashes + 1]; for (int i = 1; i <= numHashes; i++) { offsetPerBitsActive[i] = offsetPerBitsActive[i - 1] + (int) CombinatoricsUtils.binomialCoefficient(numHashes, i - 1); } for (int i = 0; i < candidateIndicesPrototype.length; i++) { candidateIndicesPrototype[offsetPerBitsActive[Integer.bitCount(i)]++] = i; } // Contains all 2^numHashes integers from 0 allIndices = new int[1 << numHashes]; for (int i = 0; i < allIndices.length; i++) { allIndices[i] = i; } }
From source file:com.cloudera.oryx.app.serving.als.model.LocalitySensitiveHash.java
LocalitySensitiveHash(double sampleRate, int numFeatures, int numCores) { // How many hash functions to use? use as few as possible that still achieve the desired sample // rate or less, approximately. int numHashes = 0; int bitsDiffering = 0; for (; numHashes < MAX_HASHES; numHashes++) { // For a given number of hashes, consider partitions differing from the target hash in how many bits? // Choose enough such that number to test is as large as possible while <= the number of cores bitsDiffering = 0;// w ww.ja v a 2 s. c o m // Number of different partitions that are examined when allowing the given number of bits to differ long numPartitionsToTry = 1; // Make bitsDiffering as large as possible given number of cores while (bitsDiffering < numHashes && numPartitionsToTry < numCores) { // There are numHashes-choose-bitsDiffering ways for numHashes bits to differ in // exactly bitsDiffering bits bitsDiffering++; numPartitionsToTry += CombinatoricsUtils.binomialCoefficient(numHashes, bitsDiffering); } // Note that this allows numPartitionsToTry to overshoot numCores by one step if (bitsDiffering == numHashes && numPartitionsToTry < numCores) { // Can't keep busy enough; keep going continue; } // Consider what fraction of all 2^n partitions is then considered, as a proxy for the // sample rate // Stop as soon as it's <= target sample rate if (numPartitionsToTry <= sampleRate * (1L << numHashes)) { break; } } log.info("LSH with {} hashes, querying partitions with up to {} bits differing", numHashes, bitsDiffering); this.maxBitsDiffering = bitsDiffering; hashVectors = new float[numHashes][]; RandomGenerator random = RandomManager.getRandom(); for (int i = 0; i < numHashes; i++) { // Pick the most-orthogonal next random vector double bestTotalDot = Double.POSITIVE_INFINITY; float[] nextBest = null; // Try, like, lots of them int candidatesSinceBest = 0; while (candidatesSinceBest < 1000) { float[] candidate = VectorMath.randomVectorF(numFeatures, random); // measure by total (absolute) dot product double score = totalAbsCos(hashVectors, i, candidate); if (score < bestTotalDot) { nextBest = candidate; // Stop if best possible score if (score == 0.0) { break; } bestTotalDot = score; candidatesSinceBest = 0; } else { candidatesSinceBest++; } } hashVectors[i] = nextBest; } log.info("Chose {} random hash vectors", hashVectors.length); // Contains all 2^numHashes integers from 0. The first element has 0 bits set. The next numHashes elements // are all such integers with 1 bit sets. Then 2 bits, and so on. This is used as a "mask" on top of an // initial candidate index in order to construct results in getCandidateIndices() candidateIndicesPrototype = new int[1 << numHashes]; int[] offsetPerBitsActive = new int[numHashes + 1]; for (int i = 1; i <= numHashes; i++) { offsetPerBitsActive[i] = offsetPerBitsActive[i - 1] + (int) CombinatoricsUtils.binomialCoefficient(numHashes, i - 1); } for (int i = 0; i < candidateIndicesPrototype.length; i++) { candidateIndicesPrototype[offsetPerBitsActive[Integer.bitCount(i)]++] = i; } // Contains all 2^numHashes integers from 0 allIndices = new int[1 << numHashes]; for (int i = 0; i < allIndices.length; i++) { allIndices[i] = i; } }
From source file:ticwear.design.app.DatetimePickerDialog.java
/** * @param context The context the dialog is to run in. * @param theme the theme to apply to this dialog * @param pageFlag Witch page will show. * @param defaultCalendar The initial datetime of the dialog. */// ww w . ja va 2 s .c o m public DatetimePickerDialog(Context context, @StyleRes int theme, int pageFlag, Calendar defaultCalendar) { super(context, resolveDialogTheme(context, theme)); // Use getContext to use wrapper context. context = getContext(); mCurrentCalendar = defaultCalendar; mCurrentCalendar.clear(Calendar.SECOND); boolean hasDateView = (pageFlag & PAGE_FLAG_DATE) == PAGE_FLAG_DATE; boolean hasTimeView = (pageFlag & PAGE_FLAG_TIME) == PAGE_FLAG_TIME; int year = defaultCalendar.get(Calendar.YEAR); int month = defaultCalendar.get(Calendar.MONTH); int day = defaultCalendar.get(Calendar.DAY_OF_MONTH); int hour = defaultCalendar.get(Calendar.HOUR_OF_DAY); int minute = defaultCalendar.get(Calendar.MINUTE); ValidationCallback validationCallback = new ValidationCallback() { @Override public void onValidationChanged(boolean valid) { final Button positive = getButton(BUTTON_POSITIVE); if (positive != null) { positive.setEnabled(valid); } } }; final Context themeContext = getContext(); final LayoutInflater inflater = LayoutInflater.from(themeContext); TicklableFrameLayout container = (TicklableFrameLayout) inflater.inflate(R.layout.dialog_datetime_picker, null); container.setSidePanelEventDispatcher(this); mViewPager = (ViewPager) container.findViewById(R.id.tic_datetimeContainer); List<View> pages = new ArrayList<>(Integer.bitCount(pageFlag)); if (hasDateView) { mDatePickerViewHolder = new DatePickerViewHolder(context); DatePicker dateView = mDatePickerViewHolder.init(mViewPager, year, month, day, this, validationCallback); dateView.setMultiPickerClient(this); dateView.setTag(R.id.title_template, R.string.date_picker_dialog_title); pages.add(dateView); } if (hasTimeView) { mTimePickerViewHolder = new TimePickerViewHolder(context); TimePicker timeView = mTimePickerViewHolder.init(mViewPager, hour, minute, DateFormat.is24HourFormat(context), this, validationCallback); timeView.setMultiPickerClient(this); timeView.setTag(R.id.title_template, R.string.time_picker_dialog_title); pages.add(timeView); } mPagerAdapter = new PickerPagerAdapter(pages); mViewPager.setAdapter(mPagerAdapter); mPageIndicator = (CirclePageIndicator) container.findViewById(R.id.tic_datetimeIndicator); mPageIndicator.setViewPager(mViewPager); mPageIndicator.setOnPageChangeListener(this); if (mPagerAdapter.getCount() < 2) { mPageIndicator.setVisibility(View.GONE); } setView(container); setButton(BUTTON_POSITIVE, getContext().getDrawable(R.drawable.tic_ic_btn_next), this); setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); setTitle(mPagerAdapter.getPageTitle(0)); }
From source file:org.apache.drill.exec.physical.impl.join.HashJoinProbeTemplate.java
/** * Setup the Hash Join Probe object/*from w ww. j av a2 s .co m*/ * * @param probeBatch * @param outgoing * @param joinRelType * @param semiJoin * @param leftStartState * @param partitions * @param cycleNum * @param container * @param spilledInners * @param buildSideIsEmpty * @param numPartitions * @param rightHVColPosition */ @Override public void setupHashJoinProbe(RecordBatch probeBatch, HashJoinBatch outgoing, JoinRelType joinRelType, boolean semiJoin, IterOutcome leftStartState, HashPartition[] partitions, int cycleNum, VectorContainer container, HashJoinBatch.HashJoinSpilledPartition[] spilledInners, boolean buildSideIsEmpty, int numPartitions, int rightHVColPosition) { this.container = container; this.spilledInners = spilledInners; this.probeBatch = probeBatch; this.probeSchema = probeBatch.getSchema(); this.joinType = joinRelType; this.outgoingJoinBatch = outgoing; this.partitions = partitions; this.cycleNum = cycleNum; this.buildSideIsEmpty = buildSideIsEmpty; this.numPartitions = numPartitions; this.numberOfBuildSideColumns = semiJoin ? 0 : rightHVColPosition; // position (0 based) of added column == #columns this.semiJoin = semiJoin; partitionMask = numPartitions - 1; // e.g. 32 --> 0x1F bitsInMask = Integer.bitCount(partitionMask); // e.g. 0x1F -> 5 joinControl = new JoinControl(((HashJoinPOP) outgoingJoinBatch.getPopConfig()).getJoinControl()); probeState = ProbeState.PROBE_PROJECT; this.recordsToProcess = 0; this.recordsProcessed = 0; // A special case - if the left was an empty file if (leftStartState == IterOutcome.NONE) { changeToFinalProbeState(); } else { this.recordsToProcess = probeBatch.getRecordCount(); } // for those outer partitions that need spilling (cause their matching inners spilled) // initialize those partitions' current batches and hash-value vectors for (HashPartition partn : this.partitions) { partn.allocateNewCurrentBatchAndHV(); } currRightPartition = 0; // In case it's a Right/Full outer join // Initialize the HV vector for the first (already read) left batch if (this.cycleNum > 0) { if (read_left_HV_vector != null) { read_left_HV_vector.clear(); } if (leftStartState != IterOutcome.NONE) { // Skip when outer spill was empty read_left_HV_vector = (IntVector) probeBatch.getContainer().getLast(); } } }
From source file:org.springframework.integration.kafka.listener.KafkaMessageListenerContainer.java
/** * The maximum number of messages that are buffered by each concurrent {@link MessageListener} runner. * Increasing the value may increase throughput, but also increases the memory consumption. * Must be a positive number and a power of 2. * @param queueSize the queue size//w w w. j av a 2s. c o m */ public void setQueueSize(int queueSize) { Assert.isTrue(queueSize > 0 && Integer.bitCount(queueSize) == 1, "'queueSize' must be a positive number and a power of 2"); this.queueSize = queueSize; }
From source file:org.bigtextml.topics.ParallelTopicModel.java
public ParallelTopicModel(BigLabelAlphabet topicAlphabet, double alphaSum, double beta) { //this.id=System.currentTimeMillis(); //this.data = new ArrayList<TopicAssignment>(); //this.data = CacheManagementServices.cacheManager.getCache("topicass"); this.data = (TopicAssignmentBigMap) ManagementServices.getBigMap("TopicAssignment"); this.topicAlphabet = topicAlphabet; this.numTopics = topicAlphabet.size(); if (Integer.bitCount(numTopics) == 1) { // exact power of 2 topicMask = numTopics - 1;/*from w w w.ja va2 s .c o m*/ topicBits = Integer.bitCount(topicMask); } else { // otherwise add an extra bit topicMask = Integer.highestOneBit(numTopics) * 2 - 1; topicBits = Integer.bitCount(topicMask); } this.alphaSum = alphaSum; this.alpha = new double[numTopics]; Arrays.fill(alpha, alphaSum / numTopics); this.beta = beta; tokensPerTopic = new int[numTopics]; formatter = NumberFormat.getInstance(); formatter.setMaximumFractionDigits(5); logger.info("Coded LDA: " + numTopics + " topics, " + topicBits + " topic bits, " + Integer.toBinaryString(topicMask) + " topic mask"); }
From source file:org.apache.hadoop.hive.llap.cache.BuddyAllocator.java
public int validateAndDetermineArenaSize(int arenaCount, long maxSizeVal) { long arenaSizeVal = (arenaCount == 0) ? MAX_ARENA_SIZE : maxSizeVal / arenaCount; // The math.min, and the fact that maxAllocation is an int, ensures we don't overflow. arenaSizeVal = Math.max(maxAllocation, Math.min(arenaSizeVal, MAX_ARENA_SIZE)); if (LlapIoImpl.LOG.isInfoEnabled()) { LlapIoImpl.LOG.info("Buddy allocator with " + (isDirect ? "direct" : "byte") + " buffers; " + (isMapped ? ("memory mapped off " + cacheDir.toString() + "; ") : "") + "allocation sizes " + minAllocation + " - " + maxAllocation + ", arena size " + arenaSizeVal + ", total size " + maxSizeVal);// w w w . j a va 2s. c om } String minName = ConfVars.LLAP_ALLOCATOR_MIN_ALLOC.varname, maxName = ConfVars.LLAP_ALLOCATOR_MAX_ALLOC.varname; if (minAllocation < 8) { throw new RuntimeException(minName + " must be at least 8 bytes: " + minAllocation); } if (maxSizeVal < maxAllocation || maxAllocation < minAllocation) { throw new RuntimeException("Inconsistent sizes; expecting " + minName + " <= " + maxName + " <= " + ConfVars.LLAP_IO_MEMORY_MAX_SIZE.varname + "; configured with min=" + minAllocation + ", max=" + maxAllocation + " and total=" + maxSizeVal); } if ((Integer.bitCount(minAllocation) != 1) || (Integer.bitCount(maxAllocation) != 1)) { throw new RuntimeException("Allocation sizes must be powers of two; configured with " + minName + "=" + minAllocation + ", " + maxName + "=" + maxAllocation); } if ((arenaSizeVal % maxAllocation) > 0) { long oldArenaSize = arenaSizeVal; arenaSizeVal = (arenaSizeVal / maxAllocation) * maxAllocation; LlapIoImpl.LOG.warn("Rounding arena size to " + arenaSizeVal + " from " + oldArenaSize + " to be divisible by allocation size " + maxAllocation); } return (int) arenaSizeVal; }
From source file:com.offbynull.voip.kademlia.model.RouteTree.java
private RouteTreeNode createRoot(RouteTreeBranchStrategy branchStrategy, RouteTreeBucketStrategy bucketStrategy) { Validate.notNull(branchStrategy);// w w w . j ava 2 s . com Validate.notNull(bucketStrategy); // Get number of branches/buckets to create for root int numOfBuckets = branchStrategy.getBranchCount(EMPTY); Validate.isTrue(numOfBuckets >= 0, "Branch cannot be negative, was %d", numOfBuckets); Validate.isTrue(numOfBuckets != 0, "Root of tree must contain at least 1 branch, was %d", numOfBuckets); Validate.isTrue(Integer.bitCount(numOfBuckets) == 1, "Branch count must be power of 2"); int suffixBitCount = Integer.bitCount(numOfBuckets - 1); // num of bits e.g. 8 --> 1000 - 1 = 0111, bitcount(0111) = 3 Validate.isTrue(suffixBitCount <= baseId.getBitLength(), "Attempting to branch too far (in root) %d bits extends past %d bits", suffixBitCount, baseId.getBitLength()); // Create buckets by creating a 0-sized top bucket and splitting it + resizing each split KBucket[] newBuckets = new KBucket(baseId, EMPTY, 0, 0).split(suffixBitCount); for (int i = 0; i < newBuckets.length; i++) { BucketParameters bucketParams = bucketStrategy.getBucketParameters(newBuckets[i].getPrefix()); int bucketSize = bucketParams.getBucketSize(); int cacheSize = bucketParams.getCacheSize(); newBuckets[i].resizeBucket(bucketSize); newBuckets[i].resizeCache(cacheSize); // insert last bucket activity time in to bucket update times... it may be null if bucket has never been accessed, in which case // we insert MIN instead Instant lastBucketActivityTime = newBuckets[i].getLatestBucketActivityTime(); if (lastBucketActivityTime == null) { lastBucketActivityTime = Instant.MIN; } bucketUpdateTimes.insert(lastBucketActivityTime, newBuckets[i].getPrefix()); } // Create root return new RouteTreeNode(EMPTY, suffixBitCount, newBuckets); }
From source file:org.apache.hadoop.util.TestGSet.java
private static boolean isPowerOfTwo(int num) { return num == 0 || (num > 0 && Integer.bitCount(num) == 1); }