Example #1
0
            internal EntityReference ClaimReference()
            {
                int  tempFreeIndex;
                int  newFreeIndex;
                uint version;

                do
                {
                    tempFreeIndex = _nextFreeIndex;
                    // Check if we need to create a new EntityLocator or whether we can recycle an existing one.
                    if ((uint)tempFreeIndex >= _entityReferenceMap.count)
                    {
                        newFreeIndex = tempFreeIndex + 1;
                        version      = 0;
                    }
                    else
                    {
                        ref EntityReferenceMapElement element = ref _entityReferenceMap[tempFreeIndex];
                        // The recycle entities form a linked list, using the egid.entityID to store the next element.
                        newFreeIndex = (int)element.egid.entityID;
                        version      = element.version;
                    }
                } while (tempFreeIndex != _nextFreeIndex.CompareExchange(newFreeIndex, tempFreeIndex));

#if DEBUG && !PROFILE_SVELTO
                // This code should be safe since we own the tempFreeIndex, this allows us to later check that nothing went wrong.
                if (tempFreeIndex < _entityReferenceMap.count)
                {
                    _entityReferenceMap[tempFreeIndex] = new EntityReferenceMapElement(new EGID(0, 0), version);
                }
#endif

                return(new EntityReference((uint)tempFreeIndex + 1, version));
            }
Example #2
0
            internal EntityReference CreateReferenceLocator(EGID egid)
            {
                // Check if we need to create a new EntityLocator or whether we can recycle an existing one.
                EntityReference reference;

                if (_nextReferenceIndex == _entityReferenceMap.count)
                {
                    _entityReferenceMap.Add(new EntityReferenceMapElement(egid));
                    reference = new EntityReference(_nextReferenceIndex++);
                }
                //if _nextEntityId is not equivalent to the count of entity references added so far, it is
                //pointing to the first deleted entity. A tombstone system recycles the entityID field to point
                //it to the next deleted entity, similar to a linked list algorithm.
                else
                {
                    ref EntityReferenceMapElement element = ref _entityReferenceMap[_nextReferenceIndex];
                    reference = new EntityReference(_nextReferenceIndex, element.version);
                    // The recycle entities form a linked list, using the egid.entityID to store the next element.
                    _nextReferenceIndex = element.egid.entityID;
                    element.egid        = egid;
                }