OrderedDictionary.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // OrderedDictionary.h
  3. //
  4. // Version 1.4
  5. //
  6. // Created by Nick Lockwood on 21/09/2010.
  7. // Copyright 2010 Charcoal Design
  8. //
  9. // Distributed under the permissive zlib license
  10. // Get the latest version from here:
  11. //
  12. // https://github.com/nicklockwood/OrderedDictionary
  13. //
  14. // This software is provided 'as-is', without any express or implied
  15. // warranty. In no event will the authors be held liable for any damages
  16. // arising from the use of this software.
  17. //
  18. // Permission is granted to anyone to use this software for any purpose,
  19. // including commercial applications, and to alter it and redistribute it
  20. // freely, subject to the following restrictions:
  21. //
  22. // 1. The origin of this software must not be misrepresented; you must not
  23. // claim that you wrote the original software. If you use this software
  24. // in a product, an acknowledgment in the product documentation would be
  25. // appreciated but is not required.
  26. //
  27. // 2. Altered source versions must be plainly marked as such, and must not be
  28. // misrepresented as being the original software.
  29. //
  30. // 3. This notice may not be removed or altered from any source distribution.
  31. //
  32. #import <Foundation/Foundation.h>
  33. NS_ASSUME_NONNULL_BEGIN
  34. /**
  35. * Ordered subclass of NSDictionary.
  36. * Supports all the same methods as NSDictionary, plus a few
  37. * new methods for operating on entities by index rather than key.
  38. */
  39. @interface OrderedDictionary<__covariant KeyType, __covariant ObjectType> : NSDictionary<KeyType, ObjectType>
  40. /**
  41. * These methods can be used to load an XML plist file. The file must have a
  42. * dictionary node as its root object, and all dictionaries in the file will be
  43. * treated as ordered. Currently, only XML plist files are supported, not
  44. * binary or ascii. Xcode will automatically convert XML plists included in the
  45. * project to binary files in built apps, so you will need to disable that
  46. * functionality if you wish to load them with these functions. A good approach
  47. * is to rename such files with a .xml extension instead of .plist. See the
  48. * OrderedDictionary README file for more details.
  49. */
  50. + (nullable instancetype)dictionaryWithContentsOfFile:(NSString *)path;
  51. + (nullable instancetype)dictionaryWithContentsOfURL:(NSURL *)url;
  52. - (nullable instancetype)initWithContentsOfFile:(NSString *)path;
  53. - (nullable instancetype)initWithContentsOfURL:(NSURL *)url;
  54. /** Returns the nth key in the dictionary. */
  55. - (KeyType)keyAtIndex:(NSUInteger)index;
  56. /** Returns the nth object in the dictionary. */
  57. - (ObjectType)objectAtIndex:(NSUInteger)index;
  58. - (ObjectType)objectAtIndexedSubscript:(NSUInteger)index;
  59. /** Returns the index of the specified key, or NSNotFound if key is not found. */
  60. - (NSUInteger)indexOfKey:(KeyType)key;
  61. /** Returns an enumerator for backwards traversal of the dictionary keys. */
  62. - (NSEnumerator<KeyType> *)reverseKeyEnumerator;
  63. /** Returns an enumerator for backwards traversal of the dictionary objects. */
  64. - (NSEnumerator<ObjectType> *)reverseObjectEnumerator;
  65. /** Enumerates keys ands objects with index using block. */
  66. - (void)enumerateKeysAndObjectsWithIndexUsingBlock:(void (^)(KeyType key, ObjectType obj, NSUInteger idx, BOOL *stop))block;
  67. @end
  68. /**
  69. * Mutable subclass of OrderedDictionary.
  70. * Supports all the same methods as NSMutableDictionary, plus a few
  71. * new methods for operating on entities by index rather than key.
  72. * Note that although it has the same interface, MutableOrderedDictionary
  73. * is not a subclass of NSMutableDictionary, and cannot be used as one
  74. * without generating compiler warnings (unless you cast it).
  75. */
  76. @interface MutableOrderedDictionary<KeyType, ObjectType> : OrderedDictionary<KeyType, ObjectType>
  77. + (instancetype)dictionaryWithCapacity:(NSUInteger)count;
  78. - (instancetype)initWithCapacity:(NSUInteger)count;
  79. - (void)addEntriesFromDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
  80. - (void)removeAllObjects;
  81. - (void)removeObjectForKey:(KeyType)key;
  82. - (void)removeObjectsForKeys:(NSArray<KeyType> *)keyArray;
  83. - (void)setDictionary:(NSDictionary<KeyType, ObjectType> *)otherDictionary;
  84. - (void)setObject:(ObjectType)object forKey:(KeyType)key;
  85. - (void)setObject:(ObjectType)object forKeyedSubscript:(KeyType)key;
  86. /** Inserts an object at a specific index in the dictionary. */
  87. - (void)insertObject:(ObjectType)object forKey:(KeyType)key atIndex:(NSUInteger)index;
  88. /** Replace an object at a specific index in the dictionary. */
  89. - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(ObjectType)object;
  90. - (void)setObject:(ObjectType)object atIndexedSubscript:(NSUInteger)index;
  91. /** Swap the indexes of two key/value pairs in the dictionary. */
  92. - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
  93. /** Removes the nth object in the dictionary. */
  94. - (void)removeObjectAtIndex:(NSUInteger)index;
  95. /** Removes the objects at the specified indexes from the mutable ordered set. */
  96. - (void)removeObjectsAtIndexes:(NSIndexSet *)indexes;
  97. @end
  98. NS_ASSUME_NONNULL_END