|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/marpit19/goquickmap/pkg/quickdict" |
| 7 | + "github.com/marpit19/goquickmap/pkg/quickmap" |
| 8 | + "github.com/marpit19/goquickmap/pkg/quickset" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + fmt.Println("GoQuickMap Demo") |
| 13 | + |
| 14 | + // QuickMap demo |
| 15 | + fmt.Println("\n--- QuickMap Demo ---") |
| 16 | + demoQuickMap() |
| 17 | + |
| 18 | + // QuickSet demo |
| 19 | + fmt.Println("\n--- QuickSet Demo ---") |
| 20 | + demoQuickSet() |
| 21 | + |
| 22 | + // QuickDict demo |
| 23 | + fmt.Println("\n--- QuickDict Demo ---") |
| 24 | + demoQuickDict() |
| 25 | +} |
| 26 | + |
| 27 | +func demoQuickMap() { |
| 28 | + // Create a new QuickMap with default capacity |
| 29 | + m := quickmap.New() |
| 30 | + |
| 31 | + // Insert some key-value pairs |
| 32 | + m.Insert("name", "Alice") |
| 33 | + m.Insert("age", 30) |
| 34 | + m.Insert("city", "New York") |
| 35 | + |
| 36 | + // Get and print values |
| 37 | + name, _ := m.Get("name") |
| 38 | + age, _ := m.Get("age") |
| 39 | + fmt.Printf("Name: %s, Age: %d\n", name, age) |
| 40 | + |
| 41 | + // Delete a key-value pair |
| 42 | + m.Delete("city") |
| 43 | + |
| 44 | + // Check if a key exists |
| 45 | + _, exists := m.Get("city") |
| 46 | + fmt.Printf("City exists: %v\n", exists) |
| 47 | + |
| 48 | + // Batch insert |
| 49 | + m.InsertMany(map[string]interface{}{ |
| 50 | + "country": "USA", |
| 51 | + "language": "English", |
| 52 | + }) |
| 53 | + |
| 54 | + // Batch delete |
| 55 | + m.DeleteMany([]string{"name", "age"}) |
| 56 | + |
| 57 | + // Print the size of the map |
| 58 | + fmt.Printf("Map size: %d\n", m.Size()) |
| 59 | + |
| 60 | + // Create a QuickMap with a specific initial capacity |
| 61 | + largeMap := quickmap.NewWithCapacity(1000) |
| 62 | + fmt.Printf("Large map initial size: %d\n", largeMap.Size()) |
| 63 | +} |
| 64 | + |
| 65 | +func demoQuickSet() { |
| 66 | + // Create a new QuickSet |
| 67 | + s := quickset.New() |
| 68 | + |
| 69 | + // Add some elements |
| 70 | + s.Add("apple") |
| 71 | + s.Add("banana") |
| 72 | + s.Add("cherry") |
| 73 | + |
| 74 | + // Check if an element exists |
| 75 | + fmt.Printf("Contains 'banana': %v\n", s.Contains("banana")) |
| 76 | + |
| 77 | + // Remove an element |
| 78 | + s.Remove("cherry") |
| 79 | + |
| 80 | + // Batch add |
| 81 | + s.AddMany([]string{"date", "elderberry", "fig"}) |
| 82 | + |
| 83 | + // Batch remove |
| 84 | + s.RemoveMany([]string{"apple", "banana"}) |
| 85 | + |
| 86 | + // Print the size of the set |
| 87 | + fmt.Printf("Set size: %d\n", s.Size()) |
| 88 | + |
| 89 | + // Create a QuickSet with a specific initial capacity |
| 90 | + largeSet := quickset.NewWithCapacity(1000) |
| 91 | + fmt.Printf("Large set initial size: %d\n", largeSet.Size()) |
| 92 | +} |
| 93 | + |
| 94 | +func demoQuickDict() { |
| 95 | + // Create a new QuickDict |
| 96 | + d := quickdict.New() |
| 97 | + |
| 98 | + // Set some key-value pairs |
| 99 | + d.Set("name", "Bob") |
| 100 | + d.Set("age", 25) |
| 101 | + d.Set("city", "San Francisco") |
| 102 | + |
| 103 | + // Get and print values |
| 104 | + name, _ := d.Get("name") |
| 105 | + age, _ := d.Get("age") |
| 106 | + fmt.Printf("Name: %s, Age: %d\n", name, age) |
| 107 | + |
| 108 | + // Delete a key-value pair |
| 109 | + d.Delete("city") |
| 110 | + |
| 111 | + // Batch set |
| 112 | + d.SetMany(map[string]interface{}{ |
| 113 | + "country": "USA", |
| 114 | + "language": "English", |
| 115 | + }) |
| 116 | + |
| 117 | + // Batch delete |
| 118 | + d.DeleteMany([]string{"name", "age"}) |
| 119 | + |
| 120 | + // Print the size of the dictionary |
| 121 | + fmt.Printf("Dictionary size: %d\n", d.Size()) |
| 122 | + |
| 123 | + // Create a QuickDict with a specific initial capacity |
| 124 | + largeDict := quickdict.NewWithCapacity(1000) |
| 125 | + fmt.Printf("Large dictionary initial size: %d\n", largeDict.Size()) |
| 126 | +} |
0 commit comments