ALERROR CIntSet::AddElement (int iElement) // AddElement // // Adds an element to the set { ALERROR error; DWORD dwBucket = ((DWORD)iElement) / g_BitsPerBucket; // Look for the bucket; if we don't find it, add a new one CIntArray *pBucket; if (error = m_Set.Lookup(dwBucket, (CObject **)&pBucket)) { pBucket = new CIntArray; if (pBucket == NULL) return ERR_MEMORY; if (error = m_Set.AddEntry(dwBucket, pBucket)) { delete pBucket; return error; } } // Look for the bit in the bucket DWORD dwBucketBit = ((DWORD)iElement) % g_BitsPerBucket; DWORD dwOffset = dwBucketBit / 32; // Make sure there is room in the bucket if ((int)dwOffset >= pBucket->GetCount()) { int iOldCount = pBucket->GetCount(); int iExpandBy = ((int)dwOffset + 1) - iOldCount; if (error = pBucket->ExpandArray(-1, iExpandBy)) return error; for (int i = iOldCount; i < pBucket->GetCount(); i++) pBucket->ReplaceElement(i, 0); } // OR-in the new element DWORD dwBit = dwBucketBit % 32; DWORD dwNewValue = ((DWORD)pBucket->GetElement(dwOffset)) | (1 << dwBit); pBucket->ReplaceElement(dwOffset, dwNewValue); return NOERROR; }
void CIntSet::RemoveElement (int iElement) // RemoveElement // // Removes the given element in the set { DWORD dwBucket = ((DWORD)iElement) / g_BitsPerBucket; // Look for the bucket CIntArray *pBucket; if (m_Set.Lookup(dwBucket, (CObject **)&pBucket) != NOERROR) return; DWORD dwBucketBit = ((DWORD)iElement) % g_BitsPerBucket; DWORD dwOffset = dwBucketBit / 32; if ((int)dwOffset >= pBucket->GetCount()) return; DWORD dwBit = dwBucketBit % 32; DWORD dwNewValue = ((DWORD)pBucket->GetElement(dwOffset)) & ~(1 << dwBit); pBucket->ReplaceElement(dwOffset, dwNewValue); }