Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Common / Utils / KeyToListMap.cs / 1305376 / KeyToListMap.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Collections.ObjectModel;
namespace System.Data.Common.Utils {
// This class contains an abstraction that map a key of type TKey to a
// list of values (of type TValue). This is really a convenience abstraction
internal class KeyToListMap : InternalBase {
#region Constructors
// effects: Creates an empty map with keys compared using comparer
internal KeyToListMap(IEqualityComparer comparer) {
Debug.Assert(null != comparer);
m_map = new Dictionary>(comparer);
}
#endregion
#region Fields
// Just a regular dictionary
private Dictionary> m_map;
#endregion
#region Properties
// effects: Yields all the keys in this
internal IEnumerable Keys {
get { return m_map.Keys; }
}
// effects: Returns all the values for all keys with all the values
// of a particular key adjacent to each other
internal IEnumerable AllValues {
get {
foreach (TKey key in Keys) {
foreach (TValue value in ListForKey(key)) {
yield return value;
}
}
}
}
// effects: Returns all the Dictionary Entries in this Map.
internal IEnumerable>> KeyValuePairs {
get {
return m_map;
}
}
#endregion
#region Methods
internal bool ContainsKey(TKey key) {
return m_map.ContainsKey(key);
}
// effects: Adds to this. If the entry already exists, another one is added
internal void Add(TKey key, TValue value) {
// If entry for key already exists, add value to the list, else
// create a new list and add the value to it
List valueList;
if (!m_map.TryGetValue(key, out valueList)) {
valueList = new List();
m_map[key] = valueList;
}
valueList.Add(value);
}
// effects: Adds for each value in values to this. If the entry already exists, another one is added
internal void AddRange(TKey key, IEnumerable values) {
foreach (TValue value in values) {
Add(key, value);
}
}
// effects: Removes all entries corresponding to key
// Returns true iff the key was removed
internal bool RemoveKey(TKey key) {
return m_map.Remove(key);
}
// requires: key exist in this
// effects: Returns the values associated with key
internal System.Collections.ObjectModel.ReadOnlyCollection ListForKey(TKey key) {
Debug.Assert(m_map.ContainsKey(key), "key not registered in map");
return new System.Collections.ObjectModel.ReadOnlyCollection(m_map[key]);
}
// effects: Returns true if the key exists and false if not.
// In case the Key exists, the out parameter is assigned the List for that key,
// otherwise it is assigned a null value
internal bool TryGetListForKey(TKey key, out System.Collections.ObjectModel.ReadOnlyCollection valueCollection)
{
List list;
valueCollection = null;
if (m_map.TryGetValue(key, out list))
{
valueCollection = new System.Collections.ObjectModel.ReadOnlyCollection(list);
return true;
}
return false;
}
// Returns all values for the given key. If no values have been added for the key,
// yields no values.
internal IEnumerable EnumerateValues(TKey key) {
List values;
if (m_map.TryGetValue(key, out values)) {
foreach (TValue value in values) { yield return value; }
}
}
internal override void ToCompactString(StringBuilder builder) {
foreach (TKey key in Keys) {
// Calling key's ToString here
StringUtil.FormatStringBuilder(builder, "{0}", key);
builder.Append(": ");
IEnumerable values = ListForKey(key);
StringUtil.ToSeparatedString(builder, values, ",", "null");
builder.Append("; ");
}
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Collections.ObjectModel;
namespace System.Data.Common.Utils {
// This class contains an abstraction that map a key of type TKey to a
// list of values (of type TValue). This is really a convenience abstraction
internal class KeyToListMap : InternalBase {
#region Constructors
// effects: Creates an empty map with keys compared using comparer
internal KeyToListMap(IEqualityComparer comparer) {
Debug.Assert(null != comparer);
m_map = new Dictionary>(comparer);
}
#endregion
#region Fields
// Just a regular dictionary
private Dictionary> m_map;
#endregion
#region Properties
// effects: Yields all the keys in this
internal IEnumerable Keys {
get { return m_map.Keys; }
}
// effects: Returns all the values for all keys with all the values
// of a particular key adjacent to each other
internal IEnumerable AllValues {
get {
foreach (TKey key in Keys) {
foreach (TValue value in ListForKey(key)) {
yield return value;
}
}
}
}
// effects: Returns all the Dictionary Entries in this Map.
internal IEnumerable>> KeyValuePairs {
get {
return m_map;
}
}
#endregion
#region Methods
internal bool ContainsKey(TKey key) {
return m_map.ContainsKey(key);
}
// effects: Adds to this. If the entry already exists, another one is added
internal void Add(TKey key, TValue value) {
// If entry for key already exists, add value to the list, else
// create a new list and add the value to it
List valueList;
if (!m_map.TryGetValue(key, out valueList)) {
valueList = new List();
m_map[key] = valueList;
}
valueList.Add(value);
}
// effects: Adds for each value in values to this. If the entry already exists, another one is added
internal void AddRange(TKey key, IEnumerable values) {
foreach (TValue value in values) {
Add(key, value);
}
}
// effects: Removes all entries corresponding to key
// Returns true iff the key was removed
internal bool RemoveKey(TKey key) {
return m_map.Remove(key);
}
// requires: key exist in this
// effects: Returns the values associated with key
internal System.Collections.ObjectModel.ReadOnlyCollection ListForKey(TKey key) {
Debug.Assert(m_map.ContainsKey(key), "key not registered in map");
return new System.Collections.ObjectModel.ReadOnlyCollection(m_map[key]);
}
// effects: Returns true if the key exists and false if not.
// In case the Key exists, the out parameter is assigned the List for that key,
// otherwise it is assigned a null value
internal bool TryGetListForKey(TKey key, out System.Collections.ObjectModel.ReadOnlyCollection valueCollection)
{
List list;
valueCollection = null;
if (m_map.TryGetValue(key, out list))
{
valueCollection = new System.Collections.ObjectModel.ReadOnlyCollection(list);
return true;
}
return false;
}
// Returns all values for the given key. If no values have been added for the key,
// yields no values.
internal IEnumerable EnumerateValues(TKey key) {
List values;
if (m_map.TryGetValue(key, out values)) {
foreach (TValue value in values) { yield return value; }
}
}
internal override void ToCompactString(StringBuilder builder) {
foreach (TKey key in Keys) {
// Calling key's ToString here
StringUtil.FormatStringBuilder(builder, "{0}", key);
builder.Append(": ");
IEnumerable values = ListForKey(key);
StringUtil.ToSeparatedString(builder, values, ",", "null");
builder.Append("; ");
}
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- InvokeHandlers.cs
- WebHeaderCollection.cs
- ProtectedUri.cs
- XmlSchemaAttributeGroupRef.cs
- XmlWhitespace.cs
- ProfileInfo.cs
- AlphaSortedEnumConverter.cs
- SendingRequestEventArgs.cs
- ConfigurationManagerHelper.cs
- BitmapMetadataBlob.cs
- KeySplineConverter.cs
- Vector3DConverter.cs
- ResourceContainer.cs
- BitmapMetadataEnumerator.cs
- NativeMethods.cs
- ReferenceAssemblyAttribute.cs
- TearOffProxy.cs
- ConfigurationValidatorAttribute.cs
- DataGrid.cs
- Attribute.cs
- DiscoveryMessageSequenceCD1.cs
- SqlMetaData.cs
- RMPublishingDialog.cs
- FloaterParagraph.cs
- MessageRpc.cs
- SiteMapNodeItem.cs
- XmlValueConverter.cs
- SponsorHelper.cs
- XmlSchemaAttributeGroupRef.cs
- ObjectReaderCompiler.cs
- AppDomainManager.cs
- Italic.cs
- CommonProperties.cs
- VerticalAlignConverter.cs
- FullTrustAssemblyCollection.cs
- InputReferenceExpression.cs
- HttpCookiesSection.cs
- UTF32Encoding.cs
- PeerApplication.cs
- EventKeyword.cs
- XmlEntity.cs
- MaskInputRejectedEventArgs.cs
- MediaTimeline.cs
- TreeView.cs
- XmlSchemaAny.cs
- XmlDocumentFragment.cs
- PropertyRef.cs
- ApplicationGesture.cs
- CardSpaceShim.cs
- StringKeyFrameCollection.cs
- Compiler.cs
- WebInvokeAttribute.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- MulticastDelegate.cs
- ScriptingJsonSerializationSection.cs
- BindValidationContext.cs
- PieceNameHelper.cs
- URL.cs
- DiagnosticsConfigurationHandler.cs
- WindowsListViewSubItem.cs
- __Filters.cs
- ConcurrentQueue.cs
- GeometryDrawing.cs
- SafeArrayTypeMismatchException.cs
- FontEmbeddingManager.cs
- InlineCollection.cs
- BrowserCapabilitiesCodeGenerator.cs
- XmlSerializerAssemblyAttribute.cs
- TrackingProfileManager.cs
- BroadcastEventHelper.cs
- SharedUtils.cs
- ComponentSerializationService.cs
- PrincipalPermission.cs
- TagPrefixInfo.cs
- KeyValueConfigurationElement.cs
- CookieProtection.cs
- WindowsStatusBar.cs
- Rect.cs
- ActivityExecutionContext.cs
- StylusCollection.cs
- StringReader.cs
- ProfilePropertyNameValidator.cs
- WhereQueryOperator.cs
- SimpleModelProvider.cs
- FrameworkTextComposition.cs
- ParseElementCollection.cs
- Options.cs
- DesignerActionService.cs
- XmlQueryTypeFactory.cs
- objectquery_tresulttype.cs
- BitmapEncoder.cs
- RMPublishingDialog.cs
- FocusWithinProperty.cs
- CodeIdentifiers.cs
- SrgsRulesCollection.cs
- HttpProfileGroupBase.cs
- StackBuilderSink.cs
- XmlDocumentFieldSchema.cs
- SystemColors.cs
- GACMembershipCondition.cs