duke@435: /* kvn@2557: * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "opto/chaitin.hpp" stefank@2314: #include "opto/compile.hpp" stefank@2314: #include "opto/indexSet.hpp" stefank@2314: #include "opto/regmask.hpp" stefank@2314: duke@435: // This file defines the IndexSet class, a set of sparse integer indices. duke@435: // This data structure is used by the compiler in its liveness analysis and duke@435: // during register allocation. It also defines an iterator for this class. duke@435: duke@435: //-------------------------------- Initializations ------------------------------ duke@435: duke@435: IndexSet::BitBlock IndexSet::_empty_block = IndexSet::BitBlock(); duke@435: duke@435: #ifdef ASSERT duke@435: // Initialize statistics counters kvn@2557: julong IndexSet::_alloc_new = 0; kvn@2557: julong IndexSet::_alloc_total = 0; duke@435: kvn@2557: julong IndexSet::_total_bits = 0; kvn@2557: julong IndexSet::_total_used_blocks = 0; kvn@2557: julong IndexSet::_total_unused_blocks = 0; duke@435: duke@435: // Per set, or all sets operation tracing duke@435: int IndexSet::_serial_count = 1; duke@435: #endif duke@435: duke@435: // What is the first set bit in a 5 bit integer? duke@435: const byte IndexSetIterator::_first_bit[32] = { duke@435: 0, 0, 1, 0, duke@435: 2, 0, 1, 0, duke@435: 3, 0, 1, 0, duke@435: 2, 0, 1, 0, duke@435: 4, 0, 1, 0, duke@435: 2, 0, 1, 0, duke@435: 3, 0, 1, 0, duke@435: 2, 0, 1, 0 duke@435: }; duke@435: duke@435: // What is the second set bit in a 5 bit integer? duke@435: const byte IndexSetIterator::_second_bit[32] = { duke@435: 5, 5, 5, 1, duke@435: 5, 2, 2, 1, duke@435: 5, 3, 3, 1, duke@435: 3, 2, 2, 1, duke@435: 5, 4, 4, 1, duke@435: 4, 2, 2, 1, duke@435: 4, 3, 3, 1, duke@435: 3, 2, 2, 1 duke@435: }; duke@435: duke@435: // I tried implementing the IndexSetIterator with a window_size of 8 and duke@435: // didn't seem to get a noticeable speedup. I am leaving in the tables duke@435: // in case we want to switch back. duke@435: duke@435: /*const byte IndexSetIterator::_first_bit[256] = { duke@435: 8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, duke@435: 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 duke@435: }; duke@435: duke@435: const byte IndexSetIterator::_second_bit[256] = { duke@435: 8, 8, 8, 1, 8, 2, 2, 1, 8, 3, 3, 1, 3, 2, 2, 1, duke@435: 8, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 8, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, duke@435: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 8, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1, duke@435: 6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, duke@435: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 8, 7, 7, 1, 7, 2, 2, 1, 7, 3, 3, 1, 3, 2, 2, 1, duke@435: 7, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 7, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, duke@435: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 7, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1, duke@435: 6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1, duke@435: 6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1, duke@435: 5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1 duke@435: };*/ duke@435: duke@435: //---------------------------- IndexSet::populate_free_list() ----------------------------- duke@435: // Populate the free BitBlock list with a batch of BitBlocks. The BitBlocks duke@435: // are 32 bit aligned. duke@435: duke@435: void IndexSet::populate_free_list() { duke@435: Compile *compile = Compile::current(); duke@435: BitBlock *free = (BitBlock*)compile->indexSet_free_block_list(); duke@435: duke@435: char *mem = (char*)arena()->Amalloc_4(sizeof(BitBlock) * duke@435: bitblock_alloc_chunk_size + 32); duke@435: duke@435: // Align the pointer to a 32 bit boundary. duke@435: BitBlock *new_blocks = (BitBlock*)(((uintptr_t)mem + 32) & ~0x001F); duke@435: duke@435: // Add the new blocks to the free list. duke@435: for (int i = 0; i < bitblock_alloc_chunk_size; i++) { duke@435: new_blocks->set_next(free); duke@435: free = new_blocks; duke@435: new_blocks++; duke@435: } duke@435: duke@435: compile->set_indexSet_free_block_list(free); duke@435: duke@435: #ifdef ASSERT duke@435: if (CollectIndexSetStatistics) { kvn@2557: inc_stat_counter(&_alloc_new, bitblock_alloc_chunk_size); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: duke@435: //---------------------------- IndexSet::alloc_block() ------------------------ duke@435: // Allocate a BitBlock from the free list. If the free list is empty, duke@435: // prime it. duke@435: duke@435: IndexSet::BitBlock *IndexSet::alloc_block() { duke@435: #ifdef ASSERT duke@435: if (CollectIndexSetStatistics) { kvn@2557: inc_stat_counter(&_alloc_total, 1); duke@435: } duke@435: #endif duke@435: Compile *compile = Compile::current(); duke@435: BitBlock* free_list = (BitBlock*)compile->indexSet_free_block_list(); duke@435: if (free_list == NULL) { duke@435: populate_free_list(); duke@435: free_list = (BitBlock*)compile->indexSet_free_block_list(); duke@435: } duke@435: BitBlock *block = free_list; duke@435: compile->set_indexSet_free_block_list(block->next()); duke@435: duke@435: block->clear(); duke@435: return block; duke@435: } duke@435: duke@435: //---------------------------- IndexSet::alloc_block_containing() ------------- duke@435: // Allocate a new BitBlock and put it into the position in the _blocks array duke@435: // corresponding to element. duke@435: duke@435: IndexSet::BitBlock *IndexSet::alloc_block_containing(uint element) { duke@435: BitBlock *block = alloc_block(); duke@435: uint bi = get_block_index(element); duke@435: _blocks[bi] = block; duke@435: return block; duke@435: } duke@435: duke@435: //---------------------------- IndexSet::free_block() ------------------------- duke@435: // Add a BitBlock to the free list. duke@435: duke@435: void IndexSet::free_block(uint i) { duke@435: debug_only(check_watch("free block", i)); duke@435: assert(i < _max_blocks, "block index too large"); duke@435: BitBlock *block = _blocks[i]; duke@435: assert(block != &_empty_block, "cannot free the empty block"); duke@435: block->set_next((IndexSet::BitBlock*)Compile::current()->indexSet_free_block_list()); duke@435: Compile::current()->set_indexSet_free_block_list(block); duke@435: set_block(i,&_empty_block); duke@435: } duke@435: duke@435: //------------------------------lrg_union-------------------------------------- duke@435: // Compute the union of all elements of one and two which interfere with duke@435: // the RegMask mask. If the degree of the union becomes exceeds duke@435: // fail_degree, the union bails out. The underlying set is cleared before duke@435: // the union is performed. duke@435: duke@435: uint IndexSet::lrg_union(uint lr1, uint lr2, duke@435: const uint fail_degree, duke@435: const PhaseIFG *ifg, duke@435: const RegMask &mask ) { duke@435: IndexSet *one = ifg->neighbors(lr1); duke@435: IndexSet *two = ifg->neighbors(lr2); duke@435: LRG &lrg1 = ifg->lrgs(lr1); duke@435: LRG &lrg2 = ifg->lrgs(lr2); duke@435: #ifdef ASSERT duke@435: assert(_max_elements == one->_max_elements, "max element mismatch"); duke@435: check_watch("union destination"); duke@435: one->check_watch("union source"); duke@435: two->check_watch("union source"); duke@435: #endif duke@435: duke@435: // Compute the degree of the combined live-range. The combined duke@435: // live-range has the union of the original live-ranges' neighbors set as duke@435: // well as the neighbors of all intermediate copies, minus those neighbors duke@435: // that can not use the intersected allowed-register-set. duke@435: duke@435: // Copy the larger set. Insert the smaller set into the larger. duke@435: if (two->count() > one->count()) { duke@435: IndexSet *temp = one; duke@435: one = two; duke@435: two = temp; duke@435: } duke@435: duke@435: clear(); duke@435: duke@435: // Used to compute degree of register-only interferences. Infinite-stack duke@435: // neighbors do not alter colorability, as they can always color to some duke@435: // other color. (A variant of the Briggs assertion) duke@435: uint reg_degree = 0; duke@435: duke@435: uint element; duke@435: // Load up the combined interference set with the neighbors of one duke@435: IndexSetIterator elements(one); duke@435: while ((element = elements.next()) != 0) { duke@435: LRG &lrg = ifg->lrgs(element); duke@435: if (mask.overlap(lrg.mask())) { duke@435: insert(element); duke@435: if( !lrg.mask().is_AllStack() ) { duke@435: reg_degree += lrg1.compute_degree(lrg); duke@435: if( reg_degree >= fail_degree ) return reg_degree; duke@435: } else { duke@435: // !!!!! Danger! No update to reg_degree despite having a neighbor. duke@435: // A variant of the Briggs assertion. duke@435: // Not needed if I simplify during coalesce, ala George/Appel. duke@435: assert( lrg.lo_degree(), "" ); duke@435: } duke@435: } duke@435: } duke@435: // Add neighbors of two as well duke@435: IndexSetIterator elements2(two); duke@435: while ((element = elements2.next()) != 0) { duke@435: LRG &lrg = ifg->lrgs(element); duke@435: if (mask.overlap(lrg.mask())) { duke@435: if (insert(element)) { duke@435: if( !lrg.mask().is_AllStack() ) { duke@435: reg_degree += lrg2.compute_degree(lrg); duke@435: if( reg_degree >= fail_degree ) return reg_degree; duke@435: } else { duke@435: // !!!!! Danger! No update to reg_degree despite having a neighbor. duke@435: // A variant of the Briggs assertion. duke@435: // Not needed if I simplify during coalesce, ala George/Appel. duke@435: assert( lrg.lo_degree(), "" ); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: return reg_degree; duke@435: } duke@435: duke@435: //---------------------------- IndexSet() ----------------------------- duke@435: // A deep copy constructor. This is used when you need a scratch copy of this set. duke@435: duke@435: IndexSet::IndexSet (IndexSet *set) { duke@435: #ifdef ASSERT duke@435: _serial_number = _serial_count++; duke@435: set->check_watch("copied", _serial_number); duke@435: check_watch("initialized by copy", set->_serial_number); duke@435: _max_elements = set->_max_elements; duke@435: #endif duke@435: _count = set->_count; duke@435: _max_blocks = set->_max_blocks; duke@435: if (_max_blocks <= preallocated_block_list_size) { duke@435: _blocks = _preallocated_block_list; duke@435: } else { duke@435: _blocks = duke@435: (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); duke@435: } duke@435: for (uint i = 0; i < _max_blocks; i++) { duke@435: BitBlock *block = set->_blocks[i]; duke@435: if (block == &_empty_block) { duke@435: set_block(i, &_empty_block); duke@435: } else { duke@435: BitBlock *new_block = alloc_block(); duke@435: memcpy(new_block->words(), block->words(), sizeof(uint32) * words_per_block); duke@435: set_block(i, new_block); duke@435: } duke@435: } duke@435: } duke@435: duke@435: //---------------------------- IndexSet::initialize() ----------------------------- duke@435: // Prepare an IndexSet for use. duke@435: duke@435: void IndexSet::initialize(uint max_elements) { duke@435: #ifdef ASSERT duke@435: _serial_number = _serial_count++; duke@435: check_watch("initialized", max_elements); duke@435: _max_elements = max_elements; duke@435: #endif duke@435: _count = 0; duke@435: _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block; duke@435: duke@435: if (_max_blocks <= preallocated_block_list_size) { duke@435: _blocks = _preallocated_block_list; duke@435: } else { duke@435: _blocks = (IndexSet::BitBlock**) arena()->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); duke@435: } duke@435: for (uint i = 0; i < _max_blocks; i++) { duke@435: set_block(i, &_empty_block); duke@435: } duke@435: } duke@435: duke@435: //---------------------------- IndexSet::initialize()------------------------------ duke@435: // Prepare an IndexSet for use. If it needs to allocate its _blocks array, it does duke@435: // so from the Arena passed as a parameter. BitBlock allocation is still done from duke@435: // the static Arena which was set with reset_memory(). duke@435: duke@435: void IndexSet::initialize(uint max_elements, Arena *arena) { duke@435: #ifdef ASSERT duke@435: _serial_number = _serial_count++; duke@435: check_watch("initialized2", max_elements); duke@435: _max_elements = max_elements; duke@435: #endif // ASSERT duke@435: _count = 0; duke@435: _max_blocks = (max_elements + bits_per_block - 1) / bits_per_block; duke@435: duke@435: if (_max_blocks <= preallocated_block_list_size) { duke@435: _blocks = _preallocated_block_list; duke@435: } else { duke@435: _blocks = (IndexSet::BitBlock**) arena->Amalloc_4(sizeof(IndexSet::BitBlock**) * _max_blocks); duke@435: } duke@435: for (uint i = 0; i < _max_blocks; i++) { duke@435: set_block(i, &_empty_block); duke@435: } duke@435: } duke@435: duke@435: //---------------------------- IndexSet::swap() ----------------------------- duke@435: // Exchange two IndexSets. duke@435: duke@435: void IndexSet::swap(IndexSet *set) { duke@435: #ifdef ASSERT duke@435: assert(_max_elements == set->_max_elements, "must have same universe size to swap"); duke@435: check_watch("swap", set->_serial_number); duke@435: set->check_watch("swap", _serial_number); duke@435: #endif duke@435: duke@435: for (uint i = 0; i < _max_blocks; i++) { duke@435: BitBlock *temp = _blocks[i]; duke@435: set_block(i, set->_blocks[i]); duke@435: set->set_block(i, temp); duke@435: } duke@435: uint temp = _count; duke@435: _count = set->_count; duke@435: set->_count = temp; duke@435: } duke@435: duke@435: //---------------------------- IndexSet::dump() ----------------------------- duke@435: // Print this set. Used for debugging. duke@435: duke@435: #ifndef PRODUCT duke@435: void IndexSet::dump() const { duke@435: IndexSetIterator elements(this); duke@435: duke@435: tty->print("{"); duke@435: uint i; duke@435: while ((i = elements.next()) != 0) { duke@435: tty->print("L%d ", i); duke@435: } duke@435: tty->print_cr("}"); duke@435: } duke@435: #endif duke@435: duke@435: #ifdef ASSERT duke@435: //---------------------------- IndexSet::tally_iteration_statistics() ----------------------------- duke@435: // Update block/bit counts to reflect that this set has been iterated over. duke@435: duke@435: void IndexSet::tally_iteration_statistics() const { kvn@2557: inc_stat_counter(&_total_bits, count()); duke@435: duke@435: for (uint i = 0; i < _max_blocks; i++) { duke@435: if (_blocks[i] != &_empty_block) { kvn@2557: inc_stat_counter(&_total_used_blocks, 1); duke@435: } else { kvn@2557: inc_stat_counter(&_total_unused_blocks, 1); duke@435: } duke@435: } duke@435: } duke@435: duke@435: //---------------------------- IndexSet::print_statistics() ----------------------------- duke@435: // Print statistics about IndexSet usage. duke@435: duke@435: void IndexSet::print_statistics() { kvn@2557: julong total_blocks = _total_used_blocks + _total_unused_blocks; duke@435: tty->print_cr ("Accumulated IndexSet usage statistics:"); duke@435: tty->print_cr ("--------------------------------------"); duke@435: tty->print_cr (" Iteration:"); kvn@2557: tty->print_cr (" blocks visited: " UINT64_FORMAT, total_blocks); kvn@2557: tty->print_cr (" blocks empty: %4.2f%%", 100.0*(double)_total_unused_blocks/total_blocks); kvn@2557: tty->print_cr (" bit density (bits/used blocks): %4.2f", (double)_total_bits/_total_used_blocks); kvn@2557: tty->print_cr (" bit density (bits/all blocks): %4.2f", (double)_total_bits/total_blocks); duke@435: tty->print_cr (" Allocation:"); kvn@2557: tty->print_cr (" blocks allocated: " UINT64_FORMAT, _alloc_new); kvn@2557: tty->print_cr (" blocks used/reused: " UINT64_FORMAT, _alloc_total); duke@435: } duke@435: duke@435: //---------------------------- IndexSet::verify() ----------------------------- duke@435: // Expensive test of IndexSet sanity. Ensure that the count agrees with the duke@435: // number of bits in the blocks. Make sure the iterator is seeing all elements duke@435: // of the set. Meant for use during development. duke@435: duke@435: void IndexSet::verify() const { duke@435: assert(!member(0), "zero cannot be a member"); duke@435: uint count = 0; duke@435: uint i; duke@435: for (i = 1; i < _max_elements; i++) { duke@435: if (member(i)) { duke@435: count++; duke@435: assert(count <= _count, "_count is messed up"); duke@435: } duke@435: } duke@435: duke@435: IndexSetIterator elements(this); duke@435: count = 0; duke@435: while ((i = elements.next()) != 0) { duke@435: count++; duke@435: assert(member(i), "returned a non member"); duke@435: assert(count <= _count, "iterator returned wrong number of elements"); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: //---------------------------- IndexSetIterator() ----------------------------- duke@435: // Create an iterator for a set. If empty blocks are detected when iterating duke@435: // over the set, these blocks are replaced. duke@435: duke@435: IndexSetIterator::IndexSetIterator(IndexSet *set) { duke@435: #ifdef ASSERT duke@435: if (CollectIndexSetStatistics) { duke@435: set->tally_iteration_statistics(); duke@435: } duke@435: set->check_watch("traversed", set->count()); duke@435: #endif duke@435: if (set->is_empty()) { duke@435: _current = 0; duke@435: _next_word = IndexSet::words_per_block; duke@435: _next_block = 1; duke@435: _max_blocks = 1; duke@435: duke@435: // We don't need the following values when we iterate over an empty set. duke@435: // The commented out code is left here to document that the omission duke@435: // is intentional. duke@435: // duke@435: //_value = 0; duke@435: //_words = NULL; duke@435: //_blocks = NULL; duke@435: //_set = NULL; duke@435: } else { duke@435: _current = 0; duke@435: _value = 0; duke@435: _next_block = 0; duke@435: _next_word = IndexSet::words_per_block; duke@435: duke@435: _max_blocks = set->_max_blocks; duke@435: _words = NULL; duke@435: _blocks = set->_blocks; duke@435: _set = set; duke@435: } duke@435: } duke@435: duke@435: //---------------------------- IndexSetIterator(const) ----------------------------- duke@435: // Iterate over a constant IndexSet. duke@435: duke@435: IndexSetIterator::IndexSetIterator(const IndexSet *set) { duke@435: #ifdef ASSERT duke@435: if (CollectIndexSetStatistics) { duke@435: set->tally_iteration_statistics(); duke@435: } duke@435: // We don't call check_watch from here to avoid bad recursion. duke@435: // set->check_watch("traversed const", set->count()); duke@435: #endif duke@435: if (set->is_empty()) { duke@435: _current = 0; duke@435: _next_word = IndexSet::words_per_block; duke@435: _next_block = 1; duke@435: _max_blocks = 1; duke@435: duke@435: // We don't need the following values when we iterate over an empty set. duke@435: // The commented out code is left here to document that the omission duke@435: // is intentional. duke@435: // duke@435: //_value = 0; duke@435: //_words = NULL; duke@435: //_blocks = NULL; duke@435: //_set = NULL; duke@435: } else { duke@435: _current = 0; duke@435: _value = 0; duke@435: _next_block = 0; duke@435: _next_word = IndexSet::words_per_block; duke@435: duke@435: _max_blocks = set->_max_blocks; duke@435: _words = NULL; duke@435: _blocks = set->_blocks; duke@435: _set = NULL; duke@435: } duke@435: } duke@435: duke@435: //---------------------------- List16Iterator::advance_and_next() ----------------------------- duke@435: // Advance to the next non-empty word in the set being iterated over. Return the next element duke@435: // if there is one. If we are done, return 0. This method is called from the next() method duke@435: // when it gets done with a word. duke@435: duke@435: uint IndexSetIterator::advance_and_next() { duke@435: // See if there is another non-empty word in the current block. duke@435: for (uint wi = _next_word; wi < (unsigned)IndexSet::words_per_block; wi++) { duke@435: if (_words[wi] != 0) { duke@435: // Found a non-empty word. duke@435: _value = ((_next_block - 1) * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word); duke@435: _current = _words[wi]; duke@435: duke@435: _next_word = wi+1; duke@435: duke@435: return next(); duke@435: } duke@435: } duke@435: duke@435: // We ran out of words in the current block. Advance to next non-empty block. duke@435: for (uint bi = _next_block; bi < _max_blocks; bi++) { duke@435: if (_blocks[bi] != &IndexSet::_empty_block) { duke@435: // Found a non-empty block. duke@435: duke@435: _words = _blocks[bi]->words(); duke@435: for (uint wi = 0; wi < (unsigned)IndexSet::words_per_block; wi++) { duke@435: if (_words[wi] != 0) { duke@435: // Found a non-empty word. duke@435: _value = (bi * IndexSet::bits_per_block) + (wi * IndexSet::bits_per_word); duke@435: _current = _words[wi]; duke@435: duke@435: _next_block = bi+1; duke@435: _next_word = wi+1; duke@435: duke@435: return next(); duke@435: } duke@435: } duke@435: duke@435: // All of the words in the block were empty. Replace duke@435: // the block with the empty block. duke@435: if (_set) { duke@435: _set->free_block(bi); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // These assignments make redundant calls to next on a finished iterator duke@435: // faster. Probably not necessary. duke@435: _next_block = _max_blocks; duke@435: _next_word = IndexSet::words_per_block; duke@435: duke@435: // No more words. duke@435: return 0; duke@435: }