Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Shared / MS / Utility / ItemList.cs / 1 / ItemList.cs
using System;
using MS.Internal.WindowsBase;
namespace MS.Utility
{
//
// ItemStructList
//
[FriendAccessAllowed] // Built into Base, also used by Framework.
internal struct ItemStructList
{
public ItemStructList(int capacity)
{
List = new T[capacity];
Count = 0;
}
//
// Non-lock-required Read methods
// (Always safe to call when locking "safe" write operations are used)
//
public T[] List;
public int Count;
public void EnsureIndex(int index)
{
int delta = (index + 1) - Count;
if (delta > 0)
{
Add(delta);
}
}
public bool IsValidIndex(int index)
{
return (index >= 0 && index < Count);
}
public int IndexOf(T value)
{
int index = -1;
for (int i = 0; i < Count; i++)
{
if (List[i].Equals(value))
{
index = i;
break;
}
}
return index;
}
public bool Contains(T value)
{
return (IndexOf(value) != -1);
}
//
// Lock-required Write operations
// "Safe" methods for Reader lock-free operation
//
// Increase size by one, new value is provided
public void Add(T item)
{
// Add without Count adjustment (incr Count after valid item added)
int index = Add(1, false);
List[index] = item;
Count++;
}
// Increase size by one, new value is provided
public void Add(ref T item)
{
// Add without Count adjustment (incr Count after valid item added)
int index = Add(1, false);
List[index] = item;
Count++;
}
// Increase size by one, new value is default value
public int Add()
{
return Add(1, true);
}
// Increase size of array by delta, fill with default values
public int Add(int delta)
{
return Add(delta, true);
}
// Increase size of array by delta, fill with default values
// Allow disable of automatic Count increment so that cases where
// non-default values are to be added to the list can be done before
// count is changed. This is important for non-locking scenarios
// (i.e. count is adjusted after array size changes)
private int Add(int delta, bool incrCount)
{
if (List != null)
{
if ((Count + delta) > List.Length)
{
T[] newList = new T[Math.Max(List.Length * 2, Count + delta)];
List.CopyTo(newList, 0);
List = newList;
}
}
else
{
List = new T[Math.Max(delta, 2)];
}
// New arrays auto-initialized to default entry values
// Any resued entried have already been cleared out by Remove or Clear
int index = Count;
// Optional adjustment of Count
if (incrCount)
{
// Adjust count after resize so that array bounds and data
// are never invalid (good for locking writes without synchronized reads)
Count += delta;
}
return index;
}
public void Sort()
{
if (List != null)
{
Array.Sort(List, 0, Count);
}
}
public void AppendTo(ref ItemStructList destinationList)
{
for (int i = 0; i < Count; i++)
{
destinationList.Add(ref List[i]);
}
}
public T[] ToArray()
{
T[] array = new T[Count];
Array.Copy(List, 0, array, 0, Count);
return array;
}
//
// Lock-required Write operations
// "UNSafe" methods for Reader lock-free operation
//
// If any of these methods are called, the entire class is considered
// unsafe for Reader lock-free operation for that point on (meaning
// Reader locks must be taken)
//
public void Clear()
{
// Return now unused entries back to default
Array.Clear(List, 0, Count);
Count = 0;
}
public void Remove(T value)
{
int index = IndexOf(value);
if (index != -1)
{
// Shift entries down
Array.Copy(List, index + 1, List, index, (Count - index - 1));
// Return now unused entries back to default
Array.Clear(List, Count - 1, 1);
Count--;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using MS.Internal.WindowsBase;
namespace MS.Utility
{
//
// ItemStructList
//
[FriendAccessAllowed] // Built into Base, also used by Framework.
internal struct ItemStructList
{
public ItemStructList(int capacity)
{
List = new T[capacity];
Count = 0;
}
//
// Non-lock-required Read methods
// (Always safe to call when locking "safe" write operations are used)
//
public T[] List;
public int Count;
public void EnsureIndex(int index)
{
int delta = (index + 1) - Count;
if (delta > 0)
{
Add(delta);
}
}
public bool IsValidIndex(int index)
{
return (index >= 0 && index < Count);
}
public int IndexOf(T value)
{
int index = -1;
for (int i = 0; i < Count; i++)
{
if (List[i].Equals(value))
{
index = i;
break;
}
}
return index;
}
public bool Contains(T value)
{
return (IndexOf(value) != -1);
}
//
// Lock-required Write operations
// "Safe" methods for Reader lock-free operation
//
// Increase size by one, new value is provided
public void Add(T item)
{
// Add without Count adjustment (incr Count after valid item added)
int index = Add(1, false);
List[index] = item;
Count++;
}
// Increase size by one, new value is provided
public void Add(ref T item)
{
// Add without Count adjustment (incr Count after valid item added)
int index = Add(1, false);
List[index] = item;
Count++;
}
// Increase size by one, new value is default value
public int Add()
{
return Add(1, true);
}
// Increase size of array by delta, fill with default values
public int Add(int delta)
{
return Add(delta, true);
}
// Increase size of array by delta, fill with default values
// Allow disable of automatic Count increment so that cases where
// non-default values are to be added to the list can be done before
// count is changed. This is important for non-locking scenarios
// (i.e. count is adjusted after array size changes)
private int Add(int delta, bool incrCount)
{
if (List != null)
{
if ((Count + delta) > List.Length)
{
T[] newList = new T[Math.Max(List.Length * 2, Count + delta)];
List.CopyTo(newList, 0);
List = newList;
}
}
else
{
List = new T[Math.Max(delta, 2)];
}
// New arrays auto-initialized to default entry values
// Any resued entried have already been cleared out by Remove or Clear
int index = Count;
// Optional adjustment of Count
if (incrCount)
{
// Adjust count after resize so that array bounds and data
// are never invalid (good for locking writes without synchronized reads)
Count += delta;
}
return index;
}
public void Sort()
{
if (List != null)
{
Array.Sort(List, 0, Count);
}
}
public void AppendTo(ref ItemStructList destinationList)
{
for (int i = 0; i < Count; i++)
{
destinationList.Add(ref List[i]);
}
}
public T[] ToArray()
{
T[] array = new T[Count];
Array.Copy(List, 0, array, 0, Count);
return array;
}
//
// Lock-required Write operations
// "UNSafe" methods for Reader lock-free operation
//
// If any of these methods are called, the entire class is considered
// unsafe for Reader lock-free operation for that point on (meaning
// Reader locks must be taken)
//
public void Clear()
{
// Return now unused entries back to default
Array.Clear(List, 0, Count);
Count = 0;
}
public void Remove(T value)
{
int index = IndexOf(value);
if (index != -1)
{
// Shift entries down
Array.Copy(List, index + 1, List, index, (Count - index - 1));
// Return now unused entries back to default
Array.Clear(List, Count - 1, 1);
Count--;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SchemaElementLookUpTable.cs
- SoapSchemaExporter.cs
- DynamicUpdateCommand.cs
- CodeDefaultValueExpression.cs
- ReadOnlyDictionary.cs
- UniformGrid.cs
- DataSourceCache.cs
- FileUpload.cs
- SqlUserDefinedTypeAttribute.cs
- ContainerControlDesigner.cs
- SecurityElement.cs
- ColorAnimation.cs
- HttpCacheVaryByContentEncodings.cs
- TraceHandler.cs
- HttpCacheVaryByContentEncodings.cs
- DataException.cs
- ServerIdentity.cs
- UmAlQuraCalendar.cs
- InputManager.cs
- SafeCryptoHandles.cs
- HtmlElementErrorEventArgs.cs
- CodeAttributeArgument.cs
- WindowsSlider.cs
- TreeNodeBindingCollection.cs
- AsyncResult.cs
- AppDomainUnloadedException.cs
- WebPartAddingEventArgs.cs
- MappedMetaModel.cs
- WinCategoryAttribute.cs
- DockAndAnchorLayout.cs
- BindingBase.cs
- IRCollection.cs
- ToolStripTemplateNode.cs
- FontInfo.cs
- IIS7UserPrincipal.cs
- MimeTypeMapper.cs
- DataGridViewImageColumn.cs
- EventPrivateKey.cs
- SqlEnums.cs
- PaginationProgressEventArgs.cs
- validationstate.cs
- AttributeUsageAttribute.cs
- Expression.cs
- DispatcherHookEventArgs.cs
- MdiWindowListStrip.cs
- SecurityHeaderElementInferenceEngine.cs
- UserCancellationException.cs
- TrackingMemoryStreamFactory.cs
- ModelUIElement3D.cs
- SimpleMailWebEventProvider.cs
- Parameter.cs
- SamlSecurityToken.cs
- PropertyItem.cs
- MethodToken.cs
- RegexCompiler.cs
- EntityParameterCollection.cs
- InvokeBase.cs
- InstanceData.cs
- Schema.cs
- ListDataBindEventArgs.cs
- HTTP_SERVICE_CONFIG_URLACL_KEY.cs
- Schema.cs
- ActiveXSerializer.cs
- CornerRadius.cs
- ServiceNotStartedException.cs
- ListViewGroup.cs
- PersonalizationProvider.cs
- SQLSingle.cs
- ExtendedProperty.cs
- Script.cs
- StreamGeometry.cs
- InputQueue.cs
- FrameworkName.cs
- SpeechSynthesizer.cs
- SpecularMaterial.cs
- ThrowHelper.cs
- ContactManager.cs
- CompressEmulationStream.cs
- HtmlElementEventArgs.cs
- Rect3DValueSerializer.cs
- ChainedAsyncResult.cs
- FormViewDeleteEventArgs.cs
- XamlSerializationHelper.cs
- HttpProfileBase.cs
- CellConstant.cs
- SiteMapNodeCollection.cs
- PropertyGridEditorPart.cs
- DescendantOverDescendantQuery.cs
- GeneralTransform3DGroup.cs
- GenerateScriptTypeAttribute.cs
- ComplexObject.cs
- PointLight.cs
- util.cs
- ReadWriteObjectLock.cs
- DataBoundControlAdapter.cs
- BamlLocalizer.cs
- InitiatorServiceModelSecurityTokenRequirement.cs
- WebPartHeaderCloseVerb.cs
- CopyEncoder.cs
- NotifyIcon.cs