Skip to content

Commit

Permalink
[SS-000] 패키지이름 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
J0onYEong committed Oct 29, 2024
1 parent 7e15ed1 commit d76f385
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 164 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SwiftStructures.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>YeongKit-Package.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>YeongKit.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>2</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>SwiftStructures</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>SwiftStructuresTests</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>YeongKit</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import PackageDescription

let package = Package(
name: "YeongKit",
name: "SwiftStructures",
platforms: [
.iOS(.v15),
.macOS(.v10_15)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "YeongKit",
name: "SwiftStructures",
targets: ["SwiftStructures"]),
],
targets: [
Expand Down
156 changes: 153 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,158 @@
# YeongKit by junios
# SwiftStructures

## Included libraries
# Milestone

- [SwiftStructures](https://github.com/J0onYEong/YeongKit/tree/main/Sources/SwiftStructures)
- [x] Red-black tree
- [x] HashMap(powerd by RBTree)
- [ ] Thread-safe dictionary
- [x] NSLock
- [ ] Actor


## Red-black tree

This is a custom implementation of a Red-Black Tree in Swift. Red-Black Trees are self-balancing binary search trees, ensuring that the tree remains balanced during insertions and deletions. This tree is particularly useful for keeping operations like search, insert, and delete efficient, with a time complexity of O(log n).

### Features

- Self-Balancing: Ensures that the tree remains balanced after each insertion by using coloring and rotation.
- Unique Elements: The tree only supports unique elements. Inserting a duplicate element will throw an error.
- Non-Thread-Safe: The current implementation does not support thread-safe operations for appending and removing elements.

### Table of Contents

1. Usage
- Creating the Tree
- Appending Elements
- Tree Height
- Printing the Tree
2. Internal Operations
- Recoloring
- Restructuring
- Example
- License


## Usage

Creating the Tree

To initialize a Red-Black Tree, use the RBTree class.

```swift
let tree = RBTree<Int>()
```

You can also initialize the tree with a root element:

```swift
let tree = RBTree<Int>(value: 10)
```

Appending Elements

You can append elements to the tree using the append function. You can append a single element or a list of elements.

``` swift
try tree.append(5) // Appends a single element
try tree.append([20, 15, 25]) // Appends multiple elements

```

### Tree Height

You can get the current height of the tree by accessing the height property.

```swift
print("Tree height: \(tree.height)")
```

### Internal Operations

#### Recoloring

Recoloring is used when both the parent and uncle of a newly inserted node are red. It recolors the parent and uncle to black and the grandparent to red.

- If the grandparent is the root, it is recolored to black.

#### Restructuring

When recoloring isn’t enough to maintain the Red-Black Tree properties, restructuring (or rotation) is used to restore balance. This process involves sorting the new node, parent, and grandparent values, selecting the middle value as the new parent, and assigning the other two nodes as its children.

> NOTE: if middle node had children already, **children are relocated** in tree restructured.

This operation ensures the balance of the tree while maintaining the Red-Black Tree rules for node colors and structure.

### Example

```swift
let tree = RBTree<Int>()

try tree.append(10)
try tree.append([5, 20, 15, 25])

try tree.remove(5)
```

## HashMap

The HashMap performs thread-safe operations during insertion and deletion. The sortedList function returns an array of values based on the sorted key order. When a count parameter is provided, the function limits the number of returned values, optimized to run in **O(log N) + the number of count** operations.

#### Example
```swift
let hashMap = HashMap<Int, String>()

hashMap[1] = "One"
hashMap[2] = "Two"

hashMap.remove(1)
hashMap.remove(2)
```

### sortedList method

The keys in the HashMap are internally managed by a Red-Black Tree. This structure is used to quickly sort the keys stored in the dictionary. The sorted method allows you to retrieve a list of values in ascending or descending order.
```swift
let hashMap = HashMap<Int, String>()
let testCase = (1...10).shuffled()

testCase.forEach { (element) in
hashMap[element] = String(element)
}

// result : ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
hashMap.ascendingValues(10)

// result : ["10", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
hashMap.descendingValues(10)

```
If the count exceeds the number of key-value pairs in the dictionary, it will be set to the total pair count of the dictionary.


## Thread-safe dictionary

### LockedDictionary

LockedDictionary is a custom thread-safe wrapper around Swift’s native Dictionary. It ensures safe, concurrent access to its elements by using a lock (NSLock). This class is particularly useful in multithreaded environments where concurrent reads and writes to a dictionary could lead to data races or inconsistencies.

#### Example

```swift
let dictionary = LockedDictionary<Int, String>()

// Add elements
dictionary[1] = "One"
dictionary[2] = "Two"

// Access elements
let value = dictionary[1] // "One"

dictionary.remove(key: 1)
let value = dictionary[1] // nil, since the element was removed

```

## License

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
159 changes: 0 additions & 159 deletions Sources/SwiftStructures/Readme.md

This file was deleted.

0 comments on commit d76f385

Please sign in to comment.